Hazelcast:在集群上部署的正确方法

时间:2017-03-08 13:58:33

标签: java configuration distributed-computing hazelcast

在一台REST服务器和5台工作机集群上部署Hazelcast的正确方法是什么?我应该在REST服务器上启动Hazelcast 5服务器实例(每个工作者一个)和1个HazelcastClient吗?

我有

  • 一台REST服务器计算机,负责处理所有用户请求;
  • 群集中有五台工作人员计算机,每台计算机都会在本地文件系统中保留一些数据。这些数据肯定会让它们保持在RAM中,我需要Hazelcast才能通过集群分发我的搜索查询。

我想要

根据用户请求,搜索5台工作机器中每台机器的数据并将结果返回给用户。 REST服务器机器将接受用户请求,而REST服务器将向集群中的每个工作程序发送搜索MultiTask。类似的东西:

public MySearchResult handleUserSearchRequest(String query) {
    MultiTask<String> task = new MultiTask<String>(query, Hazelcast.getCluster().getMembers());
    ExecutorService executorService = Hazelcast.getExecutorService();
    executorService.execute(task);
    Collection<String> results = task.get();
    return results.stream().reduce(/*some logic*/);
}

P.S。

如何从单个位置启动所有6个Hazelcast实例(Spring Boot应用程序)?

1 个答案:

答案 0 :(得分:4)

你可以简单地创建一个脚本来运行包含节点启动代码的主类,这些脚本很多次。

了解您的用例,我已经提供了一个示例代码,用于创建集群并将任务从您的案例REST客户端中的Driver类提交给所有节点。

运行以下类5次,以在TCP / IP配置下创建5个节点的集群。

public class WorkerNode {

    public static void main(String[] args){

        /*
        Create a new Hazelcast node.
        Get the configurations from Hazelcast.xml in classpath or default one from jar
         */
        HazelcastInstance workerNode = Hazelcast.newHazelcastInstance();

        System.out.println("*********** Started a WorkerNode ***********");
    }

}

这是NodeTask,包含执行IO操作的业务逻辑。

public class NodeTask implements Callable<Object>, HazelcastInstanceAware, Serializable {

    private transient HazelcastInstance hazelcastInstance;

    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
        this.hazelcastInstance = hazelcastInstance;
    }

    public Object call() throws Exception {

        Object returnableObject = "testData";

        //Do all the IO operations here and set the returnable object

        System.out.println("Running the NodeTask on a Hazelcast Node: " + hazelcastInstance.getName());

        return returnableObject;
    }
}

以下是REST客户端的驱动程序类:

public class Driver {

    public static void main(String[] args) throws Exception {

        HazelcastInstance client = HazelcastClient.newHazelcastClient();

        IExecutorService executor = client.getExecutorService("executor");
        Map<Member, Future<Object>> result = executor.submitToAllMembers(new NodeTask());

        for (Future<Object> future : result.values()) {
            /*
            Aggregation logic goes here.
             */
            System.out.println("Returned data from node: " + future.get());
        }

        client.shutdown();

        System.exit(0);
    }
}

示例Hazelcast.xml配置:

<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.8.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <network>
        <port auto-increment="true" port-count="100">5701</port>
        <join>
            <multicast enabled="false">
                <multicast-group>224.2.2.3</multicast-group>
                <multicast-port>54327</multicast-port>
            </multicast>
            <tcp-ip enabled="true">
                <!--Replace this with the IP addresses of the servers -->
                <interface>127.0.0.1</interface>
            </tcp-ip>
            <aws enabled="false"/>
        </join>
        <interfaces enabled="false">
            <interface>127.0.0.1</interface>
        </interfaces>
    </network>
</hazelcast>

Architecture