Hazelcast 简明教程

Hazelcast - Client

Hazelcast 客户端是对 Hazelcast 成员的轻量级客户端。Hazelcast 成员负责存储数据和分区。它们在传统的客户端-服务器模型中充当服务器。

Hazelcast clients are the lightweight clients to Hazelcast members. Hazelcast members are responsible to store data and the partitions. They act like the server in the traditional client-server model.

Hazelcast 客户端仅用于访问存储在集群的 Hazelcast 成员中的数据。它们不负责存储数据,也不承担存储数据的任何所有权。

Hazelcast clients are created only for accessing data stored with Hazelcast members of the cluster. They are not responsible to store data and do not take any ownership to store data.

这些客户端有自己的生命周期并且不会影响 Hazelcast 成员实例。

The clients have their own life cycle and do not affect the Hazelcast member instances.

我们先创建并运行 Server.java。

Let’s first create Server.java and run it.

import java.util.Map;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
public 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();
   }
}

现在,运行上述类。

Now, run the above class.

java -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.Server

对于设置客户端,我们还需要添加客户端 jar。

For setting up a client, we also need to add client jar.

<dependency>
   <groupId>com.hazelcast</groupId>
   <artifactId>hazelcast-client</artifactId>
   <version>3.12.12</version>
</dependency>

现在让我们创建 Client.java。请注意,类似于 Hazelcast 成员,客户端也可以通过编程方式或通过 XML 配置(即通过 -Dhazelcast.client.config 或 hazelcast-client.xml)进行配置。

Let’s now create Client.java. Note that similar to Hazelcast members, clients can also be configured programmatically or via XML configuration (i.e., via -Dhazelcast.client.config or hazelcast-client.xml).

Example

我们使用默认配置,这意味着我们的客户端将能够连接到本地实例。

Let’s use the default configuration which means our client would be able to connect to local instances.

import java.util.Map;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
public class Client {
   public static void main(String... args){
      //initialize hazelcast client
      HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();
      //read from map
      Map<String, String> vehicleOwners = hzClient.getMap("vehicleOwnerMap");
      System.out.println(vehicleOwners.get("John"));
      System.out.println("Member of cluster: " +
      hzClient.getCluster().getMembers());
      // perform shutdown
      hzClient.getLifecycleService().shutdown();
   }
}

现在,运行上述类。

Now, run the above class.

java -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.Client

Output

它将生成如下输出:

It will produce the following output −

Honda-9235
Member of cluster: [Member [localhost]:5701 - a47ec375-3105-42cd-96c7-fc5eb382e1b0]

从输出中看到 -

As seen from the output −

  1. The cluster only contains 1 member which is from Server.java.

  2. The client is able to access the map which is stored inside the server.

Load Balancing

Hazelcast 客户端支持使用各种算法进行负载均衡。负载均衡确保负载在成员之间共享,集群中的任何单个成员都不会过载。默认的负载均衡机制设置为循环。使用 config 中的 loadBalancer 标记可以更改此设置。

Hazelcast Client supports load balancing using various algorithms. Load balancing ensures that the load is shared across members and no single member of the cluster is overloaded. The default load balancing mechanism is set to round-robin. The same can be changed by using the loadBalancer tag in the config.

我们可以使用配置中的 load-balancer 标记指定负载均衡器的类型。下面是一个随意选择节点的策略的示例。

We can specify the type of load balancer using the load-balancer tag in the configuration. Here is a sample for choosing a strategy that randomly picks up a node.

<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
   http://www.hazelcast.com/schema/client-config/hazelcastclient-config-4.2.xsd">
      <load-balancer type="random"/>
</hazelcast-client>

Failover

在分布式环境中,成员可能任意失败。为了支持故障转移,建议提供多个成员的地址。如果客户端可以访问任何一个成员,那它就能访问到其他成员。addressList 参数可以在客户端配置中指定。

In a distributed environment, members can fail arbitrarily. For supporting failover, it is recommended that address to multiple members is provided. If the client gets access to any one member, that is sufficient for it to get addressed to other members. The parameters addressList can be specified in the client configuration.

例如,如果我们使用以下配置 -

For example, if we use the following configuration −

<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
   http://www.hazelcast.com/schema/client-config/hazelcastclient-config-4.2.xsd">
   <address-list>machine1, machine2</address-list>
</hazelcast-client>

即使 machine1 宕机,客户端也可以使用 machine2 访问集群的其他成员。

Even if, say, machine1 goes down, clients can use machine2 to get access to other members of the cluster.