1. 程式人生 > >nginx+tomcat9+redisson+redis+jdk1.8簡單實現session共享

nginx+tomcat9+redisson+redis+jdk1.8簡單實現session共享

一、環境安裝

由於資源限制,在虛擬機器中模擬測試,一臺虛擬機器,所有軟體均安裝到該虛擬機器內

安裝系統:CentOS Linux release 7.4.1708 (Core)

CentOS安裝選擇版本:Basic Web Server

redis版本:5.0.2

jdk版本:1.8.0_191

nginx版本:1.14.1

tomcat版本:9.0.13

1、安裝gcc

yum -y installed gcc

2、安裝redis

2.1、解壓

tar -zxvf redis-5.0.2.tar.gz -C /usr/local/

2.2、進入redis安裝目錄

cd /usr/local/redis-5.0.2

2.3、安裝

make && make install

2.4、啟動redis

/usr/local/redis-5.0.2/src/redis-server --protected-mode no &

3、安裝jdk

3.1、查詢已安裝jdk

rpm -qa | grep jdk | grep -v grep

3.2、若為openjdk或者非1.8版本,則解除安裝

rpm -e --nodeps “具體安裝jdk”

3.3、安裝

rpm -ivh jdk-8u191-linux-x64.rpm

3.4、設定環境變數

/etc/profile中新增如下配置

export JAVA_HOME=/usr/java/jdk1.8.0_191-amd64/
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin/

3.5、使配置生效

source /etc/profile

4、安裝tocmat

4.1、新建兩個tocmat

tar -zxvf apache-tomcat-9.0.13.tar.gz -C /usr/local/
cd /usr/local/
mv
apache-tomcat-9.0.13 tocmat1 tar -zxvf apache-tomcat-9.0.13.tar.gz -C /usr/local/ cd /usr/local/ mv apache-tomcat-9.0.13 tocmat2

4.2、配置tomcat埠

tomcat1 Connector port改為8081
tomcat2  server port改為8006,Connector port改為8082,AJP Connector port改為8010

如果有需要,可在tomcat/bin目錄下新建setenv.sh檔案,來設定記憶體大小等引數,也可在catalina.sh中直接設定,本例屬於測試,未進行該設定

CATALINA_PID="$CATALINA_BASE/tomcat.pid"
JAVA_OPTS="-server -Xms1000m -Xmx1000m -XX:PermSize=128M -XX:MaxPermSize=128M"
export JAVA_OPTS
export JAVA_HOME=$JAVA_HOME

5、安裝nginx

5.1、安裝依賴包

yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

5.2、解壓

tar -zxvf nginx-1.14.1.tar.gz

5.3、進入安裝目錄

cd nginx-1.14.1

5.4、測試安裝環境,預設將安裝在/usr/local/nginx下

./configure

若configure未通過,則需要處理出現的異常,否則直接make && make install也是會出錯

5.5、編譯安裝

make && install

5.6、檢查是否安裝成功

http://192.168.3.12

若啟動正常,頁面不能訪問,則檢查是否防火牆未關閉或者80埠未允許訪問,關閉防火牆方法

systemctl stop firewalld

 

二、session共享和負載均衡配置

1、tocmat配置

1.1、在tomcat/conf/context.xml中增加RedissonSessionManager,tomcat1和tomcat2都要配置

<Manager className="org.redisson.tomcat.RedissonSessionManager"
configPath="${catalina.base}/redisson.conf" readMode="MEMORY" updateMode="DEFAULT"/>

具體引數可參考:

https://github.com/redisson/redisson/blob/dfcf183fb99e2718a748148942926256f650ee24/redisson-tomcat/README.md

配置redisson

1.2、在tomcat安裝目錄下新建redisson.conf,新增如下配置

{
   "singleServerConfig":{
      "idleConnectionTimeout":10000,
      "pingTimeout":1000,
      "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":32,
      "connectionPoolSize":64,
      "database":0,
      "dnsMonitoringInterval":5000
   },
   "threads":0,
   "nettyThreads":0,
   "codec":{
      "class":"org.redisson.codec.JsonJacksonCodec"
   },
   "transportMode":"NIO"
}

本例中使用的為單例項redis,具體引數配置可參考:
https://github.com/redisson/redisson/wiki/2.-Configuration#26-single-instance-mode
redis叢集可參考如下連結中其他配置
https://github.com/redisson/redisson/wiki/2.-Configuration

1.3、拷貝jar包

https://github.com/redisson/redisson/tree/dfcf183fb99e2718a748148942926256f650ee24/redisson-tomcat下載redisson-all-3.9.1.jar和redisson-tomcat-9-3.9.1.jar,拷貝到${catalina.base}/lib下

分別啟動tomcat1和tomcat2,驗證是否可正常訪問

http://192.168.3.12:8081/mytest/index.jsp
http://192.168.3.12:8082/mytest/index.jsp

1.4、nginx配置

修改nginx.conf配置

http節點中新增如下配置

upstream backend {
        server 127.0.0.1:8081 max_fails=1 fail_timeout=10s;
        server 127.0.0.1:8082 max_fails=1 fail_timeout=10s;
}

location節點中增加proxy_pass項

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend;
        }

 

三、測試

1、tomcat1/webapps/新建目錄mytest,mytest目錄下新建index.jsp,內容如下

<%@ page language="java" %>
<html>
  <head><title>tomcat1</title></head>
  <body>
    <table align="centre" border="1">
      <tr>
        <td>SessionID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>SessionCreatedTime</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
     <tr>
        <td>ServerName</td>
        <td><%=request.getServerName()%></td>
     </tr>
     <tr>
        <td>SessionPort</td>
        <td><%=request.getServerPort()%></td>
     </tr>
      <tr>
        <td>CustomString</td>
        <td>This is the first tomcat</td>
     </tr>
    </table>
  </body>
</html>

2、tomcat2/webapps/新建目錄mytest,mytest目錄下新建index.jsp,內容如下

<%@ page language="java" %>
<html>
  <head><title>tomcat2</title></head>
  <body>
    <table align="centre" border="1">
      <tr>
        <td>SessionID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>SessionCreatedTime</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
     <tr>
        <td>ServerName</td>
        <td><%=request.getServerName()%></td>
     </tr>
     <tr>
        <td>SessionPort</td>
        <td><%=request.getServerPort()%></td>
     </tr>
      <tr>
        <td>CustomString</td>
        <td>This is the second tomcat</td>
     </tr>
    </table>
  </body>
</html>

 

3、重新啟動redis,tomcat,nginx

4、輸入地址訪問

http://192.168.3.12/mytest/

5、結果

點選重新整理按鈕

 

可以發現,兩次訪問,SessionID都是相同的,並且title和自定義的字串不一樣,說明分別訪問了tomcat1和tomcat2

 登陸redis

redis-cli

輸入以下命令可發現,session已經存放到了redis中,並且預設的超時時間為30 min