Guide to Hazelcast with Java – 使用Java的Hazelcast指南

最后修改: 2016年 10月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This is an introductory article on Hazelcast where we’ll see how to create a cluster member, a distributed Map to share data among the cluster nodes, and create a Java client to connect and query data in the cluster.

这是一篇关于Hazelcast的介绍性文章,我们将看到如何创建一个集群成员,一个分布式Map以在集群节点之间共享数据,并创建一个Java客户端来连接和查询集群中的数据。

2. What Is Hazelcast?

2.什么是Hazelcast??

Hazelcast is a distributed In-Memory Data Grid platform for Java. The architecture supports high scalability and data distribution in a clustered environment. It supports the auto-discovery of nodes and intelligent synchronization.

Hazelcast是一个Java的分布式内存数据网格平台。该架构支持集群环境下的高可扩展性和数据分布。它支持节点的自动发现和智能同步。

Hazelcast is available in different editions. To see the features for all Hazelcast editions we can refer to the following link. In this tutorial, we’ll use the open-source edition.

Hazelcast有不同的版本。要查看所有Hazelcast版本的功能,我们可以参考以下链接。在本教程中,我们将使用开源版本。

Likewise, Hazelcast offers various features such as Distributed Data Structure, Distributed Compute, Distributed Query, etc. For the purpose of this article, we’ll focus on a distributed Map.

同样地,Hazelcast也提供了各种功能,如分布式数据结构、分布式计算、分布式查询等。在这篇文章中,我们将重点讨论分布式Map

3. Maven Dependency

3.Maven的依赖性

Hazelcast offers many different libraries to deal with various requirements. We can find them under com.hazelcast group in Maven Central.

Hazelcast提供了许多不同的库来处理各种需求。我们可以在Maven中心的com.hazelcast组下找到它们。

However, in this article, we’ll only use the core dependency needed to create a standalone Hazelcast cluster member and the Hazelcast Java Client:

然而,在本文中,我们将只使用创建一个独立的Hazelcast集群成员和Hazelcast Java客户端所需的核心依赖。

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
    <version>4.0.2</version>
</dependency>

The current version is available in maven central repository.

当前版本可在maven中央仓库中找到。

4. A First Hazelcast Application

4.第一个Hazelcast应用程序

4.1. Create a Hazelcast Member

4.1.创建一个Hazelcast成员

Members (also called nodes) automatically join together to form a cluster. This automatic joining takes place with various discovery mechanisms that the members use to find each other.

成员(也称为节点)自动加入到一起,形成一个集群。这种自动加入是通过成员用来寻找对方的各种发现机制进行的。

Let’s create a member that stores data in a Hazelcast distributed map:

让我们创建一个成员,在Hazelcast分布式地图中存储数据。

public class ServerNode {
    
    HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
    ...
}

When we start the ServerNode application, we can see the flowing text in the console which means that we create a new Hazelcast node in our JVM which will have to join the cluster.

当我们启动ServerNode应用程序时,我们可以在控制台中看到流动的文本,这意味着我们在JVM中创建了一个新的Hazelcast节点,它将不得不加入集群。

Members [1] {
    Member [192.168.1.105]:5701 - 899898be-b8aa-49aa-8d28-40917ccba56c this
}

To create multiple nodes we can start the multiple instances of ServerNode application. As a result, Hazelcast will automatically create and add a new member to the cluster.

为了创建多个节点,我们可以启动ServerNode应用程序的多个实例。这样,Hazelcast就会自动创建并向集群添加一个新成员。

For example, if we run the ServerNode application again, we’ll see the following log in the console which says that there are two members in the cluster.

例如,如果我们再次运行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. Create a Distributed Map

4.2.创建一个分布式的地图

Next, let’s create a distributed Map. We need the instance of HazelcastInstance created earlier to construct a distributed Map which extends java.util.concurrent.ConcurrentMap interface.

接下来,让我们来创建一个分布式Map。我们需要先前创建的HazelcastInstance的实例来构建一个分布式Map,它扩展了java.util.concurrent.ConcurrentMap接口。

Map<Long, String> map = hazelcastInstance.getMap("data");
...

Finally, let’s add some entries to the map:

最后,让我们在map上添加一些条目。

FlakeIdGenerator idGenerator = hazelcastInstance.getFlakeIdGenerator("newid");
for (int i = 0; i < 10; i++) {
    map.put(idGenerator.newId(), "message" + i);
}

As we can see above, we have added 10 entries to the map. We used FlakeIdGenerator to ensure that we get the unique key for the map. For more details on FlakeIdGenerator, we can check out the following link.

正如我们在上面看到的,我们已经向map添加了10个条目。我们使用了FlakeIdGenerator来确保我们得到地图的唯一键。关于FlakeIdGenerator的更多细节,我们可以查看以下链接

While this may not be a real-world example, we only used it to demonstrate one of the many operations that we can apply to the distributed map. Later on, we’ll see how to retrieve the entries added by the cluster member from the Hazelcast Java client.

虽然这可能不是一个真实的例子,但我们只是用它来演示我们可以应用于分布式地图的众多操作中的一个。稍后,我们将看到如何从Hazelcast Java客户端检索集群成员添加的条目。

Internally, Hazelcast partitions the map entries and distributes and replicates the entries among the cluster members. For more details on Hazelcast Map, we can check out the following link.

在内部,Hazelcast对map条目进行分区,并在集群成员中分发和复制这些条目。关于Hazelcast Map的更多细节,我们可以查看以下链接

4.3. Create a Hazelcast Java Client

4.3.创建一个Hazelcast Java客户端

Hazelcast client allows us to do all Hazelcast operations without being a member of the cluster. It connects to one of the cluster members and delegates all cluster-wide operations to it.

Hazelcast客户端允许我们在不成为集群成员的情况下进行所有Hazelcast操作。它连接到集群中的一个成员,并将所有集群范围内的操作委托给它。

Let’s create a native client:

让我们创建一个本地客户端。

ClientConfig config = new ClientConfig();
config.setClusterName("dev");
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);

It’s simple as that.

就这么简单。

4.4. Access Distributed Map From Java Client

4.4.从Java客户端访问分布式的Map

Next, we’ll use the instance of HazelcastInstance created earlier to access the distributed Map:

接下来,我们将使用之前创建的HazelcastInstance实例来访问分布式Map

Map<Long, String> 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:

现在我们可以对map进行操作,而不必是集群的成员。例如,让我们尝试对条目进行迭代。

for (Entry<Long, String> entry : map.entrySet()) {
    ...
}

5. Configuring Hazelcast

5.配置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.

在本节中,我们将重点介绍如何使用声明式(XML)和编程式(API)配置Hazelcast网络,并使用Hazelcast管理中心来监控和管理正在运行的节点。

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.

当Hazelcast启动时,它会寻找一个hazelcast.config系统属性。如果它被设置了,它的值就被用作路径。否则,Hazelcast会在工作目录或classpath上搜索一个hazelcast.xml文件。

If none of the above works, Hazelcast loads the default configuration, i.e. hazelcast-default.xml that comes with hazelcast.jar.

如果上述方法都不奏效,Hazelcast会加载默认配置,即hazelcast-default.xml,该配置随hazelcast.jar.一起提供。

5.1. Network Configuration

5.1.网络配置

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.

默认情况下,Hazelcast使用组播来发现可以形成集群的其他成员。如果多播不是我们环境中首选的发现方式,那么我们可以将Hazelcast配置为一个完整的TCP/IP集群。

Let’s configure the TCP/IP cluster using declarative configuration:

让我们使用声明式配置来配置TCP/IP集群。

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
                               http://www.hazelcast.com/schema/config/hazelcast-config-4.0.xsd";
    <network>
        <port auto-increment="true" port-count="20">5701</port>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="true">
                <member>machine1</member>
                <member>localhost</member>
            </tcp-ip>
        </join>
    </network>
</hazelcast>

Alternatively, we can use the Java config approach:

另外,我们也可以使用Java配置的方法。

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.

默认情况下,Hazelcast会尝试100个端口进行绑定。在上面的例子中,如果我们把端口的值设置为5701,并把端口数限制为20,那么在成员加入集群时,Hazelcast会尝试寻找5701和5721之间的端口。

If we want to choose to use only one port, we can disable the auto-increment feature by setting auto-increment to false.

如果我们想选择只使用一个端口,我们可以通过设置auto-incrementfalse来禁用自动增量功能。

5.2. Management Center Configuration

5.2.管理中心配置

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.

要使用 Hazelcast 管理中心,我们可以将 mancenter-version.war 应用程序部署到我们的 Java 应用服务器/容器中,或者我们可以从命令行启动 Hazelcast 管理中心。我们可以从hazelcast.org下载最新的Hazelcast ZIP。该ZIP包含mancenter-version.war文件。

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.

我们可以通过在hazelcast.xml中添加网络应用的URL来配置我们的Hazelcast节点,然后让Hazelcast成员与管理中心通信。

So let’s now configure the management center using declarative configuration:

所以现在让我们使用声明式配置来配置管理中心。

<management-center enabled="true">
    http://localhost:8080/mancenter
</management-center>

Likewise, here’s the programmatic configuration:

同样地,这里是程序化的配置。

ManagementCenterConfig manCenterCfg = new ManagementCenterConfig();
manCenterCfg.setEnabled(true).setUrl("http://localhost:8080/mancenter");

6. Conclusion

6.结论

In this article, we covered introductory concepts about Hazelcast. For more details, we can take a look at the Reference Manual.

在这篇文章中,我们介绍了关于Hazelcast的介绍性概念。关于更多的细节,我们可以看一下参考手册

As usual, all the code for this article is available over on GitHub.

像往常一样,本文的所有代码都可以在GitHub上找到