我使用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抛出了一个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)
...随后导致我的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!
如何在Hazelcast Client bean的实例化(Spring)实例化过程中检测并处理ClusterListenerThread抛出的错误?
NB。我没有构建客户端bean(尽管没有可用的成员)这一事实让我感到沮丧,因为我知道一个已经构建的客户端可以处理其所有成员的事实&#39; s变得不可用,并且当一个或多个成员再次可用时,将很乐意再次开始工作。
答案 0 :(得分:5)
您需要做的是配置connectionAttemptLimit和connectionAttemptPeriod。
如果将connectionAttemptLimit设置为INT_MAX并选择合理的connectiontAttemptPeriod,则客户端实际上会每隔X秒尝试连接到给定的成员列表。这当前更像是一种解决方法,因为当您第一次打开HazelcastClient时,它将阻塞,直到其中一个成员可用。进一步的解决方法是,您可以尝试在另一个专用线程中打开客户端。
关于制作完全支持的功能,榛子团队正在进行讨论。 https://github.com/hazelcast/hazelcast/issues/552
我正在添加一个问题作为该问题的参考。