【发布时间】:2014-09-05 17:25:12
【问题描述】:
我正在通过客户端和服务器设置(不是点对点)使用 hazelcast。我是否应该使用单例模式创建一个 hazelcast 客户端实例和实例(我使用的是惰性习语,在任何情况下都可能需要再看一下)。我应该使用单例还是只调用 HazelcastInstance hz = Hazelcast 。 newHazelcastInstance (); 在我需要的时候获取实例?
在服务器实现中使用的 Hazelcast 访问器:
public class HCastAccessor {
private static final Logger Logger = LoggerFactory.getLogger(HCastAccessor.class.getName());
// private volatile static HCastAccessor instance = null;
private HCastAccessor() {
}
//lazy loaded holder idiom
private static class LazyHolder {
private static final HCastAccessor INSTANCE = new HCastAccessor();
}
public static HCastAccessor getAccessorInstance() {
return LazyHolder.INSTANCE;
}
public HazelcastInstance getHCastInstance() {
HazelcastInstance hcast = null;
try {
final Config config = new FileSystemXmlConfig("resources/hazelcast.xml");
hcast = Hazelcast.newHazelcastInstance(config);
}
catch (FileNotFoundException fe) {
Logger.error("File not found for Hazelcast:{}" + fe.getMessage());
}
catch (Exception e) {
Logger.error("Exception while starting Hazelcast server:{}" + e.getMessage());
}
return hcast;
}
}
检索到的榛树参考:
final HCastAccessor hcastAccessor = HCastAccessor.getAccessorInstance();
HazelcastInstance hcast = hcastAccessor.getHCastInstance(); // handle shutdown as well
之后我访问了多个 IMap,但在同一个类中。
日志语句:
2014-09-05 05:15:29,473 INFO c.a.w.StartHcastServer [main] Starting Hazelcast Server on 2014/09/05 05:15:29
2014-09-05 05:15:30,105 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:30,113 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5701, using socket ServerSocket[addr=/0.0.0.0,localport=5701], bind any local is true
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5701
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:30,336 INFO c.h.i.Node [main] [127.0.0.1]:5701 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:30,343 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTING
2014-09-05 05:15:30,446 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:30,449 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702
2014-09-05 05:15:30,454 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5702, timeout: 0, bind-any: true
2014-09-05 05:15:30,464 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5702. Reason: SocketException[Connection re
fused to address /127.0.0.1:5702]
2014-09-05 05:15:31,450 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4]
Members [1] {
Member [127.0.0.1]:5701 this
}
2014-09-05 05:15:31,482 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTED
2014-09-05 05:15:31,488 INFO c.a.c.n.SendServerEmail [main] hazelcast server email notification for server 192.168.110.154 WildMetrix Hazelcast server launched at 192.168.110.154
2014-09-05 05:15:32,421 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.cached.thread-1] [127.0.0.1]:5701 [dev] [3.2.4] Initializing cluster partition table first arrangement...
2014-09-05 05:15:32,615 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:32,616 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5702, using socket ServerSocket[addr=/0.0.0.0,localport=5702], bind any local is true
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5702
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:32,691 INFO c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:32,692 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTING
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5701, timeout: 0, bind-any: true
2014-09-05 05:15:32,708 INFO c.h.n.SocketAcceptor [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] Accepting socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] 5701 accepted socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] 38086 accepted socket connection from /127.0.0.1:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Couldn't join to the master : Address[127.0.0.1]:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:09,723 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:55,733 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:55,734 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:31,743 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 5
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:18:07,751 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 6
2014-09-05 05:18:33,760 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Join try count exceed limit, setting this node as master!
2014-09-05 05:18:33,760 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4]
Members [1] {
Member [127.0.0.1]:5702 this
}
2014-09-05 05:18:33,760 WARN c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2014-09-05 05:18:33,761 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTED
2014-09-05 05:18:33,777 INFO c.h.c.ClusterService [hz._hzInstance_1_dev.global-operation.thread-4] [127.0.0.1]:5701 [dev] [3.2.4]
Members [2] {
Member [127.0.0.1]:5701 this
Member [127.0.0.1]:5702
}
2014-09-05 05:18:33,784 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,826 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-1] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,827 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.migration] [127.0.0.1]:5701 [dev] [3.2.4] Re-partitioning cluster data... Migration queue size: 135
2014-09-05 05:18:34,132 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:18:34,133 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5703, using socket ServerSocket[addr=/0.0.0.0,localport=5703], bind any local is true
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5703
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:18:34,207 INFO c.h.i.Node [main] [127.0.0.1]:5703 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:18:34,208 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5703 [dev] [3.2.4] Address[127.0.0.1]:5703 is STARTING
2014-09-05 05:18:34,228 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5703 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702
【问题讨论】: