Home  >  Article  >  Database  >  How to implement springboot integrated redis sentinel master-slave

How to implement springboot integrated redis sentinel master-slave

WBOY
WBOYforward
2023-05-28 16:07:061296browse

1. Environment

spring boot 2.3.12.RELEASE
JDK 1.8
IntelliJ IDEA development tool
Redis sentinel master-slave construction

2. POM file

Others in the pom file are ignored, only system dependencies related to redis are displayed

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 重点:redis依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 对象池框架,redis依赖 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

3. Application.yml configuration

Ignore the configuration of springboot
spring convention is greater than configuration. For the default one, there is no need to reflect it in the configuration file

spring:
  redis:
    # redis库
    database: 1
    # redis节点的密码
    password: jwssw
    # 集群配置
    sentinel:
      # 集群哨兵节点配置,多个节点之间用英文逗号分割
      nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
      # 主节点名称
      master: mymaster
      # 密码
      password: jwssw

Note that if the redis sentinel configuration file adds requirepass (access key), [password] must be added to the sentinel node, otherwise there is no need to add it.

4. reidsTemplate configuration

This configuration file can be loaded directly into the startup class, because the startup class is also a configuration class of springboot

/**
 * 方法描述: 初始化redis连接
 *
 * @param factory redis连接工厂
 * @return {@link RedisTemplate}
 */
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    // 新建redisTemplate对象
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    // 设置工厂
    template.setConnectionFactory(factory);
    // 键值类型
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    // 返回redisTemplate对象
    return template;
}

5. Unit test (JUnit4)

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
    // 注入redisTemplate对象
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Test
    public void setOrGetTest() {
        // redis键值 
        String redisKey = "name";
        // 向redis存放内容
        redisTemplate.opsForValue().set(redisKey, "张三" + new Random().nextInt());
        // 获取redis中的内容并打印
        System.out.println(redisTemplate.opsForValue().get(redisKey));
    }
}

The above is the detailed content of How to implement springboot integrated redis sentinel master-slave. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete