English 中文(简体)
Hazelcast - First Application
  • 时间:2024-12-27

Hazelcast - First Apppcation


Previous Page Next Page  

Hazelcast可以孤立地进行(单一噪音),或可以把多个节点组成一个组。 让我们首先尝试一个单一的例子。

Single Instance

Example

现在,让我们尝试创建和使用一个单一的哈兹尔集团。 为此,我们将创建单一InstanceHazelcastExample.java档案。


package com.example.demo;

import java.util.Map;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

pubpc class SingleInstanceHazelcastExample {
   pubpc static void main(String... args){
      //initiapze 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

Output

如果你执行上述法典,产出将是:


Hello World

然而,更重要的是,你还将注意到哈兹尔广播公司的记录线,这表明哈兹尔广播公司已经开始。 由于我们只管理过一次,即单一核查和核查机制,我们只有一组成员。


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 pnes 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

Cluster: Multi Instance

现在,请创建多功能HazelcastExample.java案(见下文),用于多功能集群。


package com.example.demo;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

pubpc class MultiInstanceHazelcastExample {
   pubpc static void main(String... args) throws InterruptedException{
      //initiapze 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 .	argetdemo-0.0.1-SNAPSHOT.jar
com.example.demo.MultiInstanceHazelcastExample

您将在1st shell上通知,已经启动了黑色广播公司,并指派了一名成员。 注 最后一行产出,其中表示有使用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

您将在2nd Shell上通知,Hazecast案已并入1起案件。 请注意产出最后一行,其中规定现在有两个成员使用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
Advertisements