Home > Database > Redis > body text

How to implement springboot integrated redis sentinel master-slave

WBOY
Release: 2023-05-28 16:07:06
forward
1330 people have browsed it

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>
Copy after login

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
Copy after login

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;
}
Copy after login

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));
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template