SpringBoot (20): cluster using Spring Session-redis
I. the solution of session cluster
1.1.Extending the specified server
Make use of the plug-in function provided by the Servlet container to customize the creation and management policy of HttpSession and replace the default policy by configuration. Disadvantages: coupled with Tomcat/Jetty and other Servlet containers, containers can not be changed at will.
1.2.Using Filter
Using HttpServletRequestWrapper, implement your own getSession () method to take over the creation and management of Session data. Spring-session is realized through this way of thinking.
Spring session support methods in Spring Boot:
JDBC 、 MongoDB 、 Redis 、 Hazelcast 、 HashMap
II. Realization
2.1. Add dependencies
Org.springframework.session spring-session org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-redis
2.2.Configuring application.properties
# spring session starts # spring session uses storage type spring.session.store-type=redis# spring session refresh mode: default on-save#spring.session.redis.flush-mode=on-save#spring.session.redis.namespace=# session timeout Unit second # server.session.timeout=30#spring session end # # redis start # spring.redis.host=192.168.175.13spring.redis .port = 6379spring.redis.password=123456#spring.redis.database=0#spring.redis.pool.max-active=8#spring.redis.pool.max-idle=8#spring.redis.pool.max-wait=-1#spring.redis.pool.min-idle=0#spring.redis.timeout=0#redis ends #
2.3. Testing
Two new applications are created according to the above configuration, one with port 8989 and the other with port 8988. The results are as follows: