java模拟redisSentinel故障转移

模拟redis出现宕机情况下redis-sentinel的故障转移。

1、java代码以下实现方式

//倘若创建maven项目需要在pom.xml中引入依赖,不是maven项目需要引入对应jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>redisDemo</groupId>
    <artifactId>redisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>

    </dependencies>

</project>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**
 * @author: ltj
 * date: 2019-04-03-0:53
 * jedisSentinel故障转移的测试
 **/
public class RedisSentinelFailoverTest {

    //打印日志
    private static Logger logger = LoggerFactory.getLogger(RedisSentinelFailoverTest.class);

    public static void main(String[] args){
        //主服务器的名称
        String masterName = "mymaster";

        //创建set对象,存储redis-sentinel的IP和端口,本实例创建创建了三个redis-sentinel来监控redis的运行情况
        Set<String> set = new HashSet<String>();
        set.add("1**.77.87.1**:26379");
        set.add("1**.77.87.1**:26380");
        set.add("1**.77.87.1**:26381");

        //创建JedisSentinelPool
        JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName,set);
        int count=0;
        //测试死循环的情况下主服务器出现宕机的情况下sentinel发生故障转移的测试
        while(true){
            count ++;
            Jedis jedis = null;
            try{
                jedis = sentinelPool.getResource();
                int index = new Random().nextInt();
                //对redis保存key和value
                String key = "k-" + index;
                String value = "v-" + index;
                jedis.set(key,value);
                if(count%100 ==0){
                    //打印日志信息
                    logger.info("key:{},value:{}",key,jedis.get(key));
                }
                //睡眠时间
                TimeUnit.MILLISECONDS.sleep(10);
            }catch (Exception e){
                logger.error(e.getMessage(),e);
            }finally {
                if (jedis!=null){
                    jedis.close();
                }
            }

        }
    }
}

 2、配置redis和redis-sentinel服务器(博主分别创建了三个)

3、启动redis

4、启动redis-sentinel

5、运行java文件(控制台中出现需求日志)

6、kill掉redis主服务器(java控制台报错)

7、过一段时间后java控制台正常运行

8、redis-sentinel实现了redis宕机情况下的故障转移结束

 

 

注意:一开始运行java文件就报端口错误:此处需要注意redis-sentinel中配置的redis中的IP和端口的正确

具体的redis-sentinel的实现过程可以查看sentinel和redis的日志文件

redis-sentinel的配置文件可参考以下链接内容

https://blog.csdn.net/weixin_42739916/article/details/88859380

redis的配置文件可参考以下链接内容

https://mp.csdn.net/postedit/88833350

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Redis Sentinel,你需要进行以下步骤: 1. 添加依赖:在你的`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 2. 配置Redis Sentinel:在`application.properties`文件中添加以下配置: ```properties spring.redis.sentinel.master=your-master-name spring.redis.sentinel.nodes=host1:port1,host2:port2,host3:port3 ``` 将`your-master-name`替换为你的Redis Sentinel的master名称,将`host1:port1,host2:port2,host3:port3`替换为你的Redis Sentinel的节点列表。 3. 创建Redis连接工厂:在你的代码中创建一个`RedisConnectionFactory`实例,例如: ```java @Configuration public class RedisConfig { @Value("${spring.redis.sentinel.master}") private String sentinelMaster; @Value("${spring.redis.sentinel.nodes}") private String sentinelNodes; @Bean public RedisConnectionFactory redisConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master(sentinelMaster) .sentinel(hostAndPortSet(sentinelNodes)); return new JedisConnectionFactory(sentinelConfig); } private Set<RedisNode> hostAndPortSet(String nodes) { Set<RedisNode> hostAndPortSet = new HashSet<>(); String[] hostAndPorts = nodes.split(","); for (String hostAndPort : hostAndPorts) { String[] parts = hostAndPort.split(":"); hostAndPortSet.add(new RedisNode(parts, Integer.parseInt(parts))); } return hostAndPortSet; } } ``` 4. 使用RedisTemplate:在你的代码中使用`RedisTemplate`来操作Redis数据,例如: ```java @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } } ``` 现在你可以在Spring Boot中使用Redis Sentinel了。只需根据你的需求创建相应的服务类,并注入`RedisTemplate`来访问Redis数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值