Nginx series tutorials nginx Solving session consistency

Posted by djelica on Sun, 12 Apr 2020 03:53:23 +0200

session viscosity

Each request is allocated according to the hash result of the access ip, so that each visitor accesses a back-end server, which can solve the session problem.

upstream backserver {

But there are disadvantages. There is a single risk. If I have logged in on port and found that the 14 server is hung after a period of time (the session time has not expired), then I will visit the 15 server at this time. At this time, I need to log in again, because when I go to the 15 server with the JsessionId on the 14 server to request to find that it does not exist.

In the nginx configuration file, add configuration, HASH the IP to the server.

This implementation is the simplest. But for the front-end deployed with SBC, or many requests are from an external gateway, it is useless. It is also not easy to use if the app will switch wifi to ip.

session replication

It can be configured through the server.xml file of tomcat, so that each Tomcat will synchronize the corresponding session, so even if a Tomcat is down at this time, the service will not be affected.

See the following for specific tomcat, tomcat cluster configuration corresponding to tomcat official website

In tomcat's web.xml, it is configured as a cluster mode,
Configure the cluster information in tomcat's server.xml.

After configuration, the session can be copied automatically.

The disadvantage is that each server needs to save a full amount of session information, which is basically unavailable when there are many servers. But the development is simple, when there are only two or three servers, it is OK.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

          <Manager className="org.apache.catalina.ha.session.DeltaManager"

          <Channel className="">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            <Interceptor className=""/>
            <Interceptor className=""/>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

session sharing (springboot)

Add pom file in corresponding project


Configure redis information in the corresponding

Add the annotation @ enablereredishttpsession to enable spring session support

public class RedisSessionConfig {  

Add verified interface

    String port;

    public Object getSession(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("SessionId", request.getSession().getId());
        map.put("ServerPort", "The service port number is "+port);
        return map;

Visit http://localhost:8080/session

Let's change the port and visit again
This time I changed the port to 8888 access: http://localhost:8888/session

The redis database has been refreshed, and the cached data has not changed

The SessionId in the result is the same, but it is served by two different project projects. In this way, spring session uses interceptor Filter to help us set up synchronization before each request to achieve session sharing in the distributed system.

session sharing (springmvc)

First, add the corresponding jar in the pom.xml file


Configure in the corresponding springmvc-context.xml file

<bean id="redisHttpSessionConfiguration"
        <property name="maxInactiveIntervalInSeconds" value="600"/>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="100" />
        <property name="maxIdle" value="10" />
        <property name="testOnBorrow" value="true" />

    <bean id="connectionFactory"  class="" >
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="port" value="6379" />
        <property name="hostName" value="" />
        <property name="password" value="xxx" />
        <property name="timeout" value="30000" ></property>
    </bean >

    <bean id="redisTemplate" class="" >
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="keySerializer">
            <bean  class="" />
        <property name="valueSerializer">
            <bean class="" />
    </bean >

    <bean id="redisUtil" class="com.isea533.mybatis.service.RedisUtil" >
        <property name="redisTemplate" ref="redisTemplate" />
    </bean >

Secondly, configure it in web.xml (it is very important that the name of filter must be springSessionRepositoryFilter)


Access project path rendering results

Previous articles

Nginx series tutorial (1) basic introduction and installation of nginx
Nginx series tutorial (2) nginx building static resource web server
Nginx tutorial series (3) static files on nginx cache server
Nginx series tutorials (4) nginx deals with load balancing of web applications to ensure high concurrency
Nginx series tutorial (5) how to ensure the high availability of nginx
Nginx series of tutorials (6) detailed explanation of nginx location matching rules
Nginx series(7)Detailed description of nginx rewrite configuration rules
Nginx series tutorial (8) nginx configuring security certificate SSL

Topics: Web Server Session Redis Apache Nginx