如何处理在我的Hazelcast客户端bean的实例化(通过Spring)期间ClusterListenerThread抛出的错误?

Joh*_*n W 4 hazelcast

我使用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>
Run Code Online (Sandbox Code Playgroud)

我的问题是:

如果,在我的应用程序启动时,群集成员的两个碰巧都不可用,那么我看到ClusterListenerThread抛出了一个SEVERE异常:

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)
Run Code Online (Sandbox Code Playgroud)

...随后导致我的Hazelcast客户端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!
Run Code Online (Sandbox Code Playgroud)

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

NB.我对于没有构建Client bean这一事实感到沮丧(尽管没有可用的成员),因为我知道一个已构建的客户端可以处理其所有成员都不可用的事实,并且很高兴能够再次开始工作这些成员中的一个或多个再次可用.

san*_*car 5

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

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

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

我正在添加一个问题作为对该问题的参考.