本篇文章给大家带来的内容是关于SpringBoot配置redis和分布式session-redis的方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
springboot项目和传统项目配置redis的区别,更加简单方便,在分布式系统中,解决sesssion共享问题,可以用spring session redis。
1、pom.xml
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis
2、rdis配置类类import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
@Value("${redis.maxTotal}")
private Integer maxTotal;
@Value("${redis.maxIdle}")
private Integer maxIdle;
@Value("${redis.maxWaitMillis}")
private Long maxWaitMillis;
@Bean
public ShardedJedisPool shardedJedisPool() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
List jedisShardInfos = new ArrayList<>();
jedisShardInfos.add(new JedisShardInfo(host,port));
return new ShardedJedisPool(jedisPoolConfig, jedisShardInfos);
}
}
3、session redis 配置类import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* session共享
*/
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60*60)
public class RedisSessionConfig {
}