Implementation of Nginx+Tomcat on Session management

  • 2020-12-05 17:28:39
  • OfStack

preface

Nginx+Tomcat has some knowledge of Session management, but it has not been operated for 1 time. This article starts with the simplest installation and startup, and introduces several ways to manage session step by step through examples.

nginx installation configuration

1. Install nginx


[root@localhost ~]# yum install nginx

The prompt reports the following error:

[

No package nginx available.

]

Solution installation epel: EPEL is short for Enterprise Linux Add-on package. EPEL is a high-quality add-on package project created, maintained, and managed by the Fedora Special Interest Group for Red Hat Enterprise Linux(RHEL) and its derived distributions (e.g. CentOS, Scientific Linux, Oracle Enterprise Linux).


[root@localhost ~]# yum install epel-release

Once installed, nginx can be successfully installed;

2. Start and stop nginx

Go to the nginx directory first


[root@localhost nginx]# cd /usr/sbin/

Execute the command


./nginx  open 
./nginx -s stop  use kill The command forces the process to be killed 
./nginx -s quit  stay nginx The process stops when it has finished processing 
./nginx -s reload

nginx+tomcat load balancing

1. Prepare 2 tomcat and specify port 8081,8082 respectively


drwxr-xr-x. 9 root root   4096 May 7 14:16 apache-tomcat-7.0.88_8081
drwxr-xr-x. 9 root root   4096 May 7 14:16 apache-tomcat-7.0.88_8082

Modify webapps/ROOT's ES69en.ES70en for easy testing


<%
if(request.getSession().getAttribute("key")==null){
  out.println("key is null,ready init.....");  
  request.getSession().setAttribute("key","value");
}else{
  out.println("key is not null,key="+request.getSession().getAttribute("key")); 
}
%>
<br> 
sessionID:<%=session.getId()%>  
<br>  
sessionCreateTime:<%= session.getCreationTime() %>
<br>
<% 
out.println("tomcat port 8081");  
%> 

The final output specifies the port numbers 8081 and 8082 under the two tomcat

2.nginx configuration Load Balancing (default policy)

Modify nginx.conf below /etc/nginx


upstream tomcatTest {
   server 127.0.0.1:8081;  #tomcat-8081
   server 127.0.0.1:8082;  #tomcat-8082
}
 
server {
  listen    80 default_server;
  listen    [::]:80 default_server;
  server_name _;
  root     /usr/share/nginx/html;
 
  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;
 
  location / {
    proxy_pass http://tomcatTest;
  }
 
  error_page 404 /404.html;
    location = /40x.html {
  }
 
  error_page 500 502 503 504 /50x.html;
    location = /50x.html {
  }
}

The load balancing policy configured here is the default polling policy. nginx also supports other policies including: ip_hash, weight, fair(3rd party), url_hash(3rd party);

The default policy is that each web request is assigned to a different backend server one by one in chronological order, in which case a new session is created for each request. Here's a simple test:

1st request http://ip/


key is null,ready init..... 
sessionID:E7A9782DED29FF04E21DF94078CB4F62 
sessionCreateTime:1527732911441
tomcat port 8082

Second refresh http://ip/


key is null,ready init..... 
sessionID:7812E8E21DBB74CC7FBB75A0DFF2E9CB 
sessionCreateTime:1527732979810
tomcat port 8081

Refresh http for the third time ://ip/


key is null,ready init..... 
sessionID:8895F41E299785A21995D5F8BB734B86 
sessionCreateTime:1527733011878
tomcat port 8082

It can be found that 1 new session is generated each time, and the messages are assigned to different backend servers one by one in chronological order. 1 Generally, websites that need to maintain session sessions are not allowed to generate 1 session per request.

3.nginx configuration Load balancing (viscous Session)

Each request is allocated according to the hash result of access to ip, so that each visitor has a fixed access to a backend server, which can solve the problem of session. nginx can achieve viscous Session by configuring ip_hash in upstream module.


[root@localhost ~]# yum install epel-release
0

Here's a simple test:

First request http://ip/


key is null,ready init..... 
sessionID:859BADFB09A4ECEAEC5257F518C228A0 
sessionCreateTime:1527734181450
tomcat port 8081

Second refresh http://ip/


key is not null,key=value 
sessionID:859BADFB09A4ECEAEC5257F518C228A0 
sessionCreateTime:1527734181450
tomcat port 8081

Third refresh http://ip/


[root@localhost ~]# yum install epel-release
3

It can be found that key=value was set in the first request, and the value of key was obtained every time after that. sessionId and tomcat did not change, so the viscous Session was realized.

At this point, you can stop port=8081 and observe again

Refresh http for the 4th time ://ip/


[root@localhost ~]# yum install epel-release
4

5th refresh http://ip/


[root@localhost ~]# yum install epel-release
5

It can be found that the message was forwarded to tomcat-8082 and session was lost, so a new session was created.
There are also two ways to make session not lost in this situation: Session replication and Session sharing; Session sharing is much better in terms of scalability, performance,

The following highlights how to implement Session sharing under 1;

nginx+tomcat realize Session sharing

The idea of Session sharing is to save session in a public place and then take it out when using it. The specific public place can be: redis, db, memcached, etc. redis is taken as an example below

1.redis installation configuration


[root@localhost ~]# yum install epel-release
6

Configuration file /etc/ redis.conf after installation

Start the redis server


redis-server /etc/redis.conf

Start the client


redis-cli

2.Tomcat introduces dependent jar

$TOMCAT_HOME/lib Add the following jar package


[root@localhost ~]# yum install epel-release
9

3.Tomcat modifies configuration

Modify the context.xml file in the $TOMCAT_HOME/conf directory


<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
     host="localhost"
     port="6379"
     database="0"
     maxInactiveInterval="60"/>

Tomcat provides an open session management and persistent org apache. catalina. session. ManagerBase, inherit this abstract class 1 and do some simple configuration, can let your session management class take over Tomcat session read and persistence, used here is tomcat - redis session - manager to manage session;

RedisSessionManager inheritance in org apache. catalina. session. ManagerBase class, the related operations of session in such;

4. Test

1st request for http://ip/


key is null,ready init..... 
sessionID:1131499E5A65DE1591152465E7B24B1F 
sessionCreateTime:1527740273682
tomcat port 8081

Second refresh http://ip/


key is not null,key=value 
sessionID:1131499E5A65DE1591152465E7B24B1F 
sessionCreateTime:1527740273682
tomcat port 8081

Stop tomcat-8081 and refresh http://ip/ for the third time


key is not null,key=value 
sessionID:1131499E5A65DE1591152465E7B24B1F 
sessionCreateTime:1527740273682
tomcat port 8082

It can be found that at this time, the message has been forwarded to tomcat-8082 node, but session has not changed, and key can also get the value.

5. Check the redis


[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
1) "1131499E5A65DE1591152465E7B24B1F"
127.0.0.1:6379> get 1131499E5A65DE1591152465E7B24B1F
"\xac\xed\x00\x05sr\x00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataB\xd9\xd9\xf7v\xa2\xdbL\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10}\xc8\xc9\xcf\xf6\xc3\xb5Y\xc7\x0c\x8eF\xa5\xfaQ\xe8xsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01c\xb4j\x94\x12sq\x00~\x00\x03\x00\x00\x01c\xb4j\x94\x12sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x01q\x00~\x00\nsq\x00~\x00\x03\x00\x00\x01c\xb4j\x94*t\x00 1131499E5A65DE1591152465E7B24B1Fsq\x00~\x00\a\x00\x00\x00\x01t\x00\x03keyt\x00\x05valuew\b\x00\x00\x01c\xb4j\x94\x12"

It can be found that session object has been stored in redis, and sessionId is used as the value of key to store the binary data of session.

conclusion

This paper briefly introduces Nginx integration Tomcat and Nginx load balancing strategy, and demonstrates the default strategy and ip_hash strategy's management of session with examples. Finally, session sharing is introduced to solve the former two ways of session management shortcomings; We will continue to learn how Tomcat manages its read and persistence to other systems, whether session updates live, serialization scheme, expiration date, and other issues.


Related articles: