redission-tomcat: deploy quickly realize from single to multi-machine deployment

Original Address:  http://blog.jboost.cn/2019/06/29/session-redis.html

 

Some early projects for simple and fast, we are doing a stand-alone development and deployment, but with the expansion of business, or improve availability requirements, stand-alone environment has not satisfied the requirements. Stand-alone deployment to deploy multi-machine switch, there may be an important part of which is to share the session (if the start token-based authentication can be ignored). This article describes a tomcat session management based redis of open source projects: redission-tomcat, the code can not intrusive quickly realize session sharing.

1 Introduction

redisson is jedis a similar redis client, its function to be richer than some of jedis. redission-tomcat is based on a tomcat session redis project manager, project Address: https://github.com/redisson/redisson/tree/master/redisson-tomcat  . Compared to other implementations, more efficient storage of the project, the write operation and more optimized. Each call session parameter is HttpSession.setAttributewritten when redis, but other solutions are generally always be written after the entire sequence of the session.

2. Use

  1. The redisson-All-3.11.0.jar , redisson-tomcat-8-3.11.0.jar (for tomcat8, other versions can be found on the download link address in the above item page) to download two jar package into the lib directory of tomcat .

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

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

  among them

  • configPath: Redisson point profile or yaml json format is given in Step 3.
  • readMode: session attributes read mode. Possible values ​​1. MEMORY, the pattern will simultaneously session attribute saved in the local session and redis Tomcat, the updates are propagated to the subsequent session by session redis Tomcat local events;. 2 REDIS, save only the session attribute into redis. The default is REDIS.
  • updateMode: update mode session attributes. Possible values 1. DEFAULT, session attributes only setAttributesaved to redis method;. 2 AFTER_REQUEST, after each request, to save all redis session attribute. The default is DEFAULT.
  • broadcastSessionEvents: If set to true, the sessionCreated and sessionDestroyed event will be broadcast to all instances tomcat, and all registered HttpSessionListeners listener is triggered. The default is false.
  1. New configuration file in the tomcat conf directory redisson.conf, reads 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 redis environment configuration standalone mode, wherein the password, address modify their values. If a cluster model, the configuration file

    {
      "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, 
        "slaveConnectionPoolSize": 64,
        "slaveConnectionMinimumIdleSize": 24,
        "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"
    }

     

  2. We can use nginx to achieve load balancing, reference 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/;
      }
    }

          Above is the use redisson-tomcat to achieve a single multi-machine configuration deployed to all deployments.

3. Summary

With the development of technology infrastructure services and are evolving. In the initial stage of business development, the number of users, complexity of the business are relatively low, in order to achieve rapid on-line verification, often using simple single architecture. Many projects may not have had time Architecture Evolution upgrades GG, while the project is bound to have the opportunity to continue to grow with the continuous optimization and upgrading business expansion. redisson-tomcat This article describes the stand-alone program that can help to quickly switch to multi-machine support, of course, only in the session management aspects. If it comes to other issues such as file upload, regular tasks distributed support, the other will have to adjust accordingly.



My personal blog address: http://blog.jboost.cn
my github address: https://github.com/ronwxy
my micro-channel public number: jboost-ksxy (not only a real dry technology public number, welcome
attention) ------------------------------------------------ ---------------
Micro-channel public number

Guess you like

Origin www.cnblogs.com/spec-dog/p/11105943.html