SpringBoot配置redis和分布式session-redis
springboot项目 和传统项目 配置redis的区别,更加简单方便,在分布式系统中,解决sesssion共享问题,可以用spring session redis。
1、pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
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<JedisShardInfo> 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 { }
版权声明:如需转载,请注明!PS:如是转载随便,请忽略