码迷,mamicode.com
首页 > 其他好文 > 详细

Redis+Tomcat实现session绑定

时间:2018-06-18 10:59:46      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:tab   body   des   index   目的   centos7   mkdir   IV   create   

1.实验环境

实验系统:四台CentOS7.4,其中一台Nginx反向代理服务器,两台Tomcat服务器,一台Redis
实验目的:为了能够让客户端访问时,不管反向代理服务器代理到哪个服务器上都可以上客户端得到相同的数据,所以就应该有专用于存放session的服务器,Redis就可以充当次服务器
实现拓扑图:
技术分享图片
实验步骤:

  • Nginx代理配置
[root@nginx ~]# yum install -y nginx
[root@nginx ~]# vim /etc/nginx/conf.d/nginx.conf
upstream web {
        server 192.168.1.162:8080;
        server 192.168.1.161:8080;
}
server {
        listen 80;
        server_name nginx.lin.com;
        root /app/web/;
        index index.jsp index.html;
        location / {
                proxy_pass http://web;
        }
}
[root@nginx ~]# systemctl start nginx
  • Tomcat1
[root@tomcat1 ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel 
[root@tomcat1 ~]# vim /etc/tomcat/server.xml
    <Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2">
    <Host name="tomcat2.lin.com" appBase="/app/web/"
                unpackWARs="true" autoDeploy="true">
   </Host>
[root@tomcat1 ~]# mkdir /app/web/ROOT/META-INF/ -p
[root@tomcat1 ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF/
[root@tomcat1 ~]# vim /app/web/ROOT/META-INF/context.xml 
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="192.168.1.149"
         port="6379" 
         database="0"
        password="123456" 
         maxInactiveInterval="60" />
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

测试页面创建

[root@tomcat1 ROOT]# vim /app/web/ROOT/index.jsp 
<%@ page language="java" %>
                                        <html>
                                                <head><title>TomcatA</title></head>
                                                <body>
                                                        <h1><font color="red">TomcatA.magedu.com</font></h1>
                                                        <table align="centre" border="1">
                                                                <tr>
                                                                        <td>Session ID</td>
                                                                <% session.setAttribute("magedu.com","magedu.com"); %>
                                                                        <td><%= session.getId() %></td>
                                                                </tr>
                                                                <tr>
                                                                        <td>Created on</td>
                                                                        <td><%= session.getCreationTime() %></td>
                                                                </tr>
                                                        </table>
                                                </body>
                                        </html>
  • Tomcat2
[root@tomcat2 ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel 
[root@tomcat2 ~]# vim /etc/tomcat/server.xml
    <Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2">
    <Host name="tomcat2.lin.com" appBase="/app/web/"
                unpackWARs="true" autoDeploy="true">
   </Host>
[root@tomcat2 ~]# mkdir /app/web/ROOT/META-INF/ -p
[root@tomcat2 ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF
[root@tomcat2 ~]# vim /app/web/ROOT/META-INF/context.xml 
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="192.168.1.149"
         port="6379" 
         database="0"
        password="123456" 
         maxInactiveInterval="60" />
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

测试页面创建

[root@tomcat1 ROOT]# vim /app/web/ROOT/index.jsp 
<%@ page language="java" %>
                                        <html>
                                                <head><title>TomcatB</title></head>
                                                <body>
                                                        <h1><font color="red">TomcatB.magedu.com</font></h1>
                                                        <table align="centre" border="1">
                                                                <tr>
                                                                        <td>Session ID</td>
                                                                <% session.setAttribute("magedu.com","magedu.com"); %>
                                                                        <td><%= session.getId() %></td>
                                                                </tr>
                                                                <tr>
                                                                        <td>Created on</td>
                                                                        <td><%= session.getCreationTime() %></td>
                                                                </tr>
                                                        </table>
                                                </body>
                                        </html>

tomcat两台服务器上需要的jar包有:tomcat-redis-session-manager-2.0.0.jar、jedis-2.9.0.jar、commons-pool2-2.2.jar,可以从github中下载
将以上的三个jar包复制到/usr/share/tomcat/lib/目录下
最后重启服务

  • Redis缓存
[root@redis ~]# yum install -y redis
[root@redis ~]# vim /etc/redis.conf
bind 0.0.0.0
requirepass centos
[root@tomcat2 ~]# systemctl start redis

测试结果
技术分享图片
技术分享图片
redis服务器查询

127.0.0.1:6379> KEYS *
1) "2089214D59A2D2B1A76EE031C4D73FA8.jvm1.jvm1"
2) "8B8BA3B5715A5F0AC08E7223800E356C.jvm1.jvm1"
3) "9A9631C00441246A879E52A09295846C.jvm1.jvm1"
4) "61B59A937639E0CE8629A82A239BEE1B.jvm1.jvm1"
5) "mykey"
6) "905A1924350D75D599A4723F5D2D6CC9.jvm1.jvm1"

注意项说明:
1.注意三个jar包的版本,有些版本可能会不支持
2.注意redis是可以让其他服务器的访问

Redis+Tomcat实现session绑定

标签:tab   body   des   index   目的   centos7   mkdir   IV   create   

原文地址:http://blog.51cto.com/10492754/2130219

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!