我正在使用 Spring 配置连接到 2 个成员集群的(延迟加载的)Hazelcast 客户端。

<hz:client id="hazelcast" lazy-init="true">
    <hz:group name="${HzName}" password="${HzPassword}"/>
    <hz:properties>
        <hz:property name="hazelcast.client.connection.timeout">10000</hz:property>
        <hz:property name="hazelcast.client.retry.count">600</hz:property>
        <hz:property name="hazelcast.jmx">true</hz:property>
        <hz:property name="hazelcast.logging.type">slf4j</hz:property>
    </hz:properties>
    <hz:network smart-routing="true" redo-operation="true" connection-attempt-period="5000"
                connection-attempt-limit="2">
        <hz:member>${HzMember1}</hz:member>
        <hz:member>${HzMember2}</hz:member>
    </hz:network>
</hz:client>

我的问题是:

如果在我的应用程序启动时,两个集群成员碰巧都不可用,那么我会看到 ClusterListenerThread 抛出一个严重的异常:
WARNING: Unable to get alive cluster connection, try in 4945 ms later, attempt 1 of 2.
16-Apr-2015 14:57:34 com.hazelcast.client.spi.impl.ClusterListenerThread
WARNING: Unable to get alive cluster connection, try in 4987 ms later, attempt 2 of 2.
16-Apr-2015 14:57:39 com.hazelcast.client.spi.impl.ClusterListenerThread
SEVERE: Error while connecting to cluster!
java.lang.IllegalStateException: Unable to connect to any address in the config!
    at com.hazelcast.client.spi.impl.ClusterListenerThread.connectToOne(ClusterListenerThread.java:273)
    at com.hazelcast.client.spi.impl.ClusterListenerThread.run(ClusterListenerThread.java:79)
Caused by: com.hazelcast.spi.exception.RetryableIOException: java.util.concurrent.ExecutionException: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl$OwnerConnectionFuture.createNew(ClientConnectionManagerImpl.java:649)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl$OwnerConnectionFuture.access$300(ClientConnectionManagerImpl.java:605)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.ownerConnection(ClientConnectionManagerImpl.java:268)
    at com.hazelcast.client.spi.impl.ClusterListenerThread.connectToOne(ClusterListenerThread.java:245)

...这随后导致我的 Hazelcast Client bean 没有被实例化,因此下游依赖于它的存在的所有东西也会爆炸。
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hazelcast': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public static com.hazelcast.core.HazelcastInstance com.hazelcast.client.HazelcastClient.newHazelcastClient(com.hazelcast.client.config.ClientConfig)] threw exception; nested exception is java.lang.IllegalStateException: Cannot get initial partitions!

在 Hazelcast Client bean 的实例化(通过 Spring)期间,如何检测和处理 ClusterListenerThread 抛出的错误?

备注我对没有构建客户端 bean 的事实感到沮丧(尽管没有可用的成员),因为我知道一个已经构建的客户端可以处理它的所有成员变得不可用的事实,并且会很高兴地重新开始工作这些成员中的一个或多个再次可用。

最佳答案

您需要做的是配置 connectionAttemptLimit 和 connectionAttemptPeriod。

如果您将 connectionAttemptLimit 设置为 INT_MAX 并选择合理的 connectiontAttemptPeriod,则客户端实际上将每 X 秒尝试永远连接到给定的成员列表。这目前更像是一种解决方法,因为当您第一次打开 HazelcastClient 时,它将阻塞,直到其中一个成员可用。进一步的解决方法,您可以尝试在另一个专用线程中打开客户端。

关于制作完全支持的功能,hazelcast 团队在这里进行了讨论。
https://github.com/hazelcast/hazelcast/issues/552

我正在添加一个问题作为对该问题的引用。

关于hazelcast - 如何处理在我的 Hazelcast Client bean 实例化(由 Spring)期间 ClusterListenerThread 抛出的错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29677561/

10-12 17:45