spring boot笔记6——spring boot整合redis、集群设置 - Go语言中文社区

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


目录

一、spring boot整合redis

1,添加依赖

2、修改application.properties

3,配置缓存到redis中

二、集群设置

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

2,创建Redis配置类


spring boot整合redis

一、spring boot整合redis

1,添加依赖

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

<!-- 整合redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

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 // 开启缓存

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

这样就可以了!

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

二、集群设置

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<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;
 }
 
}

这样配置就可以了!

 

 

 

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_22059611/article/details/82994450
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-12 20:39:28
  • 阅读 ( 1601 )
  • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢