社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
目录
spring boot整合redis
spring boot要整合redis第一步当然是添加依赖咯,依赖如下:
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=5000
(1)spring启动类中加上:
@EnableCaching // 开启缓存
(2)service程序中加缓存操作(ItemsServiceImpl类)
这样就可以了!
一旦启动,并调用getall()方法,则会在redis生成一个allItems:
spirng.redis.cluster.nodes=192.168.10.110:7001,192.168.10.111:7001,192.168.10.112:7001
@Configuration
public class RedisConfig {
@Value("${spirng.redis.cluster.nodes}")
private String redisNodes;
@Bean
public JedisCluster getJedisCluster(){
String[] redisnodes = redisNodes.split(",");
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
for (String node : redisnodes) {
String[] arr = node.split(":");
HostAndPort hostAndPort =
new HostAndPort(arr[0],Integer.parseInt(arr[1]));
nodes.add(hostAndPort);
}
JedisCluster cluster = new JedisCluster(nodes);
return cluster;
}
}
这样配置就可以了!
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!