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進行日誌記錄。例如,如果我們想透過 sl4j 和 logback 設定日誌記錄,我們可以更新 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
廣告