Hazelcast - 配置


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

XML配置

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

  • 通过系统属性将 XML 的位置传递给 JVM - Dhazelcast.config=/path/to/hazelcast.xml

  • 当前工作目录中的 hazelcast.xml

  • 类路径中的 hazelcast.xml

  • Hazelcast 提供的默认 hazelcast.xml

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

让我们通过一个例子来尝试一下。在当前目录中创建一个名为 hazelcast.xml 的 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 的架构位置。但更重要的是,它包含实例名称。

例子

现在创建一个包含以下内容的 XMLConfigLoadExample.java 文件。

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 文件 -

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

输出

上述命令的输出将是 -

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 加载了配置并打印了配置中指定的名称(最后一行)。

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

https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/resources/hazelcast-full-example.xml

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

程序化配置

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

例子

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 文件:

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

输出

上述代码的输出是 -

Name of the instance: Programtic_Hazelcast_Instance

记录

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

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

例子

定义配置 logback.xml 文件并将其添加到类路径中,例如 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 的日志记录级别设置为错误,并要求 Hazelcast 使用 sl4j 记录器。

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

输出

John

变量

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

因此,这是带有变量 ${varname} 的 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>${instance_name}</instance-name>
</hazelcast>

例子

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

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();
   }
}

并且,以下是命令 -

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

输出

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

Name of the instance: dev_cluster