Nginx+tomcat集群redis共享session应用方案

部署环境

主机 软件版本
192.168.88.1 nginx-1.12.2+redis-3.2.11
192.168.88.2 apache-tomcat-7.0.79 + jdk1.8
192.168.88.3 apache-tomcat-7.0.79 + jdk1.8

所需tomcat jar包,下载

  • commons-pool-1.5.4.jar
  • commons-pool2-2.4.1.jar
  • jedis-2.6.2.jar
  • tomcat-juli-adapters.jar
  • tomcat-juli.jar
  • tomcat-redis-session-manager1.2.jar

Tomcat配置

  1. 把tomcat jar包复制到$TOMCAT_BASE 下的lib目录
  2. 修改tomcat的配置文件conf/comtext.xml
#单点redis配置(我已经配置成功)
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
        host="192.168.88.1" 
        port="6379" 
        database="0" 
        maxInactiveInterval="60"/>
</Context>


#基于redis集群配置(我还没测试)
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
        maxInactiveInterval="60"
        sentinelMaster="mymaster" 
        sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381"/>
</Context>

  3、添加一个tomcat测试页index.jsp

# cat /opt/tomcat/webapps/ROOT/index.jsp 
<html>
<head>
   <title>nginx tomcat session test</title>
   <meta name="Generator" content="EditPlus">
   <meta name="Author" content="">
   <meta name="Keywords" content="">
   <meta name="Description" content="">
</head>

<h1>tomcat88.2</h1>    #这里192.168.88.3机器改为tomcat88.3
session: <%=session.getId()%>
</html>

  4、启动tomcat

nginx配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream tomcat {
        server 192.168.88.2:8080 weight=1;
        server 192.168.88.3:8080 weight=1;
    }


    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://tomcat;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

redis安装请看教程

各个组件下载

原文地址:https://www.cnblogs.com/sellsa/p/8257490.html