Hazelcast:连接到远程集群

Hazelcast: connecting to remote cluster

我们在一个远程系统(具有许多节点的单个物理系统)上有一个全部 运行 的 Hazelcast 节点集群。我们想从外部客户端连接到这个集群——一个 Java 应用程序,它使用如下代码连接到 Hazelcast:

        ClientConfig clientConfig = new ClientConfig();
        clientConfig.addAddress(config.getHost() + ":" + config.getPort());

        client = HazelcastClient.newHazelcastClient(clientConfig);

其中host为远程IP,端口为5701

这仍然连接到本地主机 (127.0.0.1)。我错过了什么?

编辑:

如果 java 客户端是本地系统上唯一的 hazelcast 应用程序 运行,它将无法连接并抛出异常:java.lang.IllegalStateException: Cannot get initial partitions!

来自日志:

14:58:26.717 [main] INFO c.m.b.p.s.s.HazelcastCacheClient - creating new Hazelcast instance

14:58:26.748 [main] INFO com.hazelcast.core.LifecycleService - HazelcastClient[hz.client_0_dev][3.2.1] is STARTING

14:58:27.029 [main] INFO com.hazelcast.core.LifecycleService - HazelcastClient[hz.client_0_dev][3.2.1] is STARTED

14:58:27.061 [hz.client_0_dev.cluster-listener] INFO com.hazelcast.core.LifecycleService - HazelcastClient[hz.client_0_dev][3.2.1] is CLIENT_CONNECTED

14:58:27.061 [hz.client_0_dev.cluster-listener] INFO c.h.client.spi.ClientClusterService -

Members [5] { Member [127.0.0.1]:5701 Member [127.0.0.1]:5702 Member [127.0.0.1]:5703 Member [127.0.0.1]:5704 Member [127.0.0.1]:5705 }

14:58:47.278 [main] ERROR c.h.c.spi.ClientPartitionService - Error while fetching cluster partition table!

com.hazelcast.spi.exception.RetryableIOException: java.util.concurrent.ExecutionException: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused: no further information ... Caused by: java.util.concurrent.ExecutionException: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused: no further information

at java.util.concurrent.FutureTask.report(Unknown Source) ~[na:1.8.0_31]

at java.util.concurrent.FutureTask.get(Unknown Source) ~[na:1.8.0_31]

at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrConnect(ClientConnectionManagerImpl.java:282) ~[BRBASE-service-manager-1.0.0-jar-with-dependencies.jar:na]

... 14 common frames omitted

Caused by: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused: no further information

at com.hazelcast.util.ExceptionUtil.rethrow(ExceptionUtil.java:45) ~[BRBASE-service-manager-1.0.0-jar-with-dependencies.jar:na] ...

你能试试吗:

ClientConfig config = new ClientConfig();
config.getNetworkConfig().addAddress(host + ":" + port);
HazelcastInstance instance = HazelcastClient.newHazelcastClient(config);

要连接到远程集群,请确保集群使用外部 IP 而不是 127.0.0.1。在我们的例子中,我们有一个物理系统,有多个节点,启用了 tcp-ip 模式。 hazelcast.xml 具有配置:

<tcp-ip enabled="true">
    <!-- This should be external IP -->
    <interface>172.x.x.x</interface>
</tcp-ip>

如果你想连接到多个 ip 的 运行 Hazelcast 作为集群添加下面到你的客户端配置然后实例化客户端。

//configure client properties
ClientConfig config = new ClientConfig();
String[] addresses = {"172.20.250.118" + ":" + "5701","172.20.250.49" + ":" + "5701"};
config.getNetworkConfig().addAddress(addresses);

//start Hazelcast client
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(config);