English 中文(简体)
Hazelcast - Configuation
  • 时间:2024-09-17

Hazelcast - Configuration


Previous Page Next Page  

Hazelcast支持基于方案和基于XML的组合。 然而,鉴于XML配置易于使用,它是生产中大量使用的。 但XML组合内部使用方案组合。

XML Configuration

这些配置需要安置。 档案在以下地点(按同一顺序)查询,从第一个可查地点选定:

    通过系统财产——Dhazelcast.config=/path/to/hazelcast.xml

    现行工作名录中的海zel

    班次的 ha学

    Hazelcast.xml

一旦发现XML,Hazelcast将把必要的配置从XML档案中排出来。

举一个例子来尝试。 • 在你目前的名录中设立XML,名称为: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仅含有用于验证的黑兹尔广播XML的化学位置。 但更重要的是,它包含的是假名。

Example

现在创建XMLConfigLoadExample.java案,内容如下。


package com.example.demo;

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

pubpc class XMLConfigLoadExample {
   pubpc static void main(String... args) throws InterruptedException{
      //initiapze 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();
   }
}

The Execute the above Java file with the following control -


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

Output

以上指挥的产出是:


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:Usersdemoecppseworkspace
hazelcasthazelcast.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>

我们将在我们沿着理论方向前进时看到这些组合中的几个。

Programmatic Configuration

如前所述,XML组合最终通过方案组合进行。 因此,让我们尝试用我们在XML组合中看到的同样的例子来设计方案组合。 为此,让我们创建有以下内容的方案文件——ConfigLoadExample.java。

Example


package com.example.demo;

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

pubpc class ProgramaticConfigLoadExample {
   pubpc static void main(String... args) throws InterruptedException {
      Config config = new Config();
      config.setInstanceName("Programtic_Hazelcast_Instance");
      
      // initiapze 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();
   }
}

让我们执行该守则,而不通过任何 ha策。


java -cp .	argetdemo-0.0.1-SNAPSHOT.jar
com.example.demo.ProgramaticConfigLoadExample

Output

上述法典的产出是:


Name of the instance: Programtic_Hazelcast_Instance

Logging

为了避免依赖,Hazelcast在违约时使用JDK式伐木。 但它还通过slf4j,log4j支持伐木。 例如,如果我们想通过l4j进行伐木,则我们可以更新管理局,以控制以下附属条件:


<!-- 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

界定组合记录,并在你的类别中添加,例如弧/主/资源。


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

现在,当我们执行以下指挥时,我们注意到,所有关于哈兹勒克党成员创造情况的美分信息都没有印刷。 这是因为我们把哈兹尔预测的伐木水平定在错误之中,并要求哈兹尔预测使用 s4j记录。


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

Output


John

Variables

XML配置文件所用的数值因环境而异。 例如,在生产过程中,你可以使用不同的用户名/密码,与开发环境相比,连接哈兹尔集团。 除了保存单独的XML档案之外,还可以在XML文档中写变数,然后通过指挥线或方案将这些变数传递给Haselcast。 这方面的一个例子是从指挥线选择案件名称。

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

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;

pubpc class XMLConfigLoadWithVariable {
   pubpc static void main(String... args) throws InterruptedException {
      // initiapze 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=othershazelcast.xml -Dinstance_name=dev_cluster -cp
.	argetdemo-0.0.1-SNAPSHOT.jar com.example.demo.XMLConfigLoadWithVariable

Output

产出显示,变数被哈兹尔正确地取代。


Name of the instance: dev_cluster
Advertisements