Docker部署Hazelcast

Hazelcast management-center

1
2
3
docker pull hazelcast/management-center

docker run -d -p 8200:8080 hazelcast/management-center:latest

安装配置管理节点,监控和实时查看缓存情况

Hazelcast镜像单节点部署

下载镜像

1
docker pull hazelcast/hazelcast

启动

1
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.1.40:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -p 5701:5701 hazelcast/hazelcast

注意:

  • hazelcast.rest.enabled=true,需要开启,不然管理节点连不上
  • docker需要后台启动服务-d,内部端口为5701
  • 最好指定publicAddress且需要设置JVM大小

节点输出

1
2
3
Members {size:1, ver:1} [
Member [192.168.1.40]:5701 - dbbe08e2-ed8c-4228-9cf8-a0ab0ae08632 this
]

Hazelcast镜像多节点multicast集群部署

节点1:

1
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.1.43:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://192.168.1.40:8200/hazelcast-mancenter" -p 5701:5701 hazelcast/hazelcast

节点2:

1
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.1.43:5702 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://192.168.1.40:8200/hazelcast-mancenter" -p 5702:5701 hazelcast/hazelcast

注意:

  • 指定MANCENTER_URL管理节点地址
  • multicast广播必须为同一台集群,因为docker下广播必须本机容器才能连接

Hazelcast镜像多节点TCP-IP集群部署

hazelcast.xml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    <management-center enabled="true">http://192.168.1.40:8200/hazelcast-mancenter</management-center>
...
<port auto-increment="true" port-count="10">5701</port>
...
<multicast enabled="false">
<multicast-group>224.2.2.3</multicast-group>
<multicast-port>54327</multicast-port>
</multicast>
<tcp-ip enabled="true">
<interface>192.168.1.40-45</interface>
<member-list>
<member>192.168.1.40</member>
<member>192.168.1.43</member>
</member-list>
</tcp-ip>

节点1

1
docker run -d -e JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=192.168.1.40:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" -v /home/xxx:/opt/hazelcast/config_ext -p 5701:5701 hazelcast/hazelcast

节点2

1
docker run -d -e JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=192.168.1.43:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" -v /home/xxx:/opt/hazelcast/config_ext -p 5701:5701 hazelcast/hazelcast

输出

1
2
3
4
Members {size:2, ver:2} [
Member [192.168.1.40]:5701 - cd40d155-d993-46a5-b07c-19f001c71f3c
Member [192.168.1.43]:5701 - 34d0798c-37d0-42e8-88f0-1268eab9a90a this
]

注意:

  • 端口自增限制为10,即每个机器端口限制为5701-5711,以提高发现效率
  • multicast关闭,tcp-ip开启
  • 限制interface,指定ip范围
  • 指定member-list,指定集群成员
  • 指定宿主机配置文件地址

配置

登录management-center

测试连接

引入包pom.xml

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.8.5</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.8.5</version>
</dependency>

client代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Slf4j
public class HazelcastTest {
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
//集群组名称
clientConfig.getGroupConfig().setName("dev");
//节点地址
clientConfig.getNetworkConfig().addAddress("192.168.1.40", "192.168.1.40:5702");
//客户端
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

UserInfo userInfo = new UserInfo();
userInfo.setUserName("xxxx");
userInfo.setUserDesc("com.hazelcast.core.Hazelcast");

//map缓存
Map<Integer, UserInfo> userInfoMap = client.getMap("instruments");

//并发测试
Runnable runnable = () -> {
long total = 10000;
Stopwatch stopwatch = Stopwatch.createStarted();
for (int i = 0; i < total; i++) {
//插入缓存
userInfoMap.put(i, userInfo);
}
stopwatch.stop();

log.info("total:{},elapsed:{},qps:{}", total, stopwatch.elapsed(TimeUnit.MILLISECONDS), stopwatch.elapsed(TimeUnit.MILLISECONDS) / total);
};

ExecutorService executorService = Executors.newFixedThreadPool(10);
int threadNum = 10;
for (int i = 0; i < threadNum; i++) {
executorService.submit(runnable);
}
}
}
------ 本文结束------

本文标题:Docker部署Hazelcast

文章作者:Perkins

发布时间:2019年09月12日

原始链接:https://perkins4j2.github.io/posts/39886/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。