nginx+tomcat+redis realizes session sharing (redisson form)

Realize nginx+tomcat+redis realize session sharing

Environmental preparation

(1) Virtual machine centos7 (virtual ip: 192.168.168.130)
(2) redis
(3) java8
(4) tomcat8
(5) nginx
First we need to install the java environment, there are many online resources, you can search by yourself

(1) Download tomcat

Here is tomcat8. After downloading it, upload it to our virtual environment and unzip it in a folder. My folder directory is: /opt/tomcat/apache-tomcat-8.0.53
Unzip command:

tar xcf  /opt/tomcat/apache-tomcat-8.0.53

Start tomcat, we can see that the startup is successful and the visit
Insert picture description here
is successful. This is the second step we prepared. After the successful visit, we start to install redis and connect with it

(2) Download redis-4.0.11.tar.gz (the version may be different)

Upload it to our virtual environment, my path is: /opt/tomcat, execute the decompression command

tar xcf redis-4.0.11.tar.gz

Enter the decompression directory redis-4.0.11, execute the makecommand, then proceed make install, then enter the src directory: start redis, you can see the successful startup.
[root@localhost src]# ./redis-server
Insert picture description here

(3) Interconnection of tomcat and redis

(1) Download two jar packages, after the download is complete, put them into the lib directory of tomcat.
redisson-all-3.8.1.jar
redisson-tomcat-7-3.8.1.jar
Insert picture description here
(2) Create the json file mySession.json in the tomcat conf folder (the file name can be arbitrary, but it must be the same as the one below The anaphora, I will talk about it below) The
json content is:

{
   "singleServerConfig":{
      "idleConnectionTimeout":10000,
      "pingTimeout":1000,
      "connectTimeout":10000,
      "timeout":3000,
      "retryAttempts":3,
      "retryInterval":1500,
      "reconnectionTimeout":3000,
      "failedAttempts":3,
      "subscriptionsPerConnection":5,
      "clientName":null,
      "address": "redis://127.0.0.1:6379",
      "subscriptionConnectionMinimumIdleSize":1,
      "subscriptionConnectionPoolSize":50,
      "connectionMinimumIdleSize":32,
      "connectionPoolSize":64,
      "database":0
   },
   "threads":0,
   "nettyThreads":0,
   "codec":{
      "class":"org.redisson.codec.JsonJacksonCodec"
   },
   "transportMode":"NIO"
}

(3) After this step is completed, we need to configure and read this configuration file in context.xml, so:

vim /opt/tomcat/apache-tomcat-8.0.53/conf/context.xml

Add content under the context note: (Note: the file name must be written as the file name we just created)

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

(4)
After these preparations for starting tomcat are completed, we start to start tomcat,

cd /opt/tomcat/apache-tomcat-8.0.53/bin
./start.sh

After the startup is complete, we visit, we can see that the same as the previous small fruit, successfully accessed, we go to the redis client to query and find that the query session is empty, so we need to simulate a method to obtain the session.
Insert picture description here

vim /opt/tomcat/apache-tomcat-8.0.53/webapps/test/index.jsp

The edit content is:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort()
                        + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>tomcatB</title>
        </head>

        <body>
        <center><h1>tomcatB</h1></center>
        <center>
                <h3>sessionId:</h3><%=session.getId()%>
                <h3>session创建时间:</h3><%=session.getCreationTime()%>
        <center>
        </body>
</html>

After saving, visit again:

192.168.168.130:8080/test/index.jsp

Insert picture description here
Then go to query the redis key value situation:
Insert picture description here
at this time, the association is complete

(4) Install another tomcat, just copy the tomcat we are running now

(Of course, if it is two machines, it is also possible)

cp  -r  /opt/tomcat/apache-tomcat-8.0.53  /opt/tomcat/tomcat8090

Then modify the port:

vim /opt/tomcat/tomcat8090/

The changes are as follows:
(1) Change from 8009 to 8010

  <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

(2) Change from 8080 to 8090 before

 <Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

(3) Change from 8005 to 8006

<Server port="8006" shutdown="SHUTDOWN">

Modify the index.jsp page to distinguish it from the previous one

vim /opt/tomcat/tomcat8090/webapps/test/index.jsp

Modify tomcat to tomcat8090

 <center><h1>tomcatA8090</h1></center>
 <center>
         <h3>sessionId:</h3><%=session.getId()%>
         <h3>session创建时间:</h3><%=session.getCreationTime()%>
 <center>

Start the start visit:
visit the first one:

192.168.168.130:8080/test/index.jsp

Insert picture description here
Access the second tomcat

192.168.130:8090/test/index.jsp

Insert picture description here
It can be seen that we are accessing different tomcats but the sessionId is the same, which means that we have configured the sharing of sessions between tomcats through redis.

(5) Install nginx and realize the last step

We can download and install nginx by searching on the Internet.
Download nginx, unzip, after unzip, enter the unzip directory, execute make && make install, and then the message will prompt us that nginx has been installed in the /usr/local/nginx folder:
Edit the configuration file:

 upstream tomcat {
     server 192.168.168.130:8080 weight=1;
     server 192.168.168.130:8090 weight=1;
    }
    server{
         `````````
	     location / {
	           # root   html;
	           # index  index.html index.htm;
	           proxy_pass http://tomcat;
	        }
	        `````````
	   }

Then start the service After the
Insert picture description here
start is successful, we visit through the browser:

192.168.168.130/test/index.jsp

Insert picture description here
Page refresh
Insert picture description here
You can see that we have successfully accessed different pages, but the sessionID is the same, which shows that we have successfully realized session sharing through nginx+tomcat+redis.
It is not recommended to use it in windows, because there will be various strange problems. Of course, it is best if the owner does not appear to ask! ! !
If you have any questions, please leave a message, let us learn together and make progress together.

Guess you like

Origin blog.csdn.net/qiuqiu1628480502/article/details/82945034