SpringBoot连接Redis Sentinel

半兽人 发表于: 2023-09-18   最后更新时间: 2023-09-18 18:58:54  
{{totalSubscript}} 订阅, 820 游览

引入Maven依赖:

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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

SpringBoot配置绑定

Spring封装的组件,比较完善的支持,我们直接在下新增关于集群的配置:

spring:
  redis:
    sentinel:
      port: 26379
      master: mymaster
      nodes:
        - 172.23.64.229
        - 172.23.14.224
        - 172.23.209.85
        - 172.23.151.215
        - 172.23.146.175
        - 172.23.115.12
    lettuce:
      shutdown-timeout: 200ms
      pool:
        max-idle: 16
        max-active: 32
        min-idle: 8

调用测试:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public List<RedisClientInfo> getClientList(String param) {
        return redisTemplate.getClientList();
    }

    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void put(String k, String v) {
        redisTemplate.opsForValue().set(k, v);
    }
}
更新于 2023-09-18
在线,1小时前登录

查看redis更多相关的文章或提一个关于redis的问题,也可以与我们一起分享文章