java连接sentinel清除缓存,Redisson redis哨兵 . 如何处理故障转移并写入redis?

我刚刚编辑了我之前的问题,而且我提供了更多细节,(希望有人可以提供帮助) .

我有一个Redis集群,包含1个主服务器和2个从服务器 . 所有3个节点都由Sentinel管理 . 故障转移工作正常,当选择新主服务器时,我可以在新主服务器上编写(从命令行) . 现在,我正在尝试使用Redisson编写一个小型Java程序,理想情况下应该将记录写入redis,并且能够处理故障转移(据我所知,它应该做到这一点) . 这是我的代码,直到现在 .

import org.redisson.Redisson;

import org.redisson.RedissonNode;

import org.redisson.api.*;

import org.redisson.api.annotation.RInject;

import org.redisson.config.Config;

import org.redisson.config.RedissonNodeConfig;

import org.redisson.config.SubscriptionMode;

import java.util.Collections;

import java.util.UUID;

public class RedissonTest {

public static class RunnableTask implements Runnable {

@RInject

RedissonClient client;

@Override

public void run(){

System.out.println("I am in ..");

RMap map = client.getMap("completeNewMap");

System.out.println("is thread interrupted?? " + Thread.currentThread().isInterrupted());

NodesGroup ngroup = client.getNodesGroup();

Collection nodes = ngroup.getNodes();

for(Node node : nodes){

System.out.println("Node ip "+ node.getAddr().toString()+" type: "+node.getType().toString());

}

for(int i=0; i < 10000; i++) {

String key = "bg_key_"+String.valueOf(i);

String value = String.valueOf(UUID.randomUUID());

String oldVal = map.get(key);

map.put(key, value);

RBucket bck = client.getBucket(key);

bck.set(value);

System.out.println("I am going to replace the old value " + oldVal + " with new value " + value + " at key "+key);

}

System.out.println("I am outta here!!");

}

}

public static void main(String[] args) {

Config config = new Config();

config.useSentinelServers()

.setMasterName("redis-cluster")

.addSentinelAddress("192.168.56.101:26379")

.addSentinelAddress("192.168.56.102:26379")

.addSentinelAddress("192.168.56.103:26379")

.setPingTimeout(100)

.setTimeout(60000)

.setRetryAttempts(25)

.setReconnectionTimeout(45000)

.setRetryInterval(1500)

.setReadMode(ReadMode.SLAVE)

.setConnectTimeout(20000)

.setSubscriptionMode(SubscriptionMode.MASTER);

RedissonClient client = Redisson.create(config);

RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);

nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("myExecutor6", 1));

RedissonNode node = RedissonNode.create(nodeConfig);

node.start();

System.out.println("Node address "+node.getRemoteAddress().toString());

RExecutorService e = client.getExecutorService("myExecutor6");

e.execute(new RunnableTask());

e.shutdown();

if(e.isShutdown()) {

e.delete();

}

client.shutdown();

node.shutdown();

System.out.println("Hello World!" );

}

}

运行代码,发生了一些我不理解的事情 . 第一个是:

为什么redisson认出我的3个主机是redis奴隶?

为什么我创建的键值对不存储到redis中?

我的想法是,在我能够写入redis之后,我将开始测试故障转移,杀死主服务器并期望程序将管理它并继续写入新主服务器,而不会丢失消息(这将是很好的能够在发生故障转移时缓存消息) .

这个简单的程序会发生什么,我可以写入redis,但是当我杀死master时,执行只会暂停一段时间,似乎接近 setTimeout 并且在没有完成任务的情况下退出 .

有什么建议吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值