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

本文详细介绍了如何在SpringBoot项目中整合Redis并配置缓存,包括添加依赖、配置属性及启用缓存操作。此外,还讲解了如何设置Redis集群,通过修改spring.properties文件和创建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;
 }
 
}

这样配置就可以了!

 

 

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值