Hazelcast区分服务端和客户端,服务端通过hazelcast.xml文件加载配置,客户端通过hazelcast-client.xml文件加载配置。3.12版本以后支持yaml文件格式的配置,在xml和yaml文件同时存在时,优先加载xml文件。
以下是hazelcast.xml文件的最小配置(yaml格式):
hazelcast:
instance-name: hazelcast-node1
group:
name: hazelcast-server
network:
port:
port: 5701
auto-increment: true
port-count: 100
join:
multicast:
enabled: false
tcp-ip:
enabled: true
interface: 127.0.0.1
member-list:
- 10.113.114.32
- 10.113.114.38
xml格式:
hazelcast-server
123456password
5701
224.2.2.3
54327
11.12.13.14
11.12.13.15
11.12.13.16
注:3.11版本以后移除了password设置,不再进行password验证。
在服务端安装Hazelcast可以通过安装包或者源码包,以安装包为例,下载ZIP格式的安装包,链接地址:
https://hazelcast.org/download/
下载完成后在指定目录完成解压,进入bin目录下,将配置文件(hazelcast.xml或者hazelcast.yaml)替换进去,然后运行start.sh脚本,有多节点的集群则依次启动。
以Java客户端为例,将hazelcast-3.12和hazelcast-client-3.12的Jar包引入工程,推荐使用Maven:
com.hazelcast
hazelcast
3.12
com.hazelcast
hazelcast-client
3.12
客户端部分的配置更加简单,通过hazelcast-client.xml或者hazelcast-client.yaml文件加载配置,在xml和yaml文件同时存在时,优先加载xml文件,以下只以最小配置为例,yaml格式:
hazelcast-client:
group:
name: hazelcast-server
network:
cluster-members:
- 11.12.13.14
- 11.12.13.15
- 11.12.13.16
xml格式:
dev
dev-pass
CLIENT_NAME
127.0.0.1
127.0.0.2
使用时:
public HazelcastInstance hazelcastClientCase(){
return HazelcastClient.newHazelcastClient();
}
将hazelcast的日志打印出来,方便定位问题,Hazelcast支持slf4j下的日志框架,包括log4j, logback等,以logback为例。
修改start.sh启动文件:
1 #!/bin/sh
2
3 PRG="$0"
4 PRGDIR=`dirname "$PRG"`
5 HAZELCAST_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
6 PID_FILE=$HAZELCAST_HOME/bin/hazelcast_instance.pid
7 #export JAVA_OPTS="-Dhazelcast.logging.type=log4j -Dlog4j.configuration=file:/root/server/application/log4j.properties"
8 export JAVA_OPTS="-Dhazelcast.logging.type=slf4j -Dlogback.configurationFile=/root/server/midware/hazelcast-3.12/bin/logback.xml"
9 if [ $JAVA_HOME ]
10 then
11 echo "JAVA_HOME found at $JAVA_HOME"
12 RUN_JAVA=$JAVA_HOME/bin/java
13 else
14 echo "JAVA_HOME environment variable not available."
15 RUN_JAVA=`which java 2>/dev/null`
16 fi
17
18 if [ -z $RUN_JAVA ]
19 then
20 echo "JAVA could not be found in your system."
21 echo "please install Java 1.6 or higher!!!"
22 exit 1
23 fi
24
25 echo "Path to Java : $RUN_JAVA"
第七第八行分别为log4j和logback的日志配置, 分别为:hazelcast.logging.type和logback.configurationFile
然后将logback的jar包拷贝到user-lib目录下:
#:~/server/midware/hazelcast-3.12/user-lib # pwd
/root/server/midware/hazelcast-3.12/user-lib
#:~/server/midware/hazelcast-3.12/user-lib # ll
total 1396
-rw-r--r-- 1 root root 489884 May 20 16:18 log4j-1.2.17.jar
-rw-r--r-- 1 root root 107064 Aug 9 11:12 logback-access-1.2.3.jar
-rw-r--r-- 1 root root 290339 Aug 9 11:12 logback-classic-1.2.3.jar
-rw-r--r-- 1 root root 471901 Aug 9 11:12 logback-core-1.2.3.jar
-rw-r--r-- 1 root root 41203 Aug 9 11:23 slf4j-api-1.7.25.jar
配置好了以后,重新启动,就可以看到日志输出到了所配置的路径下了。