Spring-session integrated into Redis

Nothing else to do, learn about the spring session management, as a beginner, I learned the following:

1. Why do you use Spring-session

In conventional single web application generally used tomcat / jetty web container and the like, the user's session is managed by the container. Recording using a browser cookie sessionId, whether a session exists container according sessionId determines that the user session. Restriction here is that, in the web containers, sheets session management server storage container.

But the evolution of the primary key sites, distributed and clustered applications is the trend (to improve performance). At this time, the user request may be to a different load distribution server, and the conventional web container session management user session that is not feasible manner. Unless a cluster or a distributed web application can share session, although tomcat and other support to do so. But this following two questions:

  • Require invasive web container, raise complex issues
  • It is bound to exchange coupling between the shared session, between a web container cluster machine

 

Once you have spring-session, we can save the session to Redis, Mongodb and other nosql database. This avoids a single point of cached restrictions.

 

Let's do it

 

1. Introducing a jar package

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

2, the following were added in the configuration file application.properties

# Set the session format for the storage Redis 
spring.session.store The -type = Redis 
server.servlet.session.timeout = 3600 
#session update policy, there ON_SAVE, IMMEDIATE, the former is calling # SessionRepository # save (org.springframework.session. when Session), the cache is flushed before the response commit, # which is as long as there is any update will refresh the cache 
spring.session.redis.flush -mode = ON- the Save 
spring.session.redis.namespace = the Spring: the session 
#redis configuration 
spring.redis.database = 0 
spring.redis.host = 10.34.51.234 
spring.redis.password = 123456 
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.port=6379

3, redis server configuration

(slightly)

4, write SpringBoot program

 

 5, run login with it

 

 

 You can see the view to the session information

6, the sequence of operation is not completed. But I did not feel it necessary to complete the sequence of operations ~

 End!

 

  

Guess you like

Origin www.cnblogs.com/newz/p/11779783.html