Nginx+tomcat+redis实现session共享

Nginx+tomcat+redis实现session共享
1,安装nginx,使用yum -y install nginx 这是epel源中的,需要安装epel源。
2,配置nginx。
 在nginx配置文件添加upstremserver和proxy,如下:
 upstream glq.com {
 #ip_hash;
 server 192.168.125.200:8080 weight=1;
 server 192.168.125.201:8080 weight=2;
 }
server {
    listen       80;
    #listen       [::]:80 default_server;
    server_name  _;
    #root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
 proxy_pass http://glq.com;  
 proxy_redirect default;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
3,配置tomcat,两台server分别为:192.168.125.200,192.168.125.201,每台主机要做如下配置:
下载tomcat,并配置相关变量。
https://tomcat.apache.org/download-80.cgi  下载
tar -xf apache-tomcat-xxx.tar.gz -C /usr/local/
ln -sv apache-tomcat-xx tomcat
vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
4,下载tomcat-redis-session-manager相关组件。
https://raw.githubusercontent.com/cc-chen/tomcat8.5-redis-session-manager/master/lib/jedis-2.5.2.jar
https://raw.githubusercontent.com/cc-chen/tomcat8.5-redis-session-manager/master/output/tomcat8.5-redis-session-manager.jar
https://raw.githubusercontent.com/cc-chen/tomcat8.5-redis-session-manager/master/lib/commons-pool2-2.2.jar
把以上3个jar包mv到tomcat下lib目录下,然后配置context.xml,添加如下:
<Valve className="com.s.tomcat.redissessions.RedisSessionHandlerValve"/>
<Manager className="com.s.tomcat.redissessions.RedisSessionManager"
    host="192.168.125.211"
    port="6379"
    database="0"
    password="123123"   //此处密码需要在redis打开密码选项。
    maxInactiveInterval="60" />
在webapps下新建testapp,并创建index.jsp:
 mkdir -pv testapp/{lib,classes,WEB-INF,META-INF}
创建index.jsp:
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.izyno.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("izyno.com","izyno.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
作为测试页,修改里面TomcatA为B,两台tomcat一样配置。
5,配置redis。
yum -y install gcc c++
下载最新的redis解压:tar -xf redis-4.0.8.tar.gz
cd redis-4.0.8
make
make PREFIX=/usr/local/redis install
cp redis.conf /usr/local/redis/
需要修改的配置:
requirepass 123123  //密码
daemonize yes   //开启守护进程
bind 0.0.0.0   //设置监听所有IP
然后启动redis:
bin/redis-server /usr/local/redis/redis.conf
查看redis端口:
[root@manager ~]# ss -tunlp
Netid  State      Recv-Q Send-Q                                                                                                     Local Address:Port                                                                                                           Peer Address:Port
tcp    LISTEN     0      128                                                                                                                   *:6379
6,测试。     
posted @ 2018-03-29 09:43  quicksand.F  阅读(296)  评论(0编辑  收藏  举报