如何创建hazelcast实例 [英] how to create hazelcast instance

查看:64
本文介绍了如何创建hazelcast实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用客户端和服务器设置(而不是对等)使用hazelcast.我应该使用单例模式创建hazelcast客户端实例还是实例(我使用的是惰性习语,无论如何都可能需要另一种外观).我应该使用单例还是仅致电 HazelcastInstance hz = Hazelcast. newHazelcastInstance(); 何时需要一个实例?

I am using hazelcast using the client and server setup (not peer to peer). Should I create an instance of hazelcast client and instance using a singleton pattern (I am using lazy idiom which probably needs another look in any case). Should I use a singleton or just call HazelcastInstance hz = Hazelcast . newHazelcastInstance (); to get an instance whenever I need one?

服务器实施中使用的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,但是在同一个类中.

I am accessing multiple IMaps after that but in the same class.

日志语句:

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

推荐答案

您需要了解的是每次调用

What you need to understand is that each invocation of

HazelcastInstance hz = Hazelcast.newHazelcastInstance ();

将精确地创建-在JVM中创建一个新的hazelcast节点(是的,您可以在一个JVM中包含多个hazelcast节点),这将必须加入集群等.除非您有相应的用例(例如,在单元测试中您可以在单个节点上设置内存集群的时候非常有用!我猜另一个用例是如果您需要更改配置(据我所知它仍然无法在运行时更改),您不应该不必经常调用它,只需对其进行缓存(例如,在您的那个单例中).

Will create exactly that - a new hazelcast node in your JVM (yes, you can have multiple hazelcast nodes inside one JVM), which will have to join the cluster etc. Unless you have a use case for that (for instance this is very useful during unit tests where you can set up your in-memory cluster on a single node! I guess another use case would be if you need to change your configuration, from what I know it still cannot be changed in runtime) you shouldn't call it too often and simply cache it (for instance in that singleton of yours).

@Edit:您正在创建HazelcastInstance的多个实例.实际上,您只会有一个LazyHolder.INSTANCE实例,但是您不会在HCastAccessor中存储hcast,因此每次调用HCastAccessor.getAccessorInstance().getHCastInstance()都会创建一个新对象.仅当hcast为null时,才需要将hcast放入HCastAccessor并创建一个新的hazelcast实例,否则将其返回.

@ you are creating multiple instances of HazelcastInstance. You will have indeed only one instance of LazyHolder.INSTANCE but you don't store hcast in HCastAccessor so every time you call HCastAccessor.getAccessorInstance().getHCastInstance() it will create a new object. You need to put hcast inside HCastAccessor and create a new hazelcast instance only if hcast is null, otherwise you return it.

请注意,您要么需要同步整个getHCastInstance()方法,要么执行带有同步的双重nullcheck(检查null,仅同步构造部分,在其中进行同步,然后在实际创建之前再次对null进行同步检查),或者它不是线程安全的.像这样:

Take care that you either need to synchronize the whole getHCastInstance() method or do a double nullcheck with synchronization (check for null, synchronize only the construction part, inside that synchronize check for null once again before doing the actual creation) or it won't be thread safe. Something like:

if(hcas == null) {
  synchronize(this) {
    if(hcas == null) {
      // create the instance here
    }
  }
}

这篇关于如何创建hazelcast实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆