java 连接到服务器时,hazelcast 客户端异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25700010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hazelcast client exception when connecting to server
提问by
I am trying to use hazelcast v3.2.4 (same version on server and client). The server (as simple an implementation that I could put in place) is running on a server. The client tries to connect to the remote server - the server prints the authentication requests but I receive the following log outputs (including exceptions) - any ideas on what I can do differently (copying both log output and config file). I am trying to connect via TCP/IP and I checked for network connectivity - I could not see anything blocking the connection.
我正在尝试使用 hazelcast v3.2.4(服务器和客户端上的相同版本)。服务器(作为我可以放置的简单实现)正在服务器上运行。客户端尝试连接到远程服务器 - 服务器打印身份验证请求,但我收到以下日志输出(包括异常) - 关于我可以做些什么的任何想法(复制日志输出和配置文件)。我正在尝试通过 TCP/IP 进行连接,并检查了网络连接 - 我没有看到任何阻止连接的东西。
Line of Code mentioned in stack:
堆栈中提到的代码行:
final ClientConfig config= new XmlClientConfigBuilder("config/hazelcast.xml").build();
HazelcastInstance hcast = HazelcastClient.newHazelcastClient(config); //this is mentioned in stack trace
Config
配置
<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>
Log output
日志输出
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
Server output
服务器输出
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
Update:
更新:
I switched to this client config but I still get an exception on the client end. I copied the server and client output - the server receives the connection request but then on the client end, I see the same error as mentioned above "SEVERE: Error while fetching cluster partition table!"- same trace as above.
我切换到此客户端配置,但在客户端仍然出现异常。我复制了服务器和客户端的输出——服务器收到了连接请求,但在客户端,我看到了与上面提到的相同的错误“严重:获取集群分区表时出错!” - 与上述相同的痕迹。
New client config
新客户端配置
<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>
Server output:
服务器输出:
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
Client output
客户端输出
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)
Update
更新
I tried this for client and server and I got the same exception as above - what am I doing wrong:
我为客户端和服务器尝试了这个,我得到了与上面相同的异常 - 我做错了什么:
ClientClientConfig clientConfig = new ClientConfig().addAddress("xxx.xxx.xxx.xxx"); HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
ClientClientConfig clientConfig = new ClientConfig().addAddress("xxx.xxx.xxx.xxx"); HazelcastInstance 客户端 = HazelcastClient.newHazelcastClient(clientConfig);
Server
服务器
HazelcastInstance hcast = Hazelcast.newHazelcastInstance();
UpdateIn short, make sure the config for both server and client are correct (do not mix up tags from one set to another). For the server, ensure that the hazelcast server is listening on the external IP address (not the loopback addresS), that there are no firewall settings blocking the connection (on client, server or in-between). Thanks to Peter - I'm back to using hazelcast and enjoying my experience of using this tool. Strongly recommended!
更新简而言之,确保服务器和客户端的配置都是正确的(不要将一组标签混淆到另一组)。对于服务器,确保 hazelcast 服务器正在侦听外部 IP 地址(而不是环回地址),没有防火墙设置阻止连接(在客户端、服务器或中间)。感谢彼得 - 我又回到了使用 hazelcast 并享受使用这个工具的体验。强烈推荐!
采纳答案by pveentjer
Apart from the XML problems for the client, I don't see anything obvious wrong.
除了客户端的 XML 问题之外,我没有看到任何明显的错误。
My usual approach is the following; try to run server and client on the same JVM, then at least you have made sure that there is no problem. I always verify the basics before I'm going to waste a second on network problems.
我通常的做法如下;尝试在同一个 JVM 上运行服务器和客户端,那么至少你已经确定没有问题。在我打算在网络问题上浪费一秒钟之前,我总是先验证基础知识。
Once you have verified that, check the following section. My gut feeling is that there is something with the firewall. The first section contains configuration information about iptables, the second contains a second to test the network connection.
确认后,请检查以下部分。我的直觉是防火墙有问题。第一部分包含有关 iptables 的配置信息,第二部分包含用于测试网络连接的第二部分。
\subsection{iptables} If you are making use of iptables, the following rule can be added to allow for outbound traffic from ports 33000-31000: \begin{lstlisting} iptables -A OUTPUT -p TCP --dport 33000:31000 -m state --state NEW -j ACCEPT \end{lstlisting} and to control incoming traffic from any address to port 5701: \begin{lstlisting} iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 5701 -j ACCEPT \end{lstlisting} and to allow incoming multicast traffic: \begin{lstlisting} iptables -A INPUT -m pkttype --pkt-type multicast -j ACCEPT \end{lstlisting}
\subsection{iptables} 如果您使用 iptables,可以添加以下规则以允许来自端口 33000-31000 的出站流量: \begin{lstlisting} iptables -A OUTPUT -p TCP --dport 33000:31000 -m state --state NEW -j ACCEPT \end{lstlisting} 并控制从任何地址到端口 5701 的传入流量: \begin{lstlisting} iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 5701 -j ACCEPT \end{lstlisting} 并允许传入的多播流量:\begin{lstlisting} iptables -A INPUT -m pkttype --pkt-type multicast -j ACCEPT \end{lstlisting}
\section{Connectivity test} If you are having troubles because machines won't join a cluster, you might check the network connectity between the 2 machines. You can use a tool called iperf for that. On one machine you execute: \begin{lstlisting} iperf -s -p 5701 \end{lstlisting} This means that you are listening at port 5701.
\section{连接测试} 如果您因为机器无法加入集群而遇到问题,您可以检查两台机器之间的网络连接。为此,您可以使用名为 iperf 的工具。在一台机器上你执行: \begin{lstlisting} iperf -s -p 5701 \end{lstlisting} 这意味着你正在监听端口 5701。
At the other machine you execute the following command: \begin{lstlisting} iperf -c 192.168.1.107 -d -p 5701 \end{lstlisting} Where you replace '192.168.1.107' by the ip address of your first machine. If you run the command and you get output like this:
在另一台机器上执行以下命令: \begin{lstlisting} iperf -c 192.168.1.107 -d -p 5701 \end{lstlisting} 在这里你用第一台机器的 IP 地址替换 '192.168.1.107'。如果您运行该命令并获得如下输出:
\begin{lstlisting}
\begin{lstlisting}
Server listening on TCP port 5701
服务器侦听 TCP 端口 5701
TCP window size: 85.3 KByte (default)
TCP 窗口大小:85.3 KB(默认)
Client connecting to 192.168.1.107, TCP port 5701
客户端连接到 192.168.1.107,TCP 端口 5701
TCP window size: 59.4 KByte (default)
TCP 窗口大小:59.4 KB(默认)
[ 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 \end{lstlisting} You know the 2 machines can connect to each other. However if you are seeing something like this: \begin{lstlisting} Server listening on TCP port 5701
[5]本地192.168.1.105端口40524连接192.168.1.107端口5701 [4]本地192.168.1.105端口5701连接192.168.1.107端口ID.33641.M.50s.50s7s0405s.1.105秒[405秒]Bandwidth sec [ 5] 0.0-10.3 sec 6.25 MBytes 5.07 Mbits/sec \end{lstlisting} 你知道两台机器可以相互连接。但是,如果您看到类似这样的内容:\begin{lstlisting} 服务器正在侦听 TCP 端口 5701
TCP window size: 85.3 KByte (default)
TCP 窗口大小:85.3 KB(默认)
connect failed: No route to host \end{lstlisting} Then you know that you might have a network connection problem on your hands.
连接失败:没有到主机 \end{lstlisting} 的路由 然后你知道你手头上可能有网络连接问题。
回答by ali haider
The error you're mentioning appears to come from the following (ClientPartitionServiceImpl) where it calls the GetPartitionRequest and passes in the address. Can you check what you are passing in as address (may require for you to build hazelcast with the version you're using) or check the interfaces/config file being used in more detail.
您提到的错误似乎来自以下 (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;
}
}
回答by bitec
Just will add some situations, that I faced for the "Error while fetching cluster partition table"
只是会添加一些情况,我面临的“获取集群分区表时出错”
- for ipv6: check http://docs.hazelcast.org/docs/latest/manual/html/ipv6.htmlpage. If you use ipv6 don't foget to set "hazelcast.prefer.ipv4.stack" to true on server
- for ipv6: for some reasons everything still doesn't work for client if you don't set "" For ipv4 it works with "enabled=false". I use one single machine and don't need the cluster, but need clients connected. Magic: client can connect to server with "tcp-ip enabled=false" if using ipv4, but cannot if using ipv6.
- 对于 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,则不能。