Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (2) Realizing Session Sharing

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (1) Using NGINX to Build a Cluster

Java cluster combat: single architecture upgrade to cluster architecture (3) sharing of uploaded files

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (4) Using REDIS Distributed Locks

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (5) Scheduled Tasks

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (6) Distributed Cache REDIS

By default, the session is stored in the memory of the TOMCAT server. If we have two TOMCATs, their sessions are not shared. What we have to do this time is to save the session in redis, so that two TOMCATs can share the session. In fact, the detailed principle of this product is still very complicated, but you just need to remember one thing: TOMCAT must implement session sharing if it wants to be a cluster. Fortunately, this function is relatively simple to implement, let's implement it below.

GitHub:  https://github.com/Dengxd/JavaCluster  All source codes are here, GitHub often cannot be connected, so refresh several times

Install Redis

Windows Redis download address: Releases zkteco-home/redis-windows GitHub   github often fails to connect, try a few more times

Just click on the Source code (zip). This guy is also funny. The source code is written here. I downloaded it but found that there is no source code. It is an EXE file, so I don’t need to compile it.

Unzip the file, double-click to run redis-server.exe, and this interface appears:

Ok, redis installation is complete.

Install and configure nginx

To install NGINX, you can refer to the previous article "Java Cluster: Upgrading Monolithic Architecture to Cluster Architecture (1) Using NGINX to Build a Cluster", but today we don't use three servers, but one server, as shown below:

TOMCAT and NGINX are in the same server, so when configuring NGINX, the IP should be changed to 127.0.0.1

Modify the nginx.conf file, put:

upstream mytomcat {
		server 192.168.1.200:8000;
		server 192.168.1.201:8001;
}

changed to

upstream mytomcat {
		server 127.0.0.1:8000;
		server 127.0.0.1:8001;
}

If you have already started nginx, open another cmd window and use the command nginx -s reload to refresh the configuration:

write a login page

This login page is similar to the login function of ordinary websites, but there is a difference: before verifying the username and password, first check whether there is a username in the session. If there is, it means that you have already logged in, and you will not log in again. In addition, the port number is also passed to the page for display.

String loginUser=(String)session.getAttribute("user");
request.setAttribute("port",MainApplication.port);
if(loginUser!=null){
    return "hello";
}

Let's simply not read the database, use abc for the user name, and 123 for the password

if(user.equals("abc") && password.equals("123")) {
    session.setAttribute("user",user);
    return "hello";
}

Add to the pom.xml file:

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

The applicatioin.properties file should add redis and session configuration

spring.redis.host=localhost
spring.redis.port=6379
# spring.redis.password=123456
spring.redis.timeout=1000
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=2000


# session存到redis中(最重要的就是这一行)
spring.session.store-type=redis
# session失效时间
spring.session.timeout=1800
server.servlet.session.cookie.max-age=1800

test

First, start the login webpage program, open two cmd windows, and run the following two commands respectively:

  1. java -jar login-0-SNAPSHOT.jar --server.port=8000
  2. java -jar login-1.0-SNAPSHOT.jar --server.port=8001

Then we start nginx

Enter http://localhost/login in your browser

Enter the user name abc, password 123, click submit, enter the hello page

You can see that tomcat on port 8000 is processing user login

We can open a few more tabs and enter http://localhost/hello

If you see port 8001, it means success.

View the data saved in redis

Double-click to run redis-cli.exe, enter keys *, press Enter, you can see the session data saved in redis:

Session-related principles

If you are interested in session principles, you can search online. There are still many such articles. Here are a few:

https://blog.csdn.net/YXXXYX/article/details/125342292

https://www.cnblogs.com/jing99/p/11785070.html

https://blog.csdn.net/qq_43842093/article/details/120836732

Guess you like

Origin blog.csdn.net/dengxiaodai/article/details/129707451