1. ภาพรวม
นี่เป็นบทความแนะนำเกี่ยวกับ Hazelcast ที่เราจะดูวิธีสร้างสมาชิกคลัสเตอร์แผนที่แบบกระจายเพื่อแชร์ข้อมูลระหว่างโหนดคลัสเตอร์และสร้างไคลเอ็นต์ Java เพื่อเชื่อมต่อและสืบค้นข้อมูลในคลัสเตอร์
2. Hazelcast คืออะไร?
Hazelcast เป็นแพลตฟอร์ม In-Memory Data Grid สำหรับ Java สถาปัตยกรรมรองรับความสามารถในการปรับขยายและการกระจายข้อมูลสูงในสภาพแวดล้อมคลัสเตอร์ รองรับการค้นหาโหนดอัตโนมัติและการซิงโครไนซ์อัจฉริยะ
Hazelcast มีให้เลือกหลายรุ่น หากต้องการดูคุณสมบัติของ Hazelcast ทุกรุ่นเราสามารถอ้างอิงได้จากลิงค์ต่อไปนี้ ในบทช่วยสอนนี้เราจะใช้รุ่นโอเพนซอร์ส
ในทำนองเดียวกัน Hazelcast มีคุณสมบัติต่างๆเช่นกระจายโครงสร้างข้อมูลที่กระจาย Compute กระจายแบบสอบถาม ฯลฯ สำหรับวัตถุประสงค์ของบทความนี้เราจะมุ่งเน้นไปที่การกระจายแผนที่
3. การพึ่งพา Maven
Hazelcast มีห้องสมุดที่แตกต่างกันมากมายเพื่อจัดการกับข้อกำหนดต่างๆ เราสามารถหาได้จาก com.hazelcast group ใน Maven Central
อย่างไรก็ตามในบทความนี้เราจะใช้เฉพาะการพึ่งพาหลักที่จำเป็นในการสร้างสมาชิกคลัสเตอร์ Hazelcast แบบสแตนด์อโลนและไคลเอ็นต์ Hazelcast Java:
com.hazelcast hazelcast 4.0.2
เวอร์ชันปัจจุบันพร้อมใช้งานในที่เก็บส่วนกลางของ maven
4. แอปพลิเคชั่น Hazelcast ครั้งแรก
4.1. สร้างสมาชิก Hazelcast
สมาชิก (หรือที่เรียกว่าโหนด) จะรวมกลุ่มกันโดยอัตโนมัติเพื่อสร้างคลัสเตอร์ การเข้าร่วมอัตโนมัตินี้เกิดขึ้นพร้อมกับกลไกการค้นพบต่างๆที่สมาชิกใช้เพื่อค้นหาซึ่งกันและกัน
มาสร้างสมาชิกที่เก็บข้อมูลในแผนที่แบบกระจายของ Hazelcast:
public class ServerNode { HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(); ... }
เมื่อเราเริ่มแอปพลิเคชันServerNodeเราจะเห็นข้อความต่อเนื่องในคอนโซลซึ่งหมายความว่าเราสร้างโหนด Hazelcast ใหม่ใน JVM ของเราซึ่งจะต้องเข้าร่วมคลัสเตอร์
Members [1] { Member [192.168.1.105]:5701 - 899898be-b8aa-49aa-8d28-40917ccba56c this }
ในการสร้างโหนดหลายโหนดเราสามารถเริ่มแอปพลิเคชันServerNodeหลายอินสแตนซ์ได้ ด้วยเหตุนี้ Hazelcast จะสร้างและเพิ่มสมาชิกใหม่ในคลัสเตอร์โดยอัตโนมัติ
ตัวอย่างเช่นหากเราเรียกใช้แอปพลิเคชันServerNodeอีกครั้งเราจะเห็นบันทึกต่อไปนี้ในคอนโซลซึ่งระบุว่ามีสมาชิกสองคนในคลัสเตอร์
Members [2] { Member [192.168.1.105]:5701 - 899898be-b8aa-49aa-8d28-40917ccba56c Member [192.168.1.105]:5702 - d6b81800-2c78-4055-8a5f-7f5b65d49f30 this }
4.2. สร้างแผนที่แบบกระจาย
ต่อไปมาสร้างแผนที่แบบกระจาย เราต้องการอินสแตนซ์ของHazelcastInstance ที่สร้างขึ้นก่อนหน้านี้เพื่อสร้างแผนที่แบบกระจายซึ่งขยายส่วนต่อประสานjava.util.concurrent.ConcurrentMap
Map map = hazelcastInstance.getMap("data"); ...
ในที่สุดเรามาเพิ่มรายการลงในแผนที่ :
FlakeIdGenerator idGenerator = hazelcastInstance.getFlakeIdGenerator("newid"); for (int i = 0; i < 10; i++) { map.put(idGenerator.newId(), "message" + i); }
ในฐานะที่เราสามารถมองเห็นข้างต้นเราได้เพิ่ม 10 รายการกับแผนที่ เราใช้FlakeIdGeneratorเพื่อให้แน่ใจว่าเราได้รับคีย์เฉพาะสำหรับแผนที่ สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับFlakeIdGeneratorเราสามารถดูได้จากลิงค์ต่อไปนี้
แม้ว่านี่อาจไม่ใช่ตัวอย่างในโลกแห่งความเป็นจริง แต่เราใช้เพื่อแสดงให้เห็นการดำเนินการอย่างใดอย่างหนึ่งที่เราสามารถนำไปใช้กับแผนที่แบบกระจาย ในภายหลังเราจะดูวิธีการดึงรายการที่เพิ่มโดยสมาชิกคลัสเตอร์จากไคลเอ็นต์ Hazelcast Java
ภายใน Hazelcast จะแบ่งพาร์ติชันรายการแผนที่และแจกจ่ายและจำลองรายการระหว่างสมาชิกคลัสเตอร์ สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ Hazelcast Mapเราสามารถดูได้จากลิงค์ต่อไปนี้
4.3. สร้างไคลเอ็นต์ Hazelcast Java
ไคลเอ็นต์ Hazelcast ช่วยให้เราดำเนินการ Hazelcast ทั้งหมดได้โดยไม่ต้องเป็นสมาชิกของคลัสเตอร์ มันเชื่อมต่อกับหนึ่งในสมาชิกคลัสเตอร์และมอบหมายการดำเนินการทั้งคลัสเตอร์ทั้งหมดให้กับมัน
มาสร้างไคลเอนต์ดั้งเดิม:
ClientConfig config = new ClientConfig(); config.setClusterName("dev"); HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
ง่ายๆแค่นั้นเอง
4.4. เข้าถึงแผนที่กระจายจากไคลเอนต์ Java
Next, we'll use the instance of HazelcastInstance created earlier to access the distributed Map:
Map map = hazelcastInstanceClient.getMap("data"); ...
Now we can do operations on a map without being a member of the cluster. For example, let's try to iterate over the entries:
for (Entry entry : map.entrySet()) { ... }
5. Configuring Hazelcast
In this section, we'll focus on how to configure the Hazelcast network using declaratively (XML) and programmatically (API) and use the Hazelcast management center to monitor and manage nodes that are running.
While Hazelcast is starting up, it looks for a hazelcast.config system property. If it's set, its value is used as the path. Otherwise, Hazelcast searches for a hazelcast.xml file in the working directory or on the classpath.
If none of the above works, Hazelcast loads the default configuration, i.e. hazelcast-default.xml that comes with hazelcast.jar.
5.1. Network Configuration
By default, Hazelcast uses multicast for discovering other members that can form a cluster. If multicast isn't a preferred way of discovery for our environment, then we can configure Hazelcast for a full TCP/IP cluster.
Let's configure the TCP/IP cluster using declarative configuration:
Alternatively, we can use the Java config approach:
Config config = new Config(); NetworkConfig network = config.getNetworkConfig(); network.setPort(5701).setPortCount(20); network.setPortAutoIncrement(true); JoinConfig join = network.getJoin(); join.getMulticastConfig().setEnabled(false); join.getTcpIpConfig() .addMember("machine1") .addMember("localhost").setEnabled(true);
By default, Hazelcast will try 100 ports to bind. In the example above, if we set the value of port as 5701 and limit the port count to 20, as members are joining the cluster, Hazelcast tries to find ports between 5701 and 5721.
If we want to choose to use only one port, we can disable the auto-increment feature by setting auto-increment to false.
5.2. Management Center Configuration
The management center allows us to monitor the overall state of the clusters, we can also analyze and browse the data structures in detail, update map configurations, and take thread dump from nodes.
To use the Hazelcast management center, we can either deploy the mancenter-version.war application into our Java application server/container or we can start Hazelcast Management Center from the command line. We can download the latest Hazelcast ZIP from hazelcast.org. The ZIP contains the mancenter-version.war file.
We can configure our Hazelcast nodes by adding the URL of the web application to hazelcast.xml and then have the Hazelcast members communicate with the management center.
So let's now configure the management center using declarative configuration:
//localhost:8080/mancenter
Likewise, here's the programmatic configuration:
ManagementCenterConfig manCenterCfg = new ManagementCenterConfig(); manCenterCfg.setEnabled(true).setUrl("//localhost:8080/mancenter");
6. Conclusion
In this article, we covered introductory concepts about Hazelcast. For more details, we can take a look at the Reference Manual.
As usual, all the code for this article is available over on GitHub.