【问题标题】:hazelcast client exception when connecting to server连接到服务器时hazelcast客户端异常
【发布时间】:2014-09-06 12:01:10
【问题描述】:

我正在尝试使用 hazelcast v3.2.4(服务器和客户端上的版本相同)。服务器(作为我可以实施的简单实现)正在服务器上运行。客户端尝试连接到远程服务器 - 服务器打印身份验证请求,但我收到以下日志输出(包括异常) - 关于我可以做不同的任何想法(复制日志输出和配置文件)。我正在尝试通过 TCP/IP 进行连接,并检查了网络连接 - 我看不到任何阻塞连接的东西。

堆栈中提到的代码行:

final ClientConfig config= new XmlClientConfigBuilder("config/hazelcast.xml").build();
HazelcastInstance hcast = HazelcastClient.newHazelcastClient(config);   //this is mentioned in stack trace

配置

<hazelcast-client xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-client-config-3.1.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <group>
        <name>dev</name> 
        <password>dev-pass</password> 
    </group>
    <management-center enabled="false">http://localhost:8080/mancenter</management-center>
    <network>
       <cluster-members>
            <address>xxx.xxx.xxx.xxx</address>
        </cluster-members>
        <smart-routing>true</smart-routing>
        <redo-operation>true</redo-operation>
        <connection-pool-size>30</connection-pool-size>

        <port auto-increment="true" port-count="100">5701</port>
        <outbound-ports>
            <ports>0</ports>
        </outbound-ports>
        <join>
            <multicast enabled="false">
                <multicast-group>224.2.2.3</multicast-group>
                <multicast-port>54327</multicast-port>
            </multicast>
            <tcp-ip enabled="false">
                <interface>xxx.xxx.xxx.xxx</interface>
            </tcp-ip>
            <aws enabled="false">
                <access-key>my-access-key</access-key>
                <secret-key>my-secret-key</secret-key>
                <region>us-west-1</region>
                <host-header>ec2.amazonaws.com</host-header>
                <security-group-name>hazelcast-sg</security-group-name>
                <tag-key>type</tag-key>
                <tag-value>hz-nodes</tag-value>
            </aws>
        </join>
        <interfaces enabled="false">
            <interface>10.10.1.*</interface>
        </interfaces>
        <ssl enabled="false" />
        <socket-interceptor enabled="false" />
        <symmetric-encryption enabled="false">
            <algorithm>PBEWithMD5AndDES</algorithm>
            <salt>thesalt</salt>
            <password>thepass</password>
            <iteration-count>19</iteration-count>
        </symmetric-encryption>
    </network>

日志输出

Sep 05, 2014 4:06:02 PM com.hazelcast.core.LifecycleService
INFO: HazelcastClient[hz.client_0_dev][3.2.4] is STARTING
Sep 05, 2014 4:06:02 PM com.hazelcast.core.LifecycleService
INFO: HazelcastClient[hz.client_0_dev][3.2.4] is STARTED
Sep 05, 2014 4:06:02 PM com.hazelcast.core.LifecycleService
INFO: HazelcastClient[hz.client_0_dev][3.2.4] is CLIENT_CONNECTED
Sep 05, 2014 4:06:02 PM com.hazelcast.client.spi.ClientClusterService
INFO: 

Members [1] {
    Member [127.0.0.1]:5701
}

Sep 05, 2014 4:06:22 PM com.hazelcast.client.spi.ClientPartitionService
SEVERE: 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
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrConnect(ClientConnectionManagerImpl.java:319)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.tryToConnect(ClientConnectionManagerImpl.java:261)
    at com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.send(ClientInvocationServiceImpl.java:149)
    at com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:59)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.getPartitionsFrom(ClientPartitionServiceImpl.java:105)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.getInitialPartitions(ClientPartitionServiceImpl.java:94)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.start(ClientPartitionServiceImpl.java:60)
    at com.hazelcast.client.HazelcastClient.start(HazelcastClient.java:223)
    at com.hazelcast.client.HazelcastClient.newHazelcastClient(HazelcastClient.java:186)
    at com.xxx.test.HCastClientAccessor.getHCastInstance(HCastClientAccessor.java:55)
    at com.xxx.test.HCastTest.<clinit>(HCastTest.java:12)
Caused by: java.util.concurrent.ExecutionException: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused: no further information
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:262)
    at java.util.concurrent.FutureTask.get(FutureTask.java:119)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrConnect(ClientConnectionManagerImpl.java:316)
    ... 10 more
Caused by: com.hazelcast.core.HazelcastException: java.net.ConnectException: Connection refused: no further information
    at com.hazelcast.util.ExceptionUtil.rethrow(ExceptionUtil.java:45)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl$ConnectionProcessor.call(ClientConnectionManagerImpl.java:384)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl$ConnectionProcessor.call(ClientConnectionManagerImpl.java:332)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at com.hazelcast.util.executor.CompletableFutureTask.run(CompletableFutureTask.java:57)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
    at com.hazelcast.util.executor.PoolExecutorThreadFactory$ManagedThread.run(PoolExecutorThreadFactory.java:59)
Caused by: java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:708)
    at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:115)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl$ConnectionProcessor.call(ClientConnectionManagerImpl.java:365)
    ... 11 more

服务器输出

INFO: [127.0.0.1]:5701 [dev] [3.2.4] Accepting socket connection from /xxx.xxx.xxx.xxx:49705
Sep 05, 2014 4:05:57 PM com.hazelcast.nio.TcpIpConnectionManager
INFO: [127.0.0.1]:5701 [dev] [3.2.4] 5701 accepted socket connection from /xxx.xxx.xxx.xxx:49705
Sep 05, 2014 4:05:57 PM com.hazelcast.client.AuthenticationRequest
INFO: [127.0.0.1]:5701 [dev] [3.2.4] Received auth from Connection [/xxx.xxx.xxx.xxx:49705 -> null] live=true, client=true, type=JAVA_CLIENT, successfully authenticated
Sep 05, 2014 4:09:43 PM com.hazelcast.nio.TcpIpConnection
INFO: [127.0.0.1]:5701 [dev] [3.2.4] Connection [Address[xxx.xxx.xxx.xxx]:49705] lost. Reason: java.io.IOException[Connection reset by peer]
Sep 05, 2014 4:09:43 PM com.hazelcast.client.ClientEngine
INFO: [127.0.0.1]:5701 [dev] [3.2.4] Destroying ClientEndpoint{conn=Connection [/xxx.xxx.xxx.xxx:49705 -> Address[xxx.xxx.xxx.xxx]:49705] live=false, client=true, type=JAVA_CLIENT, uuid='70afcf60-96e0-444d-8981-3aa983530514', firstConnection=true, authenticated=true}
Sep 05, 2014 4:09:43 PM com.hazelcast.nio.ReadHandler
WARNING: [127.0.0.1]:5701 [dev] [3.2.4] hz._hzInstance_1_dev.IO.thread-in-0 Closing socket to endpoint Address[192.168.101.106]:49705, Cause:java.io.IOException: Connection reset by peer

更新:

我切换到此客户端配置,但客户端仍然出现异常。我复制了服务器和客户端的输出——服务器收到了连接请求,但是在客户端,我看到了与上面提到的相同的错误“严重:获取集群分区表时出错!”——相同的跟踪如上。

新客户端配置

<hazelcast-client xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-client-config-3.2.4.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <group>
        <name>dev</name> 
        <password>dev-pass</password> 
    </group>
    <management-center enabled="false">http://localhost:8080/mancenter</management-center>
    <network>
       <cluster-members>
            <address>xxx.xxx.xxx.xxx</address>
        </cluster-members>
        <smart-routing>true</smart-routing>
        <redo-operation>true</redo-operation>
        <connection-timeout>60000</connection-timeout>
        <connection-attempt-limit>10</connection-attempt-limit>
        <connection-pool-size>30</connection-pool-size>
    </network>
        <executor-pool-size>40</executor-pool-size> <!-- added -->

</hazelcast-client>

服务器输出:

Sep 07, 2014 5:57:01 PM com.hazelcast.nio.SocketAcceptor
INFO: [127.0.0.1]:5701 [dev] [3.2.4] Accepting socket connection from /xxx.xxx.xxx.xxx:58521
Sep 07, 2014 5:57:01 PM com.hazelcast.nio.TcpIpConnectionManager
INFO: [127.0.0.1]:5701 [dev] [3.2.4] 5701 accepted socket connection from /xxx.xxx.xxx.xxx:58521
Sep 07, 2014 5:57:03 PM com.hazelcast.client.AuthenticationRequest
INFO: [127.0.0.1]:5701 [dev] [3.2.4] Received auth from Connection [/xxx.xxx.xxx.xxx:58521 -> null] live=true, client=true, type=JAVA_CLIENT, successfully authenticated

客户端输出

Sep 07, 2014 5:58:04 PM com.hazelcast.client.spi.ClientPartitionService
SEVERE: 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
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrConnect(ClientConnectionManagerImpl.java:319)
    at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.tryToConnect(ClientConnectionManagerImpl.java:261)
    at com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.send(ClientInvocationServiceImpl.java:149)
    at com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:59)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.getPartitionsFrom(ClientPartitionServiceImpl.java:105)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.getInitialPartitions(ClientPartitionServiceImpl.java:94)
    at com.hazelcast.client.spi.impl.ClientPartitionServiceImpl.start(ClientPartitionServiceImpl.java:60)
    at com.hazelcast.client.HazelcastClient.start(HazelcastClient.java:223)
    at com.hazelcast.client.HazelcastClient.newHazelcastClient(HazelcastClient.java:186)

更新

我为客户端和服务器尝试了这个,但我得到了与上面相同的异常 - 我做错了什么:

客户 ClientConfig clientConfig = new ClientConfig().addAddress("xxx.xxx.xxx.xxx"); HazelcastInstance 客户端 = HazelcastClient.newHazelcastClient(clientConfig);

服务器

HazelcastInstance hcast = Hazelcast.newHazelcastInstance();

更新 简而言之,确保服务器和客户端的配置都是正确的(不要将标签从一组混淆到另一组)。对于服务器,确保 hazelcast 服务器正在侦听外部 IP 地址(不是环回地址),没有防火墙设置阻止连接(在客户端、服务器或中间)。感谢 Peter - 我重新开始使用 hazelcast 并享受使用此工具的体验。强烈推荐!

【问题讨论】:

  • Hazelcast 客户端配置无效。它是客户端和服务器配置的混合,例如客户端不存在 部分。你能在你的ide中启用模式验证吗?
  • 感谢 pveentjer - 除了配置和防火墙更改外,接口还需要是服务器应该监听的外部 IP 地址(而不是本地环回地址)

标签: java hazelcast


【解决方案1】:

除了客户端的 XML 问题,我没有发现任何明显的错误。

我通常的做法如下;尝试在同一个JVM上运行服务器和客户端,那么至少你已经确定没有问题了。
在我将一秒钟浪费在网络问题上之前,我总是先验证基础知识。

验证后,请检查以下部分。
我的直觉是防火墙有问题。第一部分包含有关iptables 的配置信息,第二部分包含用于测试网络连接的第二部分。

iptables
如果您使用iptables,可以添加以下规则以允许来自端口 33000-31000 的出站流量:

iptables -A OUTPUT -p TCP --dport 33000:31000 -m state --state NEW -j ACCEPT

并控制从任何地址到端口 5701 的传入流量:

iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 5701 -j ACCEPT

并允许传入的多播流量:

iptables -A INPUT -m pkttype --pkt-type multicast -j ACCEPT

连接测试
如果您因为机器无法加入集群而遇到问题,您可以检查两台机器之间的网络连接。为此,您可以使用名为 iperf 的工具。
在一台机器上执行:

iperf -s -p 5701

这意味着您正在侦听端口 5701。

在另一台机器上执行以下命令:

iperf -c 192.168.1.107 -d -p 5701

您将192.168.1.107 替换为您的第一台机器的IP 地址。
如果你运行命令并得到如下输出:

------------------------------------------------------------
Server listening on TCP port 5701
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
------------------------------------------------------------
Client connecting to 192.168.1.107, TCP port 5701
TCP window size: 59.4 KByte (default)
------------------------------------------------------------
[  5] local 192.168.1.105 port 40524 connected with 192.168.1.107 port 5701
[  4] local 192.168.1.105 port 5701 connected with 192.168.1.107 port 33641
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0-10.2 sec  55.8 MBytes  45.7 Mbits/sec
[  5]  0.0-10.3 sec  6.25 MBytes  5.07 Mbits/sec

您知道两台机器可以相互连接。
但是,如果您看到这样的内容:

Server listening on TCP port 5701
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
connect failed: No route to host.  

那你知道你手上可能有网络连接问题。

【讨论】:

  • 感谢您的回复 - iperf 测试在机器之间工作(我在发布问题之前已经这样做了,但可能已将其排除在帖子之外)。我认为可能导致问题的服务器和/或客户端(如您提到的 - 客户端)的 xml 有问题。您可能已经根据我的问题在 github 上添加了这个 - 我的经验是我没有在文档中遇到客户端 xml 配置(我可能错过了) - 我只注意到配置中的“客户端”部分通过搜索结果。
  • 我意识到它是一个 ClientConfig 而不是一个配置,但我不确定它是否需要一个不同的 XML 配置(我现在很清楚),因为我已经完成了额外的客户端配置设置
  • 我尝试更改客户端配置(上面复制了更新)。我不得不怀疑我是否在做与分区有关的不正确的事情
  • 我已经尝试过启用 partition-group="false" 和 true - 对我来说都没有改变任何东西。接下来,我将简单地尝试 API 调用而不是客户端 XML,但我不确定这是否是问题所在。我也会检查本地设置(我想我在发布这个问题之前做过,我在客户端遇到了同样的问题)。
  • 我尝试了不同的排列 - 我可以在本地连接,但不能从我的桌面连接到服务器(将尝试不同网络上的不同服务器,只是为了勾选所有框)
【解决方案2】:

您提到的错误似乎来自以下 (ClientPartitionServiceImpl),它调用 GetPartitionRequest 并传入地址。您能否检查您作为地址传入的内容(可能需要您使用您正在使用的版本构建 hazelcast)或更详细地检查正在使用的接口/配置文件。

public ClientPartitionServiceImpl(HazelcastClient client) {
    this.client = client;
}

public void start() {
    getInitialPartitions();
    client.getClientExecutionService().scheduleWithFixedDelay(new RefreshTask(), INITIAL_DELAY, PERIOD, TimeUnit.SECONDS);
}

public void refreshPartitions() {
    try {
        client.getClientExecutionService().execute(new RefreshTask());
    } catch (RejectedExecutionException ignored) {
        EmptyStatement.ignore(ignored);
    }
}

private class RefreshTask implements Runnable {
    public void run() {
        if (updating.compareAndSet(false, true)) {
            try {
                final ClientClusterService clusterService = client.getClientClusterService();
                final Address master = clusterService.getMasterAddress();
                final PartitionsResponse response = getPartitionsFrom(master);
                if (response != null) {
                    processPartitionResponse(response);
                }
            } catch (HazelcastInstanceNotActiveException ignored) {
                EmptyStatement.ignore(ignored);
            } finally {
                updating.set(false);
            }
        }
    }
}

private void getInitialPartitions() {
    final ClientClusterService clusterService = client.getClientClusterService();
    final Collection<MemberImpl> memberList = clusterService.getMemberList();
    for (MemberImpl member : memberList) {
        final Address target = member.getAddress();
        PartitionsResponse response = getPartitionsFrom(target);
        if (response != null) {
            processPartitionResponse(response);
            return;
        }
    }
    throw new IllegalStateException("Cannot get initial partitions!");
}

private PartitionsResponse getPartitionsFrom(Address address) {
    try {
        final Future<PartitionsResponse> future =
                client.getInvocationService().invokeOnTarget(new GetPartitionsRequest(), address);
        return client.getSerializationService().toObject(future.get());
    } catch (Exception e) {
        LOGGER.severe("Error while fetching cluster partition table!", e);
    }
    return null;
}

GetPartitionsRequest

public final class GetPartitionsRequest extends CallableClientRequest implements Portable, RetryableRequest {

    @Override
    public Object call() throws Exception {
        InternalPartitionService service = getService();
        service.firstArrangement();
        ClusterService clusterService = getClientEngine().getClusterService();
        Collection<MemberImpl> memberList = clusterService.getMemberList();
        Address[] addresses = new Address[memberList.size()];
        Map<Address, Integer> addressMap = new HashMap<Address, Integer>(memberList.size());
        int k = 0;
        for (MemberImpl member : memberList) {
            Address address = member.getAddress();
            addresses[k] = address;
            addressMap.put(address, k);
            k++;
        }
        InternalPartition[] partitions = service.getPartitions();
        int[] indexes = new int[partitions.length];
        for (int i = 0; i < indexes.length; i++) {
            Address owner = partitions[i].getOwnerOrNull();
            int index = -1;
            if (owner != null) {
                final Integer idx = addressMap.get(owner);
                if (idx != null) {
                    index = idx;
                }

            }
            indexes[i] = index;
        }
        return new PartitionsResponse(addresses, indexes);
    }

    @Override
    public String getServiceName() {
        return InternalPartitionService.SERVICE_NAME;
    }

    @Override
    public int getFactoryId() {
        return ClientPortableHook.ID;
    }

    @Override
    public int getClassId() {
        return ClientPortableHook.GET_PARTITIONS;
    }

    @Override
    public Permission getRequiredPermission() {
        return null;
    }
}

【讨论】:

  • 对此进行调查。我可以在同一台计算机上连接(客户端/服务器代码和配置有效),但每当我尝试从一台计算机连接到另一台计算机时(无论我使用什么机器),都会出现分区异常。
  • 简化您的环境,直到某些东西可行。联网是 Hazelcast 中非常基本的行为,并且已经过足够的测试。它一定是你网络中的东西。
  • @pveentjer 我正在更新我的帖子 - 我尝试了默认客户端和服务器,但我在 v3.2.4 中遇到了同样的异常 - 除非弄错了,否则使用 hazelcast-default.xml 而不是我的配置.为什么这不起作用?
  • 在同一台机器上运行客户端和服务器时是否有效?你确认了吗?
  • 它在同一台计算机上运行 - 当我从一台计算机连接到另一台计算机时会出现问题
【解决方案3】:

只是添加一些情况,我遇到“获取集群分区表时出错”

  • 对于 ipv6:检查 http://docs.hazelcast.org/docs/latest/manual/html/ipv6.html 页面。如果您使用 ipv6,请不要在服务器上将“hazelcast.prefer.ipv4.stack”设置为 true
  • 对于 ipv6:由于某些原因,如果您不设置“”,客户端仍然无法正常工作。对于 ipv4,它与“enabled=false”一起工作。我使用一台机器,不需要集群,但需要连接客户端。魔术:如果使用 ipv4,客户端可以使用“tcp-ip enabled=false”连接到服务器,但如果使用 ipv6,则不能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多