Quickly implement Tomcat cluster Session sharing
preface
In the Internet scenario with huge user traffic, setting up Tomcat cluster is an essential solution to ease the load of Web servers, and the resulting problem of Session information, namely Session synchronization, is also exposed: After a user logs in, a message is displayed indicating that the user needs to log in again, which seriously affects user experience. This paper mainly studies how to use the Spring Session framework to solve the Tomcat cluster Session sharing problem. If there is any supplement, welcome to correct.
The body of the
Environment to prepare
- Three instances of Tomcat
- Redis
The project structure
The project is relatively simple, with only one controller class besides the launch class.
Controller implementation
The UserController has two main request methods, one that accepts the user’s login and the other that retrieves the login information. After the login interface is invoked, the request data is stored in the current Session, and the corresponding login data can be obtained by invoking getUserInfo interface during the validity of the Session.
@RequestMapping("/user")
@RestController
public class UserController {
@RequestMapping("/login")
public String login(HttpSession session, HttpServletRequest request) {
String id = request.getParameter("id");
String name = request.getParameter("name");
HashMap<Object, Object> userInfo = new HashMap<>(16);
userInfo.put("id", id);
userInfo.put("name", name);
session.setAttribute("USER_INFO", userInfo);
return userInfo + "Saved to session successfully";
}
@RequestMapping("/getUserInfo")
public String getUserInfo(HttpSession session, HttpServletRequest request) {
Object user_info = session.getAttribute("USER_INFO");
if (user_info == null) {
return "Please log in before reading session data";
}
return "Reading data from session"+ user_info; }}Copy the code
Now we cluster three Instances of Tomcat and run the project; If we send a login request to a Tomcat instance and then send a request for user information again, the Tomcat will be able to correctly return the information stored after the previous login; When we try to get user information on another Tomcat instance, the message “Please log in first, then read session data” is returned. This indicates that the session information of the two Tomcat instances exists independently.
Using the Spring Session
Now, if you want to share Session information between these Tomcats, you can log in once and access data on other cluster instances using the Spring Session framework, which can share sessions without any intrusion into the application. First we need to make POM files to import Spring Session-related libraries
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Copy the code
Spring Session uses the in-memory database Redis to store Session information to share sessions between clusters.
After introducing post-dependency libraries, we need to configure sessions on the application.properties file.
server.servlet.session.timeout=3600 //1 spring.session.redis.flush-mode=IMMEDIATE //2 Spring. The session. Redis. Namespace = spring: the session / / 3/4 spring. Redis. Host = 127.0.0.1 spring. Redis. Password = spring.redis.port=6380Copy the code
The following describes the new configuration in the file:
- Specifies the Session timeout duration. The default unit is second
- Set the Session refresh mode, including ON_SAVE and IMMEDIATE. IMMEDIATE indicates that the file is written to Redis immediately. ON_SAVE indicates only when executed
SessionRepository.save(org.springframework.session.Session)
Is written to Redis. - Stored in the Redis session namespace.
- Connect to the Redis database
The login request is then invoked again after the project is packaged into each Tomcat, and all the current KEYS are queried in Redis
Then we get user information for another Tomcat request, and we can see that the result is no longer “please log in first, then read session data “, but the session data that was logged in on the previous Tomcat instance. This also shows that Tomcat cluster session sharing implementation, isn’t it easy?
reference
- Docs. Spring. IO/spring – sess…
- Docs. Spring. IO/spring – sess…