Spring Boot series (vii) Spring boot uses Redis for session sharing
This article describes spring boot using the Redis implementation session sharing, need springboot actual combat full video tutorial, click here.
Redis is a cache message middleware and a key-value storage system with rich characteristics. Spring boot provides automatic configuration for the Jedis client library and the Jedis client-based abstraction provided by spring Data Redis. Spring-boot-starter-redis ' starter POM ' provides a convenient way to collect dependencies.
Introducing Spring-boot-starter-redis, add the following configuration in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
You can inject an automatically configured redisconnectionfactory,stringredistemplate or an ordinary redistemplate instance that is the same as other spring beans. By default, this instance will attempt to use localhost:6379 to connect to the Redis server.
@Component
public class Mybean {
private stringredistemplate template;
@Autowired
Public Mybean (stringredistemplate template) {
This.template = template;
}
// ...
}
If you add a @bean of any of your own automatic configuration types, it will replace the default (except for Redistemplate, which is excluded based on the Bean's name ' Redistemplate ' instead of its type). If a commons-pool2 exists under the classpath path, a connection pool factory is obtained by default.
Applications using Redis Cases
Add a configuration file with the following configuration:
# REDIS (redisproperties)
# Redis Server Address
spring.redis.host=192.168.0.58
# Redis Server Connection port
spring.redis.port=6379
# Connection time-out (milliseconds)
Spring.redis.timeout=0
Redis configuration class with the following code:
Import org.springframework.boot.context.properties.ConfigurationProperties;
Import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties (prefix = "Spring.redis")
public class Redisconn {
Private String host;
private int port;
private int timeout;
Public String GetHost () {
return host;
}
public void Sethost (String host) {
This.host = host;
}
public int Getport () {
return port;
}
public void Setport (int port) {
This.port = port;
}
public int gettimeout () {
return timeout;
}
public void setTimeout (int timeout) {
This.timeout = timeout;
}
@Override
Public String toString () {
Return "Redis [localhost=" + Host + ", port=" + Port + ", timeout=" + timeout + "]";
}
}
Note: In the Redisconn class, annotate @configurationproperties (prefix = "Spring. Redis ") reads the information that begins with Spring.redis in the default profile information for Springboot.
Configuring the Cache Class
Import Java.lang.reflect.Method;
Import Java.util.HashMap;
Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.cache.CacheManager;
Import Org.springframework.cache.annotation.CachingConfigurerSupport;
Import org.springframework.cache.annotation.EnableCaching;
Import Org.springframework.cache.interceptor.KeyGenerator;
Import Org.springframework.context.annotation.Bean;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.PropertySource;
Import Org.springframework.data.redis.cache.RedisCacheManager;
Import Org.springframework.data.redis.connection.RedisConnectionFactory;
Import Org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.data.redis.core.StringRedisTemplate;
Import Org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
Import org.springframework.stereotype.Component;
Import Com.cachemodle.RedisConn;
Import Com.fasterxml.jackson.annotation.JsonAutoDetect;
Import Com.fasterxml.jackson.annotation.PropertyAccessor;
Import Com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author SANDSA Redis Cache Service
*
*/
@Configuration
@EnableCaching
public class Redisconfig extends Cachingconfigurersupport {
@Autowired
Private Redisconn Redisconn;
/**
* Strategy for key production
*
* @return
*/
@Bean
@Override
Public Keygenerator Keygenerator () {
return new Keygenerator () {
@Override
public object generate (object target, Method method, Object ... params) {
StringBuilder sb = new StringBuilder ();
Sb.append (Target.getclass (). GetName ());
Sb.append (Method.getname ());
for (Object obj:params) {
Sb.append (Obj.tostring ());
}
return sb.tostring ();
}
};
}
/**
* Manage Cache
*
* @param redistemplate
* @return
*/
@SuppressWarnings ("Rawtypes")
@Bean
Public CacheManager CacheManager (redistemplate redistemplate) {
Rediscachemanager RCM = new Rediscachemanager (redistemplate);
Set the cache expiration time in seconds
Rcm.setdefaultexpiration (60);
map<string, long> map = new hashmap<string, long> ();
Map.put ("Test", 60L);
Rcm.setexpires (map);
return RCM;
}
/**