Redission-tomcat quickly implements deployment from single machine to multi-machine deployment

Redission-tomcat quickly implements deployment from single machine to multi-machine deployment

Preface

In the early stages of some projects, development and deployment are done on a single machine for the sake of simplicity and speed. However, as the business expands or the requirements for availability increase, the single-machine environment no longer meets the needs. When switching from a single-machine deployment to a multi-machine deployment, an important step may be session sharing (which can be ignored if token-based authentication is used from the beginning). This article introduces a redis-based tomcat session management open source project: redission-tomcat, which can quickly implement session sharing without code intrusion.

Introduction

Redisson is a redis client similar to Jedis, but with richer functions than Jedis. redission-tomcat is a tomcat session manager project based on redis, project address. Compared to other implementations, this project's storage is more efficient and write operations are more optimized. Each session parameter is written to redis when calling HttpSession.setAttribute, while other solutions generally serialize the entire session and write it each time.

use

1. Download the two jar packages redisson-all-3.11.0.jar and redisson-tomcat-8-3.11.0.jar (for tomcat8, other versions can be found in the above project address page) and put them in the lib directory of tomcat.

2. Add the following configuration to the context.xml file in the tomcat conf directory

<Manager className="org.redisson.tomcat.RedissonSessionManager"
configPath="${catalina.base}/conf/redisson.conf" 
readMode="MEMORY" updateMode="AFTER_REQUEST" broadcastSessionEvents="false"/>

in

  • configPath: points to the Redisson configuration file in json or yaml format, given in step 3.
  • readMode: The reading mode of the session attribute. The possible values ​​are 1. MEMORY, which saves session attributes to both the local Tomcat session and Redis. Subsequent session updates are propagated to the local Tomcat session through Redis events. 2. REDIS, which only saves session attributes to Redis. The default is REDIS.
  • updateMode: The update mode of the session attribute. The possible values ​​are: 1. DEFAULT, session attributes are only saved to redis through the setAttribute method; 2. AFTER_REQUEST, after each request, all session attributes are saved to redis. The default is DEFAULT.
  • broadcastSessionEvents: If set to true, the sessionCreated and sessionDestroyed events will be broadcast to all tomcat instances and all registered HttpSessionListeners will be triggered. The default value is false.

3. Add a new configuration file redisson.conf in the tomcat conf directory, the content is as follows

{
"singleServerConfig":{
"idleConnectionTimeout":10000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"password":"123456",
"subscriptionsPerConnection":5,
"clientName":null,
"address": "redis://127.0.0.1:6379",
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"connectionMinimumIdleSize":24,
"connectionPoolSize":64,
"database":0,
"dnsMonitoringInterval":5000
},
"threads":16,
"nettyThreads":32,
"codec":{
"class":"org.redisson.codec.FstCodec"
},
"transportMode":"NIO"
}

The above is the redis environment configuration for stand-alone mode, where password and address are modified to your own values. If it is cluster mode, the configuration file is

{
"sentinelServersConfig":{
"idleConnectionTimeout":10000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"failedSlaveReconnectionInterval":3000,
"failedSlaveCheckInterval":60000,
"password":null,
"subscriptionsPerConnection":5,
"clientName":null,
"loadBalancer":{
"class":"org.redisson.connection.balancer.RoundRobinLoadBalancer"
},
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"slaveConnectionMinimumIdleSize":24,
"slaveConnectionPoolSize":64,
"masterConnectionMinimumIdleSize":24,
"masterConnectionPoolSize":64,
"readMode":"SLAVE",
"subscriptionMode":"SLAVE",
"sentinelAddresses":[
"redis://127.0.0.1:26379",
"redis://127.0.0.1:26389"
],
"masterName":"mymaster",
"database":0
},
"threads":16,
"nettyThreads":32,
"codec":{
"class":"org.redisson.codec.FstCodec"
},
"transportMode":"NIO"
}

We can use nginx to achieve load balancing, refer to the configuration

upstream cnserver{
server 127.0.0.1:8080 weight=2 fail_timeout=10s max_fails=1;
server 127.0.0.1:8081 weight=2 fail_timeout=10s max_fails=1;
}
server {
listen 80;
server_name localhost;
index index.html index.htm;
location /rest/ {
index index.html;
proxy_pass http://cnserver/rest/;
}
}

The above is all the configurations for using redisson-tomcat to implement single-machine deployment to multi-machine deployment.

Summarize

The technical architecture continues to evolve as the business develops. In the early stages of business development, the number of users and business complexity are relatively low. In order to achieve rapid online verification, a simple and single architecture is often adopted. Many projects may fail before they have time to evolve and upgrade their architecture, but those projects that are fortunate enough to continue to grow will inevitably be continuously optimized and upgraded as their business expands.

The redisson-tomcat introduced in this article can help single-machine projects quickly switch to multi-machine support, of course only in the session management link. If other distributed supports such as file upload, scheduled tasks, etc. are involved, corresponding adjustments must be made.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to implement distributed locks based on Redis in Java annotations
  • redission distributed lock prevents repeated initialization problem

<<:  Detailed explanation of JS browser event model

>>:  Analysis of MySQL lock mechanism and usage

Recommend

HTML mouse css control

Generally speaking, the mouse is displayed as an u...

MySQL 8.0.20 installation and configuration detailed tutorial

This article shares with you a detailed tutorial ...

MySQL deep paging (how to quickly paginate tens of millions of data)

Table of contents Preface Case optimization summa...

A brief discussion on the calculation method of key_len in mysql explain

The MySQL explain command can analyze the perform...

Component design specifications for WeChat mini-program development

WeChat Mini Program Component Design Specificatio...

MySQL uses the truncate command to quickly clear all tables in a database

1. Execute the select statement first to generate...

Teach you to quickly build a web cluster project based on nginx

Table of contents 1. Project Environment 2. Proje...

js basic syntax and maven project configuration tutorial case

Table of contents 1. js statement Second, js arra...

JavaScript implements an input box component

This article example shares the specific code for...

Detailed explanation of Nginx version smooth upgrade solution

Table of contents background: Nginx smooth upgrad...

How to recover data after accidentally deleting ibdata files in mysql5.7.33

Table of contents 1. Scenario description: 2. Cas...

HTML blockquote tag usage and beautification

Blockquote Definition and Usage The <blockquot...

How to optimize MySQL performance through MySQL slow query

As the number of visits increases, the pressure o...