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

Hazelcast: right way to deploy on cluster

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

我有

我要

应用户请求,搜索 5 台工作机器中每台机器的数据,并将 return 结果提供给用户。 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 启动应用程序)?

你可以简单地拥有一个脚本,它可以 运行 你的主要 class 包含节点启动代码,那么多次。

了解你的用例,我已经给出了一个示例代码,用于创建集群并从你的 REST 客户端中的驱动程序 class 向所有节点提交任务。

运行 下面 class 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 ***********");
    }

}

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

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 客户端的驱动程序 class:

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>