Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis to store Session of Tomcat cluster

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to use Redis to store the Session of Tomcat clusters, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Some time ago, I spent a lot of time looking for a way to push the newly developed code to the production system, which should be able to have zero downtime and zero impact on users. My idea is to use the cluster to do this. By notifying the load balancer Nginx, removing the Tomcat node in the cluster, then upgrading the application on Tomcat, and then notifying the load balancer Nginx to reload the Tomcat node. To do this in turn, replace all the Tomcat in the cluster once. So the question is, how do you have no impact on the user when removing the Tomcat node and loading the new Tomcat node? The method is simple: share Session. Next, we use an example to illustrate this scheme. In our example, a Nginx is used for load balancing, two Tomcat are attached to the backend, and the Session sessions of each Tomcat are saved to the Redis database. Where Nginx is configured to run in non-sticky mode, that is, each request can be assigned to any node in the cluster. When you want to launch the new code, simply remove the Tomcat instance, and all visiting users will be routed to the active Tomcat instance, and because the session data is saved in the Redis database, the active users will not be affected. When the Tomcat is updated, you can add this node to the Nginx. Install Nginx

# sudo rpm-ivh nginx-1.4.2-1.el6.ngx.x86_64.rpm

Modify the configuration file / etc/nginx/nginx.conf and add the following:

Http {upstream tomcat {server localhost:8080; server localhost:8081;} include / etc/nginx/mime.types; default_type application/octet-stream

Modify the configuration file / etc/nginx/conf.d/default.conf and replace the contents of the location section:

Location / {proxy_pass http://tomcat;}

Restart Nginx

# sudo service nginx restart

Next, install two Tomcat instances. Since we are doing the demonstration on the same server, we need to change the port number of the second Tomcat instance in order to avoid conflicts between the two Tomcat instances. Because Nginx is configured to run in non-sticky mode, Round-robin load balancing is used for each request, which means that it draws a new session for each request.

Next, download and install Redis. The steps are omitted and are very simple.

Finally, we need to configure Tomcat to have Tomcat save the session Session to the Redis database.

We are going to use a third-party library like tomcat-redis-session-manager, which can be found on the home page:

Https://github.com/jcoleman/tomcat-redis-session-manager should note that this library is not out of the box and needs to be adjusted when using it. You need to download the source code and rebuild the project after updating the version of the dependent library. For example, I used commons-pool2-2.2.jar and jedis-2.6.1.jar dependent libraries. Remember to copy these jar files to the lib subdirectory of each Tomcat instance. After updating the commons-pool, jedis, and tomcat versions of these libraries, you can use build.gradle to build the entire project. After building, copy the newly generated tomcat-redis-session-manager-1.2.jar to the lib subdirectory of each Tomcat instance. And modify the context.xml configuration file of each Tomcat instance:

Restart the Tomcat instance. You can check that Redis does save the session of Tomcat. Then when we take down or restore the Tomcat instance, the access users are really not affected.

The above is all the contents of the article "how to use Redis to store the Session of Tomcat clusters". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report