Home  >  Article  >  Java  >  Use redis to implement session sharing under tomcat cluster

Use redis to implement session sharing under tomcat cluster

巴扎黑
巴扎黑Original
2017-06-26 09:22:001484browse

The previous article implemented the TOMCAT cluster under the NGINX reverse proxy in LINUX()

This time we implemented it based on the previous article Session sharing problem

Nginx machine: 192.168.1.108

The two tomcat machines are: 192.168.1.168

                                                                                                                                                           192.168.1.178

1: Test session sharing issue

Add in the original index.jsp page The following code

SessionId:<%= session.getId() %>
<% String username =(String)session.getAttribute("username" );
if(!"".equals(username)&& username!=null){
out.print("------username is "+session.getAttribute("username"));
    } else {
      out.print("------start username is  null");
          session.setAttribute("username", "lxs");
            out.print( "------now username is "+session.getAttribute("username"));

}

%>

At this time, the first time you visit through nginx is the 178 machine

When refreshing again, I accessed the 168 machine

##Okay It can be seen that the session is not shared at this time. Sessions are created separately under the cluster

2: Use redis to implement session sharing

There are many ways to achieve session sharing. This time, the session is stored in redis to achieve sharing.

1. Install redis (Please see how to install)

redis is also installed at 192.168.1.108, like Nginx Installed on the same machine

2. Copy the required jar packages to the lib directories of the two tomcats

3. Modify the contents in context.xml

respectively in tomcat/conf Add the following content to /context.xml

#

## host="192.168.1.108"

port="6379"

database="0"

maxInactiveInterval="60" />

The className is fixed and obtained is the implemented class in the jar package

host is the host name of redis and port is redis The port database is the database in which the session is stored in redis.

maxInactiveInterval indicates the session survival time

4. Test session sharing again

After the first three steps, the session sharing problem under redis has been realized, test again

Start redis, nginx, and tomcat respectively and visit http://192.168.1.108:7777/nginxTest/

At this time nginx first forwards to 178 On that machine, at this time, the username in the session is empty, and then a value is stored in

username, and then the value

## is printed out.

Visit http://192.168.1.108:7777/nginxTest/ again, at this time nginx is transferred to the machine 168

At this time the sessionid is the same as 178. At the same time, username also has a value, and the value

is directly printed out. This shows that the session is shared at this time, and at the same time The corresponding session

is also stored in redis.

The above is the detailed content of Use redis to implement session sharing under tomcat cluster. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn