nginx1.20-tomcat9-redisson集群,好像不怎么完善

发布时间 2023-08-02 18:39:11作者: 骑着母猪去打猎

配置信息

在同一台服务器上使用nginx做反向代理与两个tomcat组成简易tomcat集群

使用

nginx端口80

tomcat1端口21005,21080,21009

tomcat2端口22005,22080,22009

配置过程

1. 先下载apache-tomcat-9.0.78,解压到两个目录,分别为tomcat1和tomcat2

根据上方配置信息分别对两个目录中的/conf/server.xml进行修改

   <Resource name="bean/redisson"
       auth="Container"
       factory="org.redisson.JndiRedissonFactory"
       configPath="${catalina.base}/conf/redisson.yaml"
       closeMethod="shutdown"/>

 

 配置session共享部分,修改/conf/context.xml

<Context>
    <ResourceLink name="bean/redisson"
       global="bean/redisson"
       type="org.redisson.api.RedissonClient" />

    <Manager className="org.redisson.tomcat.JndiRedissonSessionManager"
       readMode="REDIS"
       jndiName="bean/redisson" />
</Context>

在/conf/目录中添加 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://127.0.0.1:6379"
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  connectionMinimumIdleSize: 24
  connectionPoolSize: 64
  database: 0
  dnsMonitoringInterval: 5000
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.MarshallingCodec> {}
transportMode: "NIO"

下载redisson-tomcat-9-3.23.2.jar, redisson-all-3.23.2.jar至/tomcat/lib内

 

2. 部署nginx,修改/conf/nginx.conf配置文件

 3. 安装配置redis,下载后启动

 4. 部署测试页面

/tomcat1/webapps/ROOT/index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta charset="UTF-8" />
<%
request.getSession().setAttribute("ss1", "ss111");
%>
    </head>
    <body>
        <table align="center" border="1">
            <tr>
              <td>cluster-desc1</td>
              <td>This is the first tomcat</td>
            </tr>
            <tr>
              <td>SessionID</td>
              <td><%= request.getSession().getId() %></td>
            </tr>
            <tr>
              <td>SessionCreatedTime</td>
              <td><%= request.getSession().getCreationTime() %></td>
           </tr>
           <tr>
              <td>ServerName</td>
              <td><%=request.getServerName()%></td>
           </tr>
           <tr>
              <td>SessionPort</td>
              <td><%=request.getServerPort()%></td>
           </tr>
           <tr>
              <td>ss1</td>
              <td><%=request.getSession().getAttribute("ss1") %></td>
           </tr>
           <tr>
              <td>ss2</td>
              <td><%=request.getSession().getAttribute("ss2") %></td>
           </tr>
        </table>
    </body>
</html>

/tomcat2/webapps/ROOT/index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta charset="UTF-8" />
<%
request.getSession().setAttribute("ss2", "ss222");
%>
    </head>
    <body>
        <table align="center" border="1">
            <tr>
              <td>cluster-desc2</td>
              <td>This is the first tomcat</td>
            </tr>
            <tr>
              <td>SessionID</td>
              <td><%= request.getSession().getId() %></td>
            </tr>
            <tr>
              <td>SessionCreatedTime</td>
              <td><%= request.getSession().getCreationTime() %></td>
           </tr>
           <tr>
              <td>ServerName</td>
              <td><%=request.getServerName()%></td>
           </tr>
           <tr>
              <td>SessionPort</td>
              <td><%=request.getServerPort()%></td>
           </tr>
           <tr>
              <td>ss1</td>
              <td><%=request.getSession().getAttribute("ss1") %></td>
           </tr>
           <tr>
              <td>ss2</td>
              <td><%=request.getSession().getAttribute("ss2") %></td>
           </tr>
        </table>
    </body>
</html>

5.启动redisson,tomcat1,tomcat2,nginx后访问http://127.0.0.1:21080/index.jsp,http://127.0.0.1:22080/index.js,phttp://127.0.0.1:80/index.jsp能够访问到相应内容,sessionId相同

redisson /redis-server.exe

tomcat /bin/startup.bat

nginx /start nginx

 

6.程序下载

https://repo1.maven.org/maven2/org/redisson/

redisson-all-3.23.2.jar

Tomcat 7.x - redisson-tomcat-7-3.23.2.jar

Tomcat 8.x - redisson-tomcat-8-3.23.2.jar

Tomcat 9.x - redisson-tomcat-9-3.23.2.jar

Tomcat 10.x - redisson-tomcat-10-3.23.2.jar

Try Redisson PRO with ultra-fast performance and support by SLA.