Hazelcast 简明教程

Hazelcast - Configuration

Hazelcast 支持基于编程的配置以及基于 XML 的配置。然而,由于易于使用,XML 配置在生产中被大量使用。但 XML 配置在内部使用基于编程的配置。

Hazelcast supports programmatic as well as XML-based configuration. However, it is the XML configuration which is heavily used in production, given its ease of use. But XML configuration internally uses the Programmatic configuration.

XML Configuration

hazelcast.xml 是需要放置这些配置的位置。以以下位置(按相同顺序)搜索该文件,并从第一个可用位置选择它−

The hazelcast.xml is where these configurations need to be placed. The file is searched for in the following location (in same order) and is chosen from the first available location −

  1. Passing the location of the XML to the JVM via the system property - Dhazelcast.config=/path/to/hazelcast.xml

  2. hazelcast.xml in the current working directory

  3. hazelcast.xml in the classpath

  4. default hazelcast.xml provided by Hazelcast

一旦找到 XML,Hazelcast 将从 XML 文件加载所需的配置。

Once the XML is found, Hazelcast would load the required configuration from the XML file.

让我们通过一个例子来尝试一下。在当前目录中创建一个名为 hazelcast.xml 的 XML。

Let’s try that out with an example. Create an XML in your current directory with the name hazelcast.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">
   <!-- name of the instance -->
   <instance-name>XML_Hazelcast_Instance</instance-name>
</hazelcast>

现在的 XML 只包含 Hazelcast XML 的模式位置,该模式位置用于验证。但更重要的是,它包含实例名称。

The XML as of now only contains the schema location of the Hazelcast XML which is used for validation. But more importantly, it contains the instance name.

Example

现在创建 XMLConfigLoadExample.java 文件,其中包含以下内容。

Now create an XMLConfigLoadExample.java file with the following content.

package com.example.demo;

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

public class XMLConfigLoadExample {
   public static void main(String... args) throws InterruptedException{
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();

      //specified the name written in the XML file
      System.out.println(String.format("Name of the instance: %s",hazelcast.getName()));

      //perform a graceful shutdown
      hazelcast.shutdown();
   }
}

使用以下命令执行上面的 Java 文件 -

Execute the above Java file with the following command −

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

Output

上述命令的输出将是 -

The output for above command would be −

Jan 30, 2021 1:21:41 PM com.hazelcast.config.XmlConfigLocator
INFO: Loading configuration hazelcast.xml from System property
'hazelcast.config'
Jan 30, 2021 1:21:41 PM com.hazelcast.config.XmlConfigLocator
INFO: Using configuration file at C:\Users\demo\eclipseworkspace\
hazelcast\hazelcast.xml
...
Members {size:1, ver:1} [
   Member [localhost]:5701 - 3d400aed-ddb9-4e59-9429-3ab7773e7e09 this
]
Name of cluster: XML_Hazelcast_Instance

正如您所看到的,Hazelcast 已经加载了配置并打印了配置中指定的名字(最后一行)。

As you see, Hazelcast loaded the configuration and printed the name which was specified in the configuration (last line).

XML 中可以指定许多配置选项。完整列表可以在以下位置找到 -

There are a whole lot of configuration options which can be specified in the XML. The complete list can be found at −

在我们继续学习本教程时,我们将看到其中一些配置。

We will see a few of these configurations as we move along the tutorial.

Programmatic Configuration

如前所述,XML 配置最终通过编程配置来完成。因此,让我们针对我们在 XML 配置中看到的同一个示例尝试编程配置。为此,让我们创建一个 ProgramaticConfigLoadExample.java 文件,其中包含以下内容。

As stated earlier, XML configuration is ultimately done via programmatic configuration. So, let’s try programmatic configuration for the same example which we saw in XML configuration. For that, let’s create the ProgramaticConfigLoadExample.java file with the following content.

Example

package com.example.demo;

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

public class ProgramaticConfigLoadExample {
   public static void main(String... args) throws InterruptedException {
      Config config = new Config();
      config.setInstanceName("Programtic_Hazelcast_Instance");

      // initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);

      // specified the name written in the XML file
      System.out.println(String.format("Name of the instance: %s", hazelcast.getName()));

      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

让我们在不传递任何 hazelcast.xml 文件的情况下执行代码 -

Let’s execute the code without passing any hazelcast.xml file by −

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

Output

上述代码的输出为:

The output of the above code is −

Name of the instance: Programtic_Hazelcast_Instance

Logging

为了避免依赖项,Hazelcast 默认使用基于 JDK 的日志记录。但它也支持通过 slf4j, log4j 进行日志记录。例如,如果我们想要通过 logback 设置使用 sl4j 的日志记录,我们可以更新 POM 以包含以下依赖项 -

To avoid dependencies, Hazelcast by default uses JDK based logging. But it also supports logging via slf4j, log4j. For example, if we want to setup logging via for sl4j with logback, we can update the POM to contain the following dependencies −

<!-- contains both sl4j bindings and the logback core -->
<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>1.2.3</version>
</dependency>

Example

定义一个配置 logback.xml 文件并将它添加到您的类路径,例如 src/main/resources。

Define a configuration logback.xml file and add it to your classpath, for example, src/main/resources.

<configuration>
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
      </encoder>
   </appender>

   <root level="info">
      <appender-ref ref="STDOUT" />
   </root>

   <logger name="com.hazelcast" level="error">
      <appender-ref ref="STDOUT" />
   </logger>
</configuration>

现在,当我们执行以下命令时,我们注意到关于 Hazelcast 成员创建等的所有元信息都没有被打印出来。这是因为我们已经把 Hazelcast 的日志级别设置为 error,并要求 Hazelcast 使用 sl4j 日志记录器。

Now, when we execute the following command, we notice that all the meta information about the Hazelcast member creation etc. is not printed. And this is because we have set the logging level for Hazelcast to error and asked Hazelcast to use sl4j logger.

java  -Dhazelcast.logging.type=slf4j -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.SingleInstanceHazelcastExample

Output

John

Variables

写入 XML 配置文件的值可能因环境而异。例如,在生产环境中,与开发环境相比,您可能对连接 Hazelcast 集群使用不同的用户名/密码。除了维护独立的 XML 文件外,人们还可以在 XML 文件中编写变量,然后通过命令行或以编程方式将这些变量传递给 Hazelcast。下面是通过命令行选择实例名称的一个示例。

Value written to XML configuration files can vary based on the environment. For example, in production, you may use a different username/password for connecting to the Hazelcast cluster compared to the dev environment. Instead of maintaining separate XML files, one can also write variables in the XML files and then pass those variables via command line or programmatically to Hazelcast. Here is an example for choosing the name of the instance from the command line.

因此,以下是我们的 XML 文件,其中包含变量 ${varname}

So, here is our XML file with the variable ${varname}

<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>${instance_name}</instance-name>
</hazelcast>

Example

以下是我们将用来打印变量值的示例 Java 代码 -

And here is the sample Java code we would use to print the variable value −

package com.example.demo;

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

public class XMLConfigLoadWithVariable {
   public static void main(String... args) throws InterruptedException {
      // initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();

      // specified the name written in the XML file
      System.out.println(String.format("Name of the instance: %s", hazelcast.getName()));

      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

以下命令 -

And, following is the command −

java -Dhazelcast.config=others\hazelcast.xml -Dinstance_name=dev_cluster -cp
.\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.XMLConfigLoadWithVariable

Output

并且输出显示变量已被 Hazelcast 正确替换。

And the output shows that the variable was replaced by Hazelcast correctly.

Name of the instance: dev_cluster