使用redisson-tomcat配置session共享

1.项目地址

https://github.com/redisson/redisson/tree/master/redisson-tomcat

2.主机规划

10.0.0.43 nginx
10.0.0.44 tomcat1
10.0.0.45 tomcat2
10.0.0.48 redis

3.tomcat的配置
3.1在全局context.xml或者server.xml中添加session manager

  • keyPrefix-应用于所有Redis键的字符串前缀。允许将不同的Tomcat环境连接到同一Redis实例。

  • readMode-读取会话属性模式。有两种模式可用:

    • MEMORY-将属性存储到本地Tomcat Session和Redis中。进一步的会话更新使用基于Redis的事件传播到本地-Tomcat会话。
    • REDIS-仅将属性存储到Redis中。默认模式。
  • broadcastSessionEvents-如果true再sessionCreated和sessionDestroyed事件会在所有Tomcat实例广播,并导致触发所有注册HttpSessionListeners。默认值为false。

  • updateMode-会话属性更新模式。有两种模式可用:

    • DEFAULT-会话属性仅通过Session.setAttribute方法存储到Redis中。默认模式。
    • AFTER_REQUEST-每次请求后,所有会话属性都存储到Redis中。当某些存储在会话中的对象更改了自己的状态而没有Session.setAttribute执行方法时,这很有用。
  • configPath-Redisson YAML配置的路径。更多配置请看:https://github.com/redisson/redisson/wiki/2.-Configuration

    • 单实例配置文件:
---
singleServerConfig:
  idleConnectionTimeout: 10000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  address: "redis://10.0.0.48:6379"
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  connectionMinimumIdleSize: 24
  connectionPoolSize: 64
  database: 0
  dnsMonitoringInterval: 5000
threads: 16
nettyThreads: 32
codec: ! {}
transportMode: "NIO"
  • 集群配置:
---
clusterServersConfig:
  idleConnectionTimeout: 10000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  failedSlaveReconnectionInterval: 3000
  failedSlaveCheckInterval: 60000
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  loadBalancer: ! {}
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  slaveConnectionMinimumIdleSize: 24
  slaveConnectionPoolSize: 64
  masterConnectionMinimumIdleSize: 24
  masterConnectionPoolSize: 64
  readMode: "SLAVE"
  subscriptionMode: "SLAVE"
  nodeAddresses:
  - "redis://127.0.0.1:7004"
  - "redis://127.0.0.1:7001"
  - "redis://127.0.0.1:7000"
  scanInterval: 1000
  pingConnectionInterval: 30000
  keepAlive: false
  tcpNoDelay: false
threads: 16
nettyThreads: 32
codec: ! {}
transportMode: "NIO"
3.2在tomcat1的webapps/ROOT目录下添加session.jsp

 
test1 
 
 
SessionID is <%=session.getId()%> 
SessionIP is <%=request.getServerName()%>
SessionPort is <%=request.getServerPort()%> <% out.println("Response from tomcat1"); %>
3.3在tomcat2的webapps/ROOT目录下添加session.jsp
 
 
test2 
 
 
SessionID is <%=session.getId()%> 
SessionIP is <%=request.getServerName()%>
SessionPort is <%=request.getServerPort()%> <% out.println("Response from tomcat2"); %>
3.4重启两个tomcat

catalina.sh stop
catalina.sh start

3.5使用nginx代理
cat /etc/nginx/conf.d/test.conf

upstream tomcat_endpoint {
  server 10.0.0.44:8080;
  server 10.0.0.45:8080;
}

server {
  listen 80;
  server_name node1.magedu.com;
  location / {
    proxy_pass http://tomcat_endpoint;
    include proxy_params;
  }
}

3.6检查nginx语法并重启
nginx -t
nginx -s reload
3.7在Windows hosts文件添加本地解析
10.0.0.43 node1.magedu.com
3.8测试访问

可以看到两个tomcat最终都取到了相同的session

  • 代理到tomcat1上面的session


    image.png
  • 代理到tomcat2上面的session


    image.png

你可能感兴趣的:(使用redisson-tomcat配置session共享)