Hazelcast 简明教程

Hazelcast - Setting up multi node instances

鉴于 Hazelcast 是一个分布式的 IMDG,并且通常在多台计算机上设置,因此它需要访问内部/外部网络。最重要的用例是在集群内发现 Hazelcast 节点。

Given that Hazelcast is a distributed IMDG and typically is set up on multiple machines, it requires access to the internal/external network. The most important use-case being discovery of Hazelcast nodes within a cluster.

Hazelcast 需要以下端口 -

Hazelcast requires the following ports −

  1. 1 inbound port to receive pings/data from other Hazelcast nodes/clients

  2. n number of outbound ports which are required to send ping/data to other members of the cluster.

此节点发现以几种方式发生 -

This node discovery happens in few ways −

  1. Multicast

  2. TCP/IP

  3. Amazon EC2 auto discovery

其中,我们将了解多播和 TCP/IP

Of this, we will look at Multicast and TCP/IP

Multicast

默认情况下启用多播连接机制。 https://en.wikipedia.org/wiki/Multicast 是一种通信形式,其中消息会发送到组中的所有节点。这是 Hazelcast 用于发现集群中其他成员的方式。早前我们已了解到,所有示例均使用多播来发现成员。

Multicast joining mechanism is enabled by default. https://en.wikipedia.org/wiki/Multicast is a way of communication form in which message is transmitted to all the nodes in a group. And this is what Hazelcast uses to discover other members of the cluster. All the examples that we have looked at earlier use multicast to discover members.

Example

现在,让我们明确启用它。在 hazelcast-multicast.xml 中添加以下内容:

Let’s now explicitly turn it on. Save the following in hazelcast-multicast.xml

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

   <network>
      <join>
         <multicast enabled="true" />
      </join>
   </network>
</hazelcast>

然后,让我们执行以下内容 -

And then, let us execute the following −

java -Dhazelcast.config=hazelcast-multicast.xml -cp .\target\demo-0.0.1-
SNAPSHOT.jar com.example.demo.XMLConfigLoadExample

Output

在输出中,我们注意到以下来自 Hazelcast 的行,这实际上表示使用多播连接器来发现成员。

In the output, we notice the following lines from Hazelcast which effectively means that multicast joiner is used to discover the members.

Jan 30, 2021 5:26:15 PM com.hazelcast.instance.Node
INFO: [localhost]:5701 [dev] [3.12.12] Creating MulticastJoiner

默认情况下,多播接受来自多播组内所有计算机的通信。这可能是一个安全隐患,这就是为什么通常在内部部署中会对多播通信使用防火墙的原因。因此,多播虽然适合开发工作,但在生产中,最好使用基于 TCP/IP 的发现。

Multicast, by default, accepts communication from all the machines in the multicast group. This may be a security concern and that is why typically, on-premise, multicast communication is firewalled. So, while multicast is good for development work, in production, it is best to use TCP/IP based discovery.

TCP/IP

由于陈述了多播的缺陷,因此 TCP/IP 成为首选的通信方式。在 TCP/IP 的情况下,一个成员只能连接到已知/列出的成员。

Due to the drawbacks stated for Multicast, TCP/IP is the preferred way for communication. In case of TCP/IP, a member can connect to only known/listed members.

Example

我们让 TCP/IP 用于发现机制。在 hazelcast-tcp.xml 中添加以下内容:

Let’s use TCP/IP for discovery mechanisms. Save the following in hazelcast-tcp.xml

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

   <network>
      <join>
         <multicast enabled="false" />
         <tcp-ip enabled="true">
            <members>localhost</members>
         </tcp-ip>
      </join>
   </network>
</hazelcast>

然后,我们来执行以下命令 -

And then, let’s execute the following command −

java -Dhazelcast.config=hazelcast-tcp.xml -cp .\target\demo-0.0.1-SNAPSHOT.jar
com.example.demo.XMLConfigLoadExample

Output

output 如下 -

The output is following −

INFO: [localhost]:5701 [dev] [3.12.12] Creating TcpIpJoiner
Jan 30, 2021 8:09:29 PM
com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl

上述输出显示 TCP/IP 连接器用于连接两个成员。

The above output shows that TCP/IP joiner was use to join two members.

如果您在两个不同的 Shell 上执行以下命令 -

And if you execute following command on two different shells −

java '-Dhazelcast.config=hazelcast-tcp.xml' -cp .\target\demo-0.0.1-SNAPSHOT.jar
com.example.demo.MultiInstanceHazelcastExample

我们看到以下输出 -

We see the following output −

Members {size:2, ver:2} [
   Member [localhost]:5701 - 62eedeae-2701-4df0-843c-7c3655e16b0f
   Member [localhost]:5702 - 859c1b46-06e6-495a-8565-7320f7738dd1 this
]

上述输出表示节点能够使用 TCP/IP 连接,且两者都使用本地主机作为 IP 地址。

The above output means that the nodes were able to join using TCP/IP and both are using localhost as the IP address.

请注意我们在 XML 配置文件中可以指定更多 IP 或机器名称(会通过 DNS 解析)。

Note that we can specify more IPs or the machine names (which would be resolved by DNS) in the XML configuration file.

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

   <network>
      <join>
         <multicast enabled="false" />
         <tcp-ip enabled="true">
            <members>machine1, machine2....</members>
         </tcp-ip>
      </join>
   </network>
</hazelcast>