Hazelcast 简明教程

Hazelcast - Monitoring

Hazelcast 提供有多种方法来监视集群。我们将研究如何通过 REST API 和 JMX 来进行监视。让我们首先研究 REST API。

Hazelcast provides multiple ways to monitor the cluster. We will look into how to monitor via REST API and via JMX. Let’s first look into REST API.

Monitoring Hazelcast via REST API

要通过 REST API 监视集群或成员状态,必须为成员启用基于 REST API 的通信。这可以通过配置以及以编程方式来完成。

To monitor health of the cluster or member state via REST API, one has to enable REST API based communication to the members. This can be done by configuration and also programmatically.

让我们通过 hazelcast-monitoring.xml 中的 XML 配置启用基于 REST 的监视 −

Let us enable REST based monitoring via XML configuration in hazelcast-monitoring.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">
   <instance-name>XML_Hazelcast_Instance</instance-name>

   <network>
      <rest-api enabled="true">
         <endpoint-group name="CLUSTER_READ" enabled="true" />
         <endpoint-group name="HEALTH_CHECK" enabled="true" />
      </rest-api>
   </network>
</hazelcast>

让我们创建一个在 Server.java 文件中无限期运行的 Hazelcast 实例 −

Let us create a Hazelcast instance which runs indefinitely in Server.java file −

public class Server {
   public static void main(String... args){
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      // do not shutdown, let the server run
      //hazelcast.shutdown();
   }
}

现在让我们启动集群 −

And now let us execute start the cluster −

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

启动后,可以通过调用 API 来了解集群的健康状况,如下所示 −

Once started, the health of the cluster can be found out by calling the API like −

http://localhost:5701/hazelcast/health

上述 API 调用的 output

The output of the above API call −

Hazelcast::NodeState=ACTIVE
Hazelcast::ClusterState=ACTIVE
Hazelcast::ClusterSafe=TRUE
Hazelcast::MigrationQueueSize=0
Hazelcast::ClusterSize=1

这表明集群中有一个成员,它处于活动状态。

This displays that there is 1 member in our cluster and it is Active.

有关节点的更详细信息,例如,IP、端口、名称,可以使用 − 找到

More detailed information about the nodes, for example, IP, port, name can be found using −

http://localhost:5701/hazelcast/rest/cluster

上述 API 的输出 −

The output of the above API −

Members {size:1, ver:1} [
   Member [localhost]:5701 - e6afefcb-6b7c-48b3-9ccb-63b4f147d79d this
]
ConnectionCount: 1
AllConnectionCount: 2

JMX monitoring

Hazelcast 还支持对其中嵌入的数据结构进行 JMX 监控,例如,IMap、Iqueue 等。

Hazelcast also supports JMX monitoring of the data structures embedded inside it, for example, IMap, Iqueue, and so on.

要启用 JMX 监控,我们首先需要启用基于 JVM 的 JMX 代理。可以通过将“-Dcom.sun.management.jmxremote”传递给 JVM 来完成此操作。若要使用不同的端口或使用身份验证,我们分别可以使用 -Dcom.sun.management.jmxremote.port、- Dcom.sun.management.jmxremote.authenticate。

To enable JMX monitoring, we first need to enable JVM based JMX agents. This can be done by passing "-Dcom.sun.management.jmxremote" to the JVM. For using different ports or use authentication, we can use -Dcom.sun.management.jmxremote.port, - Dcom.sun.management.jmxremote.authenticate, respectively.

除此之外,我们必须为 Hazelcast MBeans 启用 JMX。让我们通过 hazelcast-monitoring.xml 中的 XML 配置启用基于 JMX 的监控 −

Apart from this, we have to enable JMX for Hazelcast MBeans. Let us enable JMX based monitoring via XML configuration in hazelcast-monitoring.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">
   <instance-name>XML_Hazelcast_Instance</instance-name>

   <properties>
      <property name="hazelcast.jmx">true</property>
   </properties>
</hazelcast>

让我们创建一个 Hazelcast 实例,该实例在 Server.java 文件中无限期运行,并添加一个映射 −

Let us create a Hazelcast instance which runs indefinitely in Server.java file and add a map −

class Server {
   public static void main(String... args){
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      //create a simple map
      Map<String, String> vehicleOwners = hazelcast.getMap("vehicleOwnerMap");
      // add key-value to map
      vehicleOwners.put("John", "Honda-9235");
      // do not shutdown, let the server run
      //hazelcast.shutdown();
   }
}

现在我们执行以下命令启用 JMX −

Now we can execute the following command to enable JMX −

java '-Dcom.sun.management.jmxremote' '-Dhazelcast.config=others\hazelcastmonitoring.
xml' -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.Server

现在,JMX 端口可以通过 JMX 客户端(如 jConsole、VisualVM 等)进行连接。

The JMX ports can now be connected by JMX clients like jConsole, VisualVM, etc.

以下是连接使用 jConsole 时获得的结果示例,并查看 VehicleMap 的属性。正如我们可以看到,映射的名称为 vehicleOwnerMap,映射的大小为 1。

Here is a snapshot of what we will get if we connect using jConsole and see the attributes for VehicleMap. As we can see, the name of the map as vehicleOwnerMap and the size of map being 1.

jmx clients