- Hazelcast 教程
- Hazelcast - 首页
- Hazelcast - 简介
- Hazelcast - 设置
- Hazelcast - 初次应用
- Hazelcast - 配置
- 设置多节点实例
- Hazelcast - 数据结构
- Hazelcast - 客户端
- Hazelcast - 序列化
- Hazelcast 高级版
- Hazelcast - Spring 集成
- Hazelcast - 监控
- Map Reduce 和聚合
- Hazelcast - 集合监听器
- 常见陷阱和性能技巧
- Hazelcast 实用资源
- Hazelcast - 快速指南
- Hazelcast - 实用资源
- Hazelcast - 讨论
Hazelcast - 初次应用
Hazelcast 可以独立(单节点)运行,也可以运行多个节点以形成一个集群。让我们首先尝试启动单个实例。
单个实例
示例
现在,让我们尝试创建并使用单个 Hazelcast 集群实例。为此,我们将创建 SingleInstanceHazelcastExample.java 文件。
package com.example.demo; import java.util.Map; import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; public class SingleInstanceHazelcastExample { public static void main(String... args){ //initialize hazelcast server/instance HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(); System.out.println(“Hello world”); // perform a graceful shutdown hazelcast.shutdown(); } }
现在让我们编译代码并执行它 −
mvn clean install java -cp target/demo-0.0.1-SNAPSHOT.jar com.example.demo.SingleInstanceHazelcastExample
输出
如果你执行上面的代码,输出将是 −
Hello World
然而,更重要的是,你还会注意到 Hazelcast 的日志行,它表示 Hazelcast 已启动。由于我们只运行此代码一次,即一个 JVM,因此我们的集群中只有一个成员。
Jan 30, 2021 10:26:51 AM com.hazelcast.config.XmlConfigLocator INFO: Loading 'hazelcast-default.xml' from classpath. Jan 30, 2021 10:26:51 AM com.hazelcast.instance.AddressPicker INFO: [LOCAL] [dev] [3.12.12] Prefer IPv4 stack is true. Jan 30, 2021 10:26:52 AM com.hazelcast.instance.AddressPicker INFO: [LOCAL] [dev] [3.12.12] Picked [localhost]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true Jan 30, 2021 10:26:52 AM com.hazelcast.system ... Members {size:1, ver:1} [ Member [localhost]:5701 - 9b764311-9f74-40e5-8a0a-85193bce227b this ] Jan 30, 2021 10:26:56 AM com.hazelcast.core.LifecycleService INFO: [localhost]:5701 [dev] [3.12.12] [localhost]:5701 is STARTED ... You will also notice log lines from Hazelcast at the end which signifies Hazelcast was shutdown: INFO: [localhost]:5701 [dev] [3.12.12] Hazelcast Shutdown is completed in 784 ms. Jan 30, 2021 10:26:57 AM com.hazelcast.core.LifecycleService INFO: [localhost]:5701 [dev] [3.12.12] [localhost]:5701 is SHUTDOWN
集群:多实例
现在,让我们创建 MultiInstanceHazelcastExample.java 文件(如下所示),用于多实例集群。
package com.example.demo; import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; public class MultiInstanceHazelcastExample { public static void main(String... args) throws InterruptedException{ //initialize hazelcast server/instance HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(); //print the socket address of this member and also the size of the cluster System.out.println(String.format("[%s]: No. of hazelcast members: %s", hazelcast.getCluster().getLocalMember().getSocketAddress(), hazelcast.getCluster().getMembers().size())); // wait for the member to join Thread.sleep(30000); //perform a graceful shutdown hazelcast.shutdown(); } }
让我们在两个不同的外壳上执行以下命令 −
java -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.MultiInstanceHazelcastExample
你将在第一个外壳上注意到 Hazelcast 实例已启动且成员已分配。注意输出的最后一行指出使用端口 5701 的单个成员。
Jan 30, 2021 12:20:21 PM com.hazelcast.internal.cluster.ClusterService INFO: [localhost]:5701 [dev] [3.12.12] Members {size:1, ver:1} [ Member [localhost]:5701 - b0d5607b-47ab-47a2-b0eb-6c17c031fc2f this ] Jan 30, 2021 12:20:21 PM com.hazelcast.core.LifecycleService INFO: [localhost]:5701 [dev] [3.12.12] [localhost]:5701 is STARTED [/localhost:5701]: No. of hazelcast members: 1
你将在第二个外壳上注意到 Hazelcast 实例已加入第一个实例。注意输出的最后一行指出现在使用端口 5702 的有两个成员。
INFO: [localhost]:5702 [dev] [3.12.12] Members {size:2, ver:2} [ Member [localhost]:5701 - b0d5607b-47ab-47a2-b0eb-6c17c031fc2f Member [localhost]:5702 - 037b5fd9-1a1e-46f2-ae59-14c7b9724ec6 this ] Jan 30, 2021 12:20:46 PM com.hazelcast.core.LifecycleService INFO: [localhost]:5702 [dev] [3.12.12] [localhost]:5702 is STARTED [/localhost:5702]: No. of hazelcast members: 2
广告