1

我刚刚编辑了我之前的问题,我正在提供更多详细信息(希望有人能够提供帮助)。

我有一个带有 1 个主设备和 2 个从设备的 Redis 集群。所有 3 个节点均由 Sentinel 管理。The failover works fine and when the new master is elected, I can write on the new master (from the command line). 现在,我正在尝试使用 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<String, String> map = client.getMap("completeNewMap");
            System.out.println("is thread interrupted?? " + Thread.currentThread().isInterrupted());

            NodesGroup ngroup = client.getNodesGroup();
            Collection<Node> 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<String> 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 之后,我将开始测试故障转移,杀死 master,并期望程序会管理它并继续写入新的 master,而不会丢失消息(这会很好能够在发生故障转移时缓存消息)。

这个简单的程序发生的事情是我可以写入redis,但是当我杀死master时,执行只是挂起一段时间,似乎接近setTimeout并退出而没有完成任务。

有什么建议吗?

4

1 回答 1

0

您应该设置retryAttempts足够大的参数以使 Redisson 在故障转移期间存活。

于 2018-05-28T07:12:08.300 回答