Hazelcast教程

Hazelcast 配置

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

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 文件之外,还可以在 XML 文件中写入变量,然后通过命令行或以编程方式将这些变量传递给 Hazelcast。这是从命令行选择实例名称的示例。
所以,这是我们带有变量 ${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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4