#IT star is not a dream

1, Common session consistency schemes

In the Web cluster system, in order to make the web adapt to large-scale access, it is necessary to realize the application cluster deployment. To realize the cluster deployment, the first thing is to solve the session unification, that is, to realize the session consistency. At present, there are two schemes to realize the session consistency, which are session replication and session sharing.

Session replication means that session information will be replicated between cluster nodes, and each node server will have the same session information. The advantage of this scheme is that even if a node server goes down, as long as there is still a server alive, session information will not be lost, and users will not be affected. The disadvantage is that the communication between cluster nodes is frequent, which has an impact on the response speed of the application. In the case of high concurrency and high frequency operation, the performance will decline even more.

The second scheme is session sharing, that is, the session information is saved in the memory database, and then multiple nodes can access the session information in memory, the most common one is session sharing based on Memcache/Redis and other databases.

In the cluster of tomcat, session replication function is provided. Session information will be synchronized in each Tomcat. Session replication requires high network requirements. Session memory consumption has a great impact on Tomcat. For small clusters, this session replication is basically enough. For large clusters, it is recommended to use redis or memcache for session sharing.

Therefore, it is recommended to use tomcat session sharing mechanism based on redis when building tomcat cluster.
When building a cluster through nginx, there will also be session problems. According to the different nginx load balancing algorithms, the implementation mechanism of the session is also different. For example, when polling (default), specifying the weight, fair (third-party), URL (third-party) load algorithm, each node of the cluster must be realized through session sharing. When using IP hash algorithm with IP binding, users' requests will be bound to the same node server, so session problem is not considered. However, after the node server goes down, the user needs to turn off the browser and open the login again to restore normal, so the experience will become very poor. Therefore, session sharing is very useful in cluster architecture.

2, Introduction to application environment

1. session utility

Tomcat cluster is widely used in enterprise applications, so session sharing must be solved. This paper mainly introduces a simple session sharing scheme: redisson tool.

Redisson is a java language implementation of distributed lock project recommended by redis official website. Of course, redisson goes far beyond distributed locks to include other distributed structures. For example, distributed application, distributed cache, distributed call back management, distributed service (task, delay task, executor), distributed redis client, etc. What we are going to use here is the Tomcat Session Manager function provided by redisson. It supports the latest JDK and various versions of tomcat (6 / 7 / 8 / 9). The address of the official website of redisson is https://redisson.org/.
The github address shared by Tomcat session is:
https://github.com/redisson/redisson/tree/master/redisson-tomcat
Readers can choose to download different redisson according to their different tomcat versions.

2. Environmental preparation

First, the application environment of this case is described as follows:

Operating system IP address installation software purpose
 Centos7.6 172.16.213.37 nginx1.14 load balancing server
 Centos7.6 172.16.213.120 redis5, tomcat9.0 tomcat, redis server
 Centos7.6 172.16.213.106 tomcat9.0 Tomcat server

This is a web architecture composed of nginx and tomcat. The two tomcat clusters realize load balancing through nginx. In this cluster, we use redis to realize session sharing. Next, let's see how to realize session sharing in tomcat cluster architecture.

3, nginx+tomcat+redis deployment process

The deployment process of this web architecture is very simple, mainly divided into three steps, which are described in turn below.

1. redis installation and deployment

The installation and deployment of redis is relatively simple. First, download the latest version of redis from https://redis.io. Here, download the version of redis5.x, and then compile it. The process is as follows:

[root@localhost ~]#wget http://download.redis.io/releases/redis-5.0.3.tar.gz
[root@localhost ~]#tar zxvf redis-5.0.3.tar.gz
[root@localhost ~]#cd redis-5.0.3
[root@localhost redis-5.0.3]#make
[root@localhost redis-5.0.3]#make install
[root@localhost redis-5.0.3]#cp redis.conf  /etc 
[root@localhost redis-5.0.3]#cd src
[root@localhost src]#cp redis-cli  redis-server  redis-sentinel   /usr/sbin/
The above installation process has been described in detail in the previous chapter. There will be no more instructions here.

2. Configure redis and start

There is no need to configure too much for redis. Open the redis configuration file / etc/redis.conf, and modify the values of the two configuration items as follows:

bind 0.0.0.0
daemonize yes

Finally, start the redis service:

[root@localhost ~]#/usr/sbin/redis-server  /etc/redis.conf
In this way, the redis service is installed and started. The default starting port of redis is 6379.

3. tomcat installation and deployment

Here, we first deploy Tomcat on 172.16.213.120. Before deploying tomcat, we need to install JDK. Here, we choose JDK 1.8, Download linux-64 version of JDK from oracle official website. When downloading, we choose the version suitable for the running environment of our machine. The JDK provided by oracle official website is binary version. Therefore, the installation of JDK is very simple, just download the package Extract it to the corresponding directory. The installation process is as follows:

[root@localhost ~]# mkdir /usr/java
[root@localhost ~]# tar -zxvf jdk-8u181-linux-x64.tar.gz -C /usr/java/

Here we install the JDK in the / usr/java / directory. Then, in order for the program to recognize the JDK path, you need to set the environment variable of JDK. Here we set the JDK environment variable to the / etc/profile file. Add the following to the end of the / etc/profile file:

export JAVA_HOME=/usr/java/jdk1.8.0_181
export PATH=$PATH:$JAVA_HOME/bin
exportCLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH

Then execute the following command for the settings to take effect:

[root@localhost ~]# source /etc/profile

Finally, execute the "java version" command in the Shell prompt. If the following results are displayed, the installation is successful:

[root@localhost ~]#  java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

After the JDK is deployed successfully, Tomcat can be deployed. The installation of Tomcat is very simple. Download the latest Tomcat from http://tomcat.apache.org/ and extract it. Download the Tomcat 9 version here and install it in the directory / usr/loca /. Perform the following operations:

[root@localhost ~]# tar zxvf  apache-tomcat-9.0.16.tar.gz -C  /usr/local
[root@localhost ~]# mv /usr/local/apache-tomcat-9.0.16 /usr/local/tomcat9

In this way, tomcat is also installed. Because the integration of tomcat and redis needs to be configured, the tomcat service will not be started for the time being.

4. tomcat and redis integrate to realize session sharing

After tomcat installation, the next step is to configure tomcat. To configure tomcat and redis integration and realize session management, the following steps are required.

(1) , deploy jar package
First, from https://github.com/redisson/redisson/tree/master/redisson-tomcat Download two jar packages. According to the Tomcat version, download the corresponding redisson Tomcat jar package. Here we use the Tomcat version 9, so the downloaded redisson Tomcat is redisson-tomcat-9-3.11.5.jar. You also need to download a jar package that is redisson-all-3.11.5.jar. The versions of these two jar packages will be upgraded continuously. The specific version number may not be completely the same as this article The same, but does not affect our use. Put the downloaded jar package in the $Tomcat? Base / lib directory. Our path here is / usr/local/tomcat9/lib.

(2) . add RedissonSessionManager configuration
This step is to add the following configuration in the tomcat configuration file $tomcat? Base / conf / context.xml:

 <Manager className="org.redisson.tomcat.RedissonSessionManager"
configPath="${catalina.base}/conf/redisson.json" readMode="REDIS" updateMode="DEFAULT"/>
//The contents of {catalina.base}/conf/redisson.json file are as follows:
{
   "singleServerConfig":{
      "idleConnectionTimeout":10000,
      "connectTimeout":10000,
      "timeout":3000,
      "retryAttempts":3,
      "retryInterval":1500,
      "password":null,
      "subscriptionsPerConnection":5,
      "clientName":null,
      "address": "redis://172.16.213.120:6379",
      "subscriptionConnectionMinimumIdleSize":1,
      "subscriptionConnectionPoolSize":50,
      "connectionMinimumIdleSize":32,
      "connectionPoolSize":64,
      "database":0,
      "dnsMonitoringInterval":5000
   },
   "threads":0,
   "nettyThreads":0,
   "codec":{
      "class":"org.redisson.codec.FstCodec"
   },
   "transportMode":"NIO"
}

This json file is mainly used to configure the attribute information of the connection between tomcat and redis. Note the value of "address" in the json file. If tomcat and redis are installed together, it can be written as 127.0.0.1:6379. If redis is on an independent machine, it can be written as the IP address of the machine where redis is located. Here, the IP address of redis is 172.16.213.120, so just write this address on it.

So far, the interconnection configuration of tomcat and redis is completed.

5. Add the jsp file to get the session

Create a testsession.jsp file under the corresponding directory $tomcat? Base / webapps / root on tomcat server 172.16.213.120. The content is as follows:

<%@ 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>tomcat1</title>
        </head>

        <body>
        <center><h1>tomcat1</h1></center>
        <center>
                <h3>sessionId:</h3><%=session.getId()%>
                <h3>session Created:</h3><%=session.getCreationTime()%>
        <center>
        </body>
</html>

Next, start the tomcat service and execute the following command:

[root@localhost ~]#/usr/local/tomcat9/bin/startup.sh

After starting tomcat, access: http://172.16.213.120:8080/testsession.jsp , as shown in the following figure:

Then, log in to the redis client and view the key information. The operation is as follows:

[root@localhost tomcat9]# redis-cli  -h 172.16.213.120 -p 6379
172.16.213.120:6379> keys *
1) "redisson:tomcat_session:56BC7C926C3673F8F7CFEC52442B10EA"
172.16.213.120:6379>

As you can see, session information has been generated in redis. The session information in redis is exactly the same as that obtained by testsession.jsp.

6. Deploy the second tomcat instance

The first Tomcat instance tomcat1 has been deployed above, and then another Tomcat instance tomcat2 is deployed on 172.16.213.106. The simplest deployment method is to directly copy the first tomcat1 instance to the corresponding location on 172.16.213.106.

In order to distinguish two tomcat instances, some identification information in testsession.jsp file needs to be modified. The modified content is as follows:

<%@ 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>tomcat2</title>
        </head>

        <body>
        <center><h1>tomcat2</h1></center>
        <center>
                <h3>sessionId:</h3><%=session.getId()%>
                <h3>session Created:</h3><%=session.getCreationTime()%>
        <center>
        </body>
</html>

This is mainly to modify tomcat1 in testsession.jsp file to tomcat2. Finally, you need to install and configure JDK on 172.16.213.106, which has been described above and will not be explained here.

After all modifications are completed, start tomcat2 service, and then visit http://172.16.213.106:8080/testsession.jsp, as shown in the following figure:

As you can see, the session information obtained by visiting the testsession.jsp page through 172.16.213.120 and 172.16.213.106 is different. This is normal. Because this is a different server, the session is certainly different.

7. Deploying nginx load balancing

According to the above definition, nginx is installed on the 172.16.213.37 server. Download the latest nginx version from the nginx official website http://nginx.org/. Here is the nginx-1.14 version. Then compile and install it. The process is as follows:

[root@centos ~]# yum -y install zlib pcre pcre-devel openssl openssl-devel
[root@centos ~]# useradd -s /sbin/nologin www
[root@centos ~]#tar zxvf nginx-1.14.1.tar.gz
[root@centos ~]#cd nginx-1.14.1 
[root@centos nginx-1.14.1]# ./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
[root@centos nginx-1.14.1]# make
[root@centos nginx-1.14.1]# make install

In this way, nginx is installed. Next, configure nginx. Open the nginx configuration file nginx.conf, and focus on the following parts:

user  www;
worker_processes  8;
events {
    worker_connections  65536;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
   upstream tomcat {
     server 172.16.213.120:8080 weight=1;
     server 172.16.213.106: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;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
}

In this nginx configuration, the load balancing function of nginx is enabled. The back-end nodes of the cluster are two tomcat servers. After the configuration, start the nginx service. The operation is as follows:

[root@centos nginx-1.14.1]# /usr/local/nginx/sbin/nginx 

4, Test session sharing function of nginx+tomcat+redis

After nginx configuration, you can access two tomcat instances through http://172.16.213.37/testsession.jsp, as shown in the following figure:

If you can try to refresh this page constantly, the page will poll and switch between two tomcat instances, but the session value does not change, which means that both tomcat instances share the session information stored in redis, that is, the session sharing function of nginx+tomcat+redis is realized.

Tags: Linux Tomcat Redis Session Nginx

Posted on Mon, 10 Feb 2020 07:25:47 -0500 by ravi.kinjarapu