Spring boot advanced tutorial: using redis to realize session sharing
Redis is a message caching middleware and a key value storage system with rich features. Spring boot provides automatic configuration for jedis client library and jedis client based abstraction provided by spring data redis. Spring boot starter redis' starter POM 'provides a convenient way to collect dependencies.
Spring boot starter redis is introduced in POM The following configuration is added to the XML configuration file (based on the pom.xml file in the previous chapter "spring boot construction framework"):
You can inject an automatically configured redisconnectionfactory, stringredistemplate or ordinary redistemplate instance that is the same as other spring beans. By default, this instance will try to connect to the redis server using localhost: 6379.
If you add a @ bean of any of its own auto configuration types, it will replace the default (except for redistemplate, which is excluded according to the bean name 'redistemplate' rather than its type). If there is commons-pool2 under the classpath path path, a connection pool factory will be obtained by default.
Redis application case
Add a configuration file with the following configuration contents:
Redis configuration class. The specific code is as follows:
Note: the annotation @ configurationproperties (prefix = "spring. Redis") in redisconn class is used to read the default configuration file information of springboot with spring Information starting with redis.
Configure cache class
Analysis: the cache class inherits cacheingconfigurersupport, which injects the redisconn class object of the read configuration file information into this class. In this class, keygenerator () method is the key generation policy, CacheManager () method is the cache management policy, redisconnectionfactory () is the redis connection, and redistemplate () method is the redistemplate configuration information. After configuration, Java objects can be stored in redis.
Test whether the configuration is successful. Example:
The redis tool class is simply encapsulated, and the code is as follows:
The cache is automatically used when querying the database, and the cache is generated according to the method. The reference code is as follows:
Note: the value of value is the key cached in redis. This key is the key you need to define when adding cache information to identify uniqueness.
Session sharing
There are many good solutions for session sharing in distributed systems, among which hosting in cache is one of the more common solutions. Next, session spring session data redis is used to realize session sharing.
Introduce dependencies in POM The following contents are added to the XML configuration file:
Session configuration. The specific code is as follows:
Maxinactiveintervalinseconds: set the session expiration time. After redis session is used, the original spring boot server session. The timeout property is no longer valid.
The specific code of the test example is as follows:
Log in to the redis server and enter the command keys' session * 'to check whether the cache is successful.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.