如何从Java客户端检查Hazelcast集群是否存活?

7
我们使用hazelcast在客户端服务器模式下。Hazelcast集群包含2个节点,我们有大约25个客户端连接到该集群。
现在我要找的是一个简单的检查方法,尝试确定集群是否仍然活动。这应该是一项相当廉价的操作,因为这个检查将经常在每个客户端上发生(我可以想象每秒钟一次)。
那么最好的方法是什么?
4个回答

10

最简单的方法是将一个LifecycleListener注册到客户端HazelcastInstance:

    HazelcastInstance client = HazelcastClient.newHazelcastClient();
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
        @Override
        public void stateChanged(LifecycleEvent event) {

        }
    })
客户端使用定期的心跳来检测集群是否仍在运行。


抱歉回复晚了!我注册了监听器,在我关闭集群后几乎立即收到通知,所以基本上是可以的。不过我只收到了2个事件:CLIENT_CONNECTED和CLIENT_DISCONNECTED。我还需要处理其他事件吗,或者这些事件不应该在客户端进行评估? - u6f6o

9
你也可以使用 LifecycleService.isRunning() 方法:
HazelcastInstance hzInstance = HazelcastClient.newHazelcastClient();
hzInstance.getLifecycleService().isRunning()

3
在客户端未连接的情况下(即集群中没有节点且客户端重复记录“无法获取活动集群连接”),调用此方法会返回 true。 - henryw374

2
由于 `isRunning()` 可能在集群停机的情况下仍然为 `true`,因此我会采用以下方法(结合了 @konstantin-zyubin 的回答和这里) 。在我的设置中,这不需要事件监听器,这是一个优点。
if (!hazelcastInstance.getLifecycleService().isRunning()) {
  return Health.down().build();
}

int parameterCount;
LocalTopicStats topicStats;
try {
  parameterCount = hazelcastInstance.getMap("parameters").size();
  topicStats = hazelcastInstance.getTopic("myTopic").getLocalTopicStats();
} catch (Exception e) {
  // instance may run but cluster is down:
  Health.Builder builder = Health.down();
  builder.withDetail("Error", e.getMessage());
  return builder.build();
}

Health.Builder builder = Health.up();
builder.withDetail("parameterCount", parameterCount);
builder.withDetail("receivedMsgs", topicStats.getReceiveOperationCount());
builder.withDetail("publishedMsgs", topicStats.getPublishOperationCount());
return builder.build();

有没有可以用来检查的REST端点? - undefined
检查什么,@mike01010?我的代码用于检查我的Hazelcast实例的端点,但我猜这不是你的意思吧? - undefined
我正在努力弄清楚是否有端点可以检查可用实例的数量,或者队列是否准备就绪。我假设如果队列准备就绪,我可以开始提交作业而不必等待实例。 - undefined

1
我发现了一种更可靠的方法来检查hazelcast的可用性,因为
client.getLifecycleService().isRunning()

当您使用异步重新连接模式时,始终返回true,如前所述。
@Slf4j
public class DistributedCacheServiceImpl implements DistributedCacheService {

    private HazelcastInstance client;

    @Autowired
    protected ConfigLoader<ServersConfig> serversConfigLoader;

    @PostConstruct
    private void initHazelcastClient() {
        ClientConfig config = new ClientConfig();
        if (isCacheEnabled()) {
            ServersConfig.Hazelсast hazelcastConfig = getWidgetCacheSettings().getHazelcast();
            config.getGroupConfig().setName(hazelcastConfig.getName());
            config.getGroupConfig().setPassword(hazelcastConfig.getPassword());
            for (String address : hazelcastConfig.getAddresses()) {
                config.getNetworkConfig().addAddress(address);
            }

            config.getConnectionStrategyConfig()
                  .setAsyncStart(true)
                  .setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);

            config.getNetworkConfig()
                  .setConnectionAttemptLimit(0) // infinite (Integer.MAX_VALUE) attempts to reconnect
                  .setConnectionTimeout(5000);

            client = HazelcastClient.newHazelcastClient(config);
        }
    }

    @Override
    public boolean isCacheEnabled() {
        ServersConfig.WidgetCache widgetCache = getWidgetCacheSettings();
        return widgetCache != null && widgetCache.getEnabled();
    }

    @Override
    public boolean isCacheAlive() {
        boolean aliveResult = false;
        if (isCacheEnabled() && client != null) {
            try {
                IMap<Object, Object> defaultMap = client.getMap("default");
                if (defaultMap != null) {
                    defaultMap.size(); // will throw Hazelcast exception if cluster is down
                    aliveResult = true;
                }
            } catch (Exception e) {
                log.error("Connection to hazelcast cluster is lost. Reason : {}", e.getMessage());
            }
        }
        return aliveResult;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接