spring boot笔记6——spring boot整合redis、集群设置

向右看齐 2022-05-08 08:08 1149阅读 0赞

目录

一、spring boot整合redis

1,添加依赖

2、修改application.properties

3,配置缓存到redis中

二、集群设置

1,spring.properties中加入集群地址

2,创建Redis配置类


spring boot整合redis

一、spring boot整合redis

1,添加依赖

spring boot要整合redis第一步当然是添加依赖咯,依赖如下:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

70

2、修改application.properties

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

3,配置缓存到redis中

(1)spring启动类中加上:

@EnableCaching // 开启缓存

70 1

(2)service程序中加缓存操作(ItemsServiceImpl类)

70 2

这样就可以了!

一旦启动,并调用getall()方法,则会在redis生成一个allItems:

70 3

二、集群设置

1,spring.properties中加入集群地址

spirng.redis.cluster.nodes=192.168.10.110:7001,192.168.10.111:7001,192.168.10.112:7001

2,创建Redis配置类

@Configuration
public class RedisConfig {

@Value(“${spirng.redis.cluster.nodes}“)
private String redisNodes;

@Bean
public JedisCluster getJedisCluster(){

String[] redisnodes = redisNodes.split(“,”);

Set nodes = new HashSet();
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;
}

}

这样配置就可以了!

发表评论

表情:
评论列表 (有 0 条评论,1149人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot整合Redis笔记

    ?前言??博客:【无聊大侠hello word】?✍有一点思考,有一点想法,有一点理性!✍✍本文由在下【无聊大侠hello word】原创,首发于CSDN✍。