黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...

1、准备两个tomcat

黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...
黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...
黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...

2、修改 两个 tomcat 的 server.xml 文件

port 端口号要修改,两个tomcat 的端口号不能一样

黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...

tomcat1

(1)<Server port="8015" shutdown="SHUTDOWN">

(2)<Connector URIEncoding="UTF-8" connectionTimeout="20000" 
                            port="8081" protocol="HTTP/1.1" redirectPort="8443"/>

(3)<Connector port="8019" protocol="AJP/1.3" redirectPort="8443"/>

tomcat2

(1)<Server port="8016" shutdown="SHUTDOWN">

(2)<Connector URIEncoding="UTF-8" connectionTimeout="20000" 
                            port="8082" protocol="HTTP/1.1" redirectPort="8443"/>

(3)<Connector port="8020" protocol="AJP/1.3" redirectPort="8443"/>

3、部署 projsession 项目 到 两个 tomcat 里面

projsession Code -> GitHub
https://github.com/liufengji/projsession.git

http://192.168.1.73/projsession/HelloServlet
http://192.168.2.1/projsession/HelloServlet

4、修改 nginx.conf 配置文件

[[email protected] ~]# cd /usr/local/nginx/conf
[[email protected] ~]# vim nginx.conf

http {

     upstream myserver{
           //通过hash取模分机器,最好使用公共缓存redis (session问题),使用redis时,  ip_hash注掉就可以
           #ip_hash;  //不使用redis时,ip_hash 注掉和不注掉 ,有两种效果
           server 192.168.2.1:8080 weight=1; // 权重方式
           server 192.168.2.1:8081 weight=1 backup; // backup 备机方式,主机都挂了,才上
       }

      server{
          location / {
             proxy_pass http://myserver;
             proxy_connect_timeout 10;
             proxy_redirect default;
           }
        }
}

5、nginx重新加载命令

修改配置后重新加载生效

[[email protected] ~]# cd /usr/local/nginx/
[[email protected] nginx]# sbin/nginx -s reload

6、通过nginx访问项目

http://192.168.2.102/projsession/HelloServlet

7、使用redis来存储 Session

黑猴子的家:Nginx 结合 Tomcat 实现配置负载均衡,配合使用 redis 实现Session共享...

(1)每次访问nginx,打印端口变了,实现了负载均衡,但是session也变了

http://192.168.2.102/projsession/HelloServlet
session:36,port:8081
session:78,port:8082

(2)session不变,方法一,修改nginx.conf文件,不注释 ip_hash
每次访问HelloServlet服务,session不变了,每个用户都是唯一的,sessionid通过hash取模,分配访问tomcat,即实现了session不变,也实现了负载均衡,但是有个问题就是,一旦其中一台tomcat 挂掉了,一部分 用户的session 就失效了,所以建议使用redis来存储session id

http://192.168.2.102/projsession/HelloServlet
session:36,port:8081

8、拷贝对应jar包到tomcat下lib包中

commons-pool2-2.0.jar
jedis-2.5.2
tomcat-redis-session-manager1.2.jar

9、修改tomcat的content.xml文件

host 需要改成 redis ip 地址

tomcat1

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
  <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
   host="192.168.2.102"  
   port="6379"  
   database="0"  
   maxInactiveInterval="60" /> 

tomcat2

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
  <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
   host="192.168.2.102"  
   port="6379"  
   database="0"  
   maxInactiveInterval="60" /> 

记得重启tomcat

10、修改 nginx.conf 配置文件,重启nginx

[[email protected] ~]# cd /usr/local/nginx/conf
[[email protected] ~]# vim nginx.conf
//注掉 ip_hash ,采用轮询的方式
 #ip_hash;  //不使用redis时,ip_hash 注掉和不注掉 ,有两种效果

[[email protected] ~]# cd /usr/local/nginx/
[[email protected] nginx]# sbin/nginx -s reload

11、启动redis,查看 session Id

[[email protected] ~]# redis-server /myredis/redis.conf
[[email protected] ~]# redis-cli
127.0.0.1:6379> keys *
127.0.0.1:6379> get 23k234sf83f35sf7345