├── .gitignore
├── README.md
├── Screenshots
├── hazelcast_management_center.png
├── hazelcast_management_center_idgenerator.png
└── hazelcast_management_center_map.png
├── distributed-hazelcast-lock-example
├── README.md
├── docker-compose.yaml
├── hazelcast.xml
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── net
│ │ │ └── ameizi
│ │ │ └── distributed
│ │ │ └── hazelcast
│ │ │ └── example
│ │ │ ├── DistributedHazelcastLockExampleApplication.java
│ │ │ ├── LockController.java
│ │ │ └── config
│ │ │ └── HazelcastConfiguration.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── net
│ └── ameizi
│ └── distributed
│ └── hazelcast
│ └── example
│ ├── FlakeIdGeneratorSample.java
│ └── HazelcastClientTest.java
├── distributed-redis-lock-example
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── net
│ │ └── ameizi
│ │ └── distributed
│ │ └── lock
│ │ └── redis
│ │ └── example
│ │ └── DistributedRedisLockExampleApplication.java
│ └── resources
│ ├── application.properties
│ ├── redisson-cluster.yaml
│ ├── redisson-master-slave.yaml
│ ├── redisson-sentinel.yaml
│ └── redisson-single.yaml
├── distributed-zookeeper-lock-example
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── net
│ │ │ └── ameizi
│ │ │ └── distributed
│ │ │ └── lock
│ │ │ └── zookeeper
│ │ │ └── example
│ │ │ ├── DistributedZookeeperLockExampleApplication.java
│ │ │ └── config
│ │ │ ├── ZookeeperConfig.java
│ │ │ └── ZookeeperProperties.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── net
│ └── ameizi
│ ├── distributed
│ └── lock
│ │ └── zookeeper
│ │ └── example
│ │ ├── BlockingLockTest.java
│ │ ├── DistributedLockTest.java
│ │ └── NonBlockingLockTest.java
│ └── zookeeper
│ └── leader
│ └── examples
│ ├── LeaderLatchTest.java
│ ├── LeaderSelectorListener.java
│ └── LeaderSelectorTest.java
├── pom.xml
└── spring-integration-distributed-lock-examples
├── pom.xml
└── src
└── main
├── java
└── spring
│ └── integration
│ └── distributed
│ └── lock
│ └── examples
│ ├── DistributedLockRegistryApplication.java
│ └── config
│ ├── JdbcConfiguration.java
│ ├── RedisLockConfiguration.java
│ └── ZookeeperLockConfiguration.java
└── resources
└── application.properties
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # distributed-lock-examples
2 |
3 | 史上最全的分布式锁合辑(我们不造轮子,只需用好轮子!)
4 |
5 | ```
6 | distributed-lock-examples
7 | ├── README.md
8 | ├── Screenshots
9 | │ ├── hazelcast_management_center.png
10 | │ ├── hazelcast_management_center_idgenerator.png
11 | │ └── hazelcast_management_center_map.png
12 | ├── distributed-consul-lock-example
13 | │ ├── distributed-consul-lock-example.iml
14 | │ ├── pom.xml
15 | │ └── src
16 | │ └── main
17 | │ ├── java
18 | │ │ └── net
19 | │ │ └── ameizi
20 | │ │ └── distributed
21 | │ │ └── consul
22 | │ │ └── example
23 | │ │ └── DistributedConsulLockExampleApplication.java
24 | │ └── resources
25 | │ └── application.properties
26 | ├── distributed-hazelcast-lock-example
27 | │ ├── README.md
28 | │ ├── distributed-hazelcast-lock-example.iml
29 | │ ├── docker-compose.yaml
30 | │ ├── hazelcast.xml
31 | │ ├── pom.xml
32 | │ └── src
33 | │ ├── main
34 | │ │ ├── java
35 | │ │ │ └── net
36 | │ │ │ └── ameizi
37 | │ │ │ └── distributed
38 | │ │ │ └── hazelcast
39 | │ │ │ └── example
40 | │ │ │ ├── DistributedHazelcastLockExampleApplication.java
41 | │ │ │ ├── LockController.java
42 | │ │ │ └── config
43 | │ │ │ └── HazelcastConfiguration.java
44 | │ │ └── resources
45 | │ │ └── application.properties
46 | │ └── test
47 | │ ├── java
48 | │ │ └── net
49 | │ │ └── ameizi
50 | │ │ └── distributed
51 | │ │ └── hazelcast
52 | │ │ └── example
53 | │ │ ├── FlakeIdGeneratorSample.java
54 | │ │ └── HazelcastClientTest.java
55 | │ └── resources
56 | ├── distributed-redis-lock-example
57 | │ ├── distributed-redis-lock-example.iml
58 | │ ├── pom.xml
59 | │ └── src
60 | │ └── main
61 | │ ├── java
62 | │ │ └── net
63 | │ │ └── ameizi
64 | │ │ └── distributed
65 | │ │ └── lock
66 | │ │ └── redis
67 | │ │ └── example
68 | │ │ └── DistributedRedisLockExampleApplication.java
69 | │ └── resources
70 | │ ├── application.properties
71 | │ ├── redisson-cluster.yaml
72 | │ ├── redisson-master-slave.yaml
73 | │ ├── redisson-sentinel.yaml
74 | │ └── redisson-single.yaml
75 | ├── distributed-zookeeper-lock-example
76 | │ ├── distributed-zookeeper-lock-example.iml
77 | │ ├── pom.xml
78 | │ └── src
79 | │ ├── main
80 | │ │ ├── java
81 | │ │ │ └── net
82 | │ │ │ └── ameizi
83 | │ │ │ └── distributed
84 | │ │ │ └── lock
85 | │ │ │ └── zookeeper
86 | │ │ │ └── example
87 | │ │ │ ├── DistributedZookeeperLockExampleApplication.java
88 | │ │ │ └── config
89 | │ │ │ ├── ZookeeperConfig.java
90 | │ │ │ └── ZookeeperProperties.java
91 | │ │ └── resources
92 | │ │ ├── META-INF
93 | │ │ └── application.properties
94 | │ └── test
95 | │ ├── java
96 | │ │ └── net
97 | │ │ └── ameizi
98 | │ │ └── distributed
99 | │ │ └── lock
100 | │ │ └── zookeeper
101 | │ │ └── example
102 | │ │ ├── BlockingLockTest.java
103 | │ │ ├── DistributedLockTest.java
104 | │ │ └── NonBlockingLockTest.java
105 | │ └── resources
106 | ├── pom.xml
107 | └── spring-integration-distributed-lock-examples
108 | ├── pom.xml
109 | ├── spring-integration-distributed-lock-examples.iml
110 | └── src
111 | └── main
112 | ├── java
113 | │ └── spring
114 | │ └── integration
115 | │ └── distributed
116 | │ └── lock
117 | │ └── examples
118 | │ ├── DistributedLockRegistryApplication.java
119 | │ └── config
120 | │ ├── JdbcConfiguration.java
121 | │ ├── RedisLockConfiguration.java
122 | │ └── ZookeeperLockConfiguration.java
123 | └── resources
124 | └── application.properties
125 | ```
126 |
127 | 注:为学习实验方便,示例代码中的zookeeper、redis、jdbc、hazelcast均使用本地嵌入式,实际应用应使用独立部署的服务。
128 |
129 | * distributed-hazelcast-lock-example(hazelcast实现分布式锁及分布式id)
130 |
131 | 修改`docker-compose.yaml`和`hazelcast.xml`中宿主机的IP 地址后执行`docker-compose up -d`即可启动一个三个节点的hazelcast集群。
132 |
133 | 浏览地址栏访问`http://localhost:8080/hazelcast-mancenter`可访问`hazelcast`的控制台
134 |
135 | 三台子节点信息
136 |
137 | 
138 |
139 | 分布式 id
140 |
141 | 
142 |
143 | map
144 |
145 | 
146 |
147 | * distributed-redis-lock-example(redis 实现分布式锁)
148 |
149 | * distributed-zookeeper-lock-example(zookeeper 实现分布式锁)
150 |
151 | * spring-integration-distributed-lock-examples(spring-integration 实现分布式锁) 分别使用ZookeeperLockRegistry、RedisLockRegistry、JdbcLockRegistry实现分布式锁
152 |
153 |
--------------------------------------------------------------------------------
/Screenshots/hazelcast_management_center.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/c514db77447a5abb85784a2329464ab84cf0b4bc/Screenshots/hazelcast_management_center.png
--------------------------------------------------------------------------------
/Screenshots/hazelcast_management_center_idgenerator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/c514db77447a5abb85784a2329464ab84cf0b4bc/Screenshots/hazelcast_management_center_idgenerator.png
--------------------------------------------------------------------------------
/Screenshots/hazelcast_management_center_map.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/c514db77447a5abb85784a2329464ab84cf0b4bc/Screenshots/hazelcast_management_center_map.png
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/README.md:
--------------------------------------------------------------------------------
1 | # Hazelcast
2 |
3 | Docker部署Hazelcast
4 |
5 | ```bash
6 | $ docker run -e JAVA_OPTS="-Xms512M -Xmx1024M" -p 5701:5701 hazelcast/hazelcast:3.12.7
7 | $ docker run -e JAVA_OPTS="-Xms512M -Xmx1024M" -p 5702:5701 hazelcast/hazelcast:3.12.7
8 | $ docker run -e JAVA_OPTS="-Xms512M -Xmx1024M" -p 5703:5701 hazelcast/hazelcast:3.12.7
9 | ```
10 |
11 | 正确姿势应该是这样的
12 |
13 | ```bash
14 | $ docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.128:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://172.24.202.128:8080/hazelcast-mancenter" -p 5701:5701 hazelcast/hazelcast:3.12.7
15 | $ docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.128:5702 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://172.24.202.128:8080/hazelcast-mancenter" -p 5702:5701 hazelcast/hazelcast:3.12.7
16 | $ docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.128:5703 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://172.24.202.128:8080/hazelcast-mancenter" -p 5703:5701 hazelcast/hazelcast:3.12.7
17 | ```
18 |
19 | Hazelcast management-center
20 |
21 | 安装配置管理节点,监控和实时查看缓存情况
22 |
23 | ```bash
24 | $ docker run
25 | -m 512m \
26 | -p 8080:8080 \
27 | --rm \
28 | hazelcast/management-center:3.12.9
29 | ```
30 |
31 | ### Hazelcast镜像单节点部署
32 |
33 | ```bash
34 | docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.128:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -p 5701:5701 hazelcast/hazelcast:3.12.7
35 | ```
36 |
37 | 注意:
38 |
39 | * hazelcast.rest.enabled=true,需要开启,不然管理节点连不上
40 | * docker需要后台启动服务-d,内部端口为5701
41 | * 最好指定publicAddress且需要设置JVM大小
42 |
43 | ### Hazelcast镜像多节点multicast集群部署
44 |
45 | 节点1:
46 |
47 | ```bash
48 | docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.128:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://127.0.0.1:8080/hazelcast-mancenter" -p 5701:5701 hazelcast/hazelcast:3.12.7
49 | ```
50 |
51 | 节点2:
52 | ```bash
53 | docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=172.24.202.129:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://127.0.0.1:8080/hazelcast-mancenter" -p 5701:5701 hazelcast/hazelcast:3.12.7
54 | ```
55 |
56 | 注意:
57 |
58 | * 指定MANCENTER_URL管理节点地址
59 | * multicast广播必须为同一台集群,因为docker下广播必须本机容器才能连接
60 |
61 | ### Hazelcast镜像多节点TCP-IP集群部署
62 |
63 | hazelcast.xml配置:
64 |
65 | ```xml
66 | http://127.0.0.1:8080/hazelcast-mancenter
67 | 5701
68 |
69 | 224.2.2.3
70 | 54327
71 |
72 |
73 | 172.24.202.120-129
74 |
75 | 172.24.202.128
76 | 172.24.202.129
77 |
78 |
79 | ```
80 |
81 | 节点1
82 |
83 | ```bash
84 | docker run -d -e JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.128:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" -v ./config:/opt/hazelcast/config_ext -p 5701:5701 hazelcast/hazelcast:3.12.7
85 | ```
86 |
87 | 节点2
88 |
89 | ```bash
90 | docker run -d -e JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.129:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" -v ./config:/opt/hazelcast/config_ext -p 5701:5701 hazelcast/hazelcast:3.12.7
91 | ```
92 |
93 | 输出
94 |
95 | ```
96 | Members {size:2, ver:2} [
97 | Member [172.24.202.128]:5701 - cd40d155-d993-46a5-b07c-19f001c71f3c
98 | Member [172.24.202.129]:5701 - 34d0798c-37d0-42e8-88f0-1268eab9a90a this
99 | ]
100 | ```
101 |
102 | 注意:
103 |
104 | * 端口自增限制为10,即每个机器端口限制为5701-5711,以提高发现效率
105 | * multicast关闭,tcp-ip开启
106 | * 限制interface,指定ip范围
107 | * 指定member-list,指定集群成员
108 | * 指定宿主机配置文件地址
109 |
110 |
111 | 快速在本地启动一个三个节点的hazelcast集群
112 |
113 | ```bash
114 | docker run \
115 | -d \
116 | -e \
117 | JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.128:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" \
118 | -v $(pwd)/hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml \
119 | -p 5701:5701 \
120 | hazelcast/hazelcast:3.12.7
121 | ```
122 |
123 |
124 | ```bash
125 | docker run \
126 | -d \
127 | -e \
128 | JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.128:5702 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" \
129 | -v $(pwd)/hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml \
130 | -p 5702:5701 \
131 | hazelcast/hazelcast:3.12.7
132 | ```
133 |
134 |
135 | ```bash
136 | docker run \
137 | -d \
138 | -e \
139 | JAVA_OPTS="-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.128:5703 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M" \
140 | -v $(pwd)/hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml \
141 | -p 5703:5701 \
142 | hazelcast/hazelcast:3.12.7
143 | ```
144 |
145 |
146 | 配置
147 |
148 | 登录management-center
149 |
150 | 打开地址:http://127.0.0.1:8080/hazelcast-mancenter
151 |
152 | 第一次设置初始化账户和密码,密码有格式要求
153 |
154 | 添加成员节点,即Change URL
155 |
156 | 输入Cluster Name and Password,即集群名和集群密码
157 |
158 | 输入Server UR,为Management Center URL,即管理系统地址;例如http://127.0.0.1:8080/hazelcast-mancenter
159 |
160 | 测试连接
161 |
162 | 引入包pom.xml
163 |
164 | ```xml
165 |
166 | com.hazelcast
167 | hazelcast
168 | 3.12.7
169 |
170 |
171 | com.hazelcast
172 | hazelcast-spring
173 | 3.12.7
174 |
175 |
176 | com.hazelcast
177 | hazelcast-client
178 | 3.12.7
179 |
180 | ```
181 |
182 | client代码
183 |
184 | ```java
185 | @Slf4j
186 | public class HazelcastTest {
187 | public static void main(String[] args) {
188 | ClientConfig clientConfig = new ClientConfig();
189 | //集群组名称
190 | clientConfig.getGroupConfig().setName("dev");
191 | //节点地址
192 | clientConfig.getNetworkConfig().addAddress("172.24.202.128:5701", "172.24.202.129:5701");
193 | //客户端
194 | HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
195 | }
196 | }
197 | ```
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/docker-compose.yaml:
--------------------------------------------------------------------------------
1 | version: "3.6"
2 |
3 | services:
4 | management-center:
5 | container_name: management-center
6 | image: hazelcast/management-center:3.12.9
7 | ports:
8 | - 8080:8080
9 |
10 | hazelcast1:
11 | container_name: hazelcast1
12 | image: hazelcast/hazelcast:3.12.7
13 | depends_on:
14 | - management-center
15 | ports:
16 | - 5701:5701
17 | volumes:
18 | - ./hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml
19 | environment:
20 | - JAVA_OPTS=-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.121:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M
21 |
22 | hazelcast2:
23 | container_name: hazelcast2
24 | image: hazelcast/hazelcast:3.12.7
25 | depends_on:
26 | - management-center
27 | ports:
28 | - 5702:5701
29 | volumes:
30 | - ./hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml
31 | environment:
32 | - JAVA_OPTS=-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.121:5702 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M
33 |
34 | hazelcast3:
35 | container_name: hazelcast3
36 | image: hazelcast/hazelcast:3.12.7
37 | depends_on:
38 | - management-center
39 | ports:
40 | - 5703:5701
41 | volumes:
42 | - ./hazelcast.xml:/opt/hazelcast/config_ext/hazelcast.xml
43 | environment:
44 | - JAVA_OPTS=-Dhazelcast.config=/opt/hazelcast/config_ext/hazelcast.xml -Dhazelcast.local.publicAddress=172.24.202.121:5703 -Dhazelcast.rest.enabled=true -Xms128M -Xmx512M
45 |
46 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/hazelcast.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | dev
9 | dev-pass
10 |
11 |
12 |
13 | http://172.24.202.121:8080/hazelcast-mancenter
14 |
15 |
16 |
17 | 3
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | net.ameizi
6 | distributed-lock-examples
7 | 1.0
8 |
9 | distributed-hazelcast-lock-example
10 | jar
11 | distributed-hazelcast-lock-example
12 | Demo project for Spring Boot
13 |
14 | 1.8
15 |
16 |
17 |
18 | org.projectlombok
19 | lombok
20 | true
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 | com.hazelcast
28 | hazelcast
29 |
30 |
31 | com.hazelcast
32 | hazelcast-client
33 |
34 |
35 | com.hazelcast
36 | hazelcast-spring
37 |
38 |
39 | junit
40 | junit
41 | 4.12
42 |
43 |
44 |
45 |
46 |
47 | org.apache.maven.plugins
48 | maven-compiler-plugin
49 |
50 | 1.8
51 | 1.8
52 | UTF-8
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-maven-plugin
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/main/java/net/ameizi/distributed/hazelcast/example/DistributedHazelcastLockExampleApplication.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.hazelcast.example;
2 |
3 | import com.hazelcast.core.HazelcastInstance;
4 | import com.hazelcast.cp.lock.FencedLock;
5 | import lombok.extern.slf4j.Slf4j;
6 | import org.junit.Assert;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import javax.annotation.Resource;
13 |
14 | @Slf4j
15 | @RestController
16 | @SpringBootApplication
17 | public class DistributedHazelcastLockExampleApplication {
18 |
19 | @Resource(name="hazelcastInstance1")
20 | private HazelcastInstance hz1;
21 | @Resource(name="hazelcastInstance2")
22 | private HazelcastInstance hz2;
23 | @Resource(name="hazelcastInstance3")
24 | private HazelcastInstance hz3;
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(DistributedHazelcastLockExampleApplication.class, args);
28 | }
29 |
30 | @GetMapping("/reentrant-lock")
31 | public void reentrantlock() {
32 | FencedLock hz1Lock = hz1.getCPSubsystem().getLock("LOCK");
33 | FencedLock hz2Lock = hz2.getCPSubsystem().getLock("LOCK");
34 |
35 | hz1Lock.lock();
36 | hz1Lock.lock();
37 |
38 | boolean b = hz2Lock.tryLock();
39 | Assert.assertFalse(b);
40 |
41 | hz1Lock.unlock();
42 | hz1Lock.unlock();
43 |
44 | b = hz2Lock.tryLock();
45 | Assert.assertTrue(b);
46 | hz2Lock.unlock();
47 | }
48 |
49 | @GetMapping("/lock")
50 | public void lock() {
51 | // 默认情况下FencedLock是可重入锁,可以通过FencedLockConfig设置为非重入锁
52 | FencedLock hz1Lock = hz1.getCPSubsystem().getLock("LOCK");
53 | FencedLock hz2Lock = hz2.getCPSubsystem().getLock("LOCK");
54 |
55 | hz1Lock.lock();
56 | // 再次加锁会报错 com.hazelcast.cp.lock.exception.LockAcquireLimitReachedException: Lock[LOCK] reentrant lock limit is already reached!]
57 | // hz1Lock.lock();
58 |
59 | boolean b = hz2Lock.tryLock();
60 | Assert.assertFalse(b);
61 |
62 | hz1Lock.unlock();
63 |
64 | b = hz2Lock.tryLock();
65 | Assert.assertTrue(b);
66 | hz2Lock.unlock();
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/main/java/net/ameizi/distributed/hazelcast/example/LockController.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.hazelcast.example;
2 |
3 | import com.hazelcast.core.HazelcastInstance;
4 | import com.hazelcast.cp.lock.FencedLock;
5 | import com.hazelcast.flakeidgen.FlakeIdGenerator;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.junit.Assert;
8 | import org.springframework.boot.SpringApplication;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import javax.annotation.Resource;
13 |
14 |
15 | @Slf4j
16 | @RestController
17 | public class LockController {
18 |
19 | public static final String ID_GENERATOR = "idGenerator";
20 |
21 | @Resource(name="hazelcastInstance1")
22 | private HazelcastInstance hazelcastInstance;
23 |
24 | public static void main(String[] args) {
25 | SpringApplication.run(DistributedHazelcastLockExampleApplication.class, args);
26 | }
27 |
28 | /**
29 | * 分布式锁
30 | */
31 | @GetMapping("/loc")
32 | public void lock() {
33 | FencedLock loc = hazelcastInstance.getCPSubsystem().getLock("loc");
34 | boolean b = loc.tryLock();
35 | Assert.assertTrue(b);
36 | loc.unlock();
37 | }
38 |
39 | /**
40 | * 分布式 id
41 | * @return
42 | */
43 | @GetMapping("/getid")
44 | public long getid(){
45 | FlakeIdGenerator flakeIdGenerator = hazelcastInstance.getFlakeIdGenerator(ID_GENERATOR);
46 | return flakeIdGenerator.newId();
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/main/java/net/ameizi/distributed/hazelcast/example/config/HazelcastConfiguration.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.hazelcast.example.config;
2 |
3 | import com.hazelcast.client.HazelcastClient;
4 | import com.hazelcast.client.config.ClientConfig;
5 | import com.hazelcast.client.config.ClientFlakeIdGeneratorConfig;
6 | import com.hazelcast.config.Config;
7 | import com.hazelcast.config.FlakeIdGeneratorConfig;
8 | import com.hazelcast.config.ManagementCenterConfig;
9 | import com.hazelcast.config.cp.FencedLockConfig;
10 | import com.hazelcast.core.HazelcastInstance;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 |
14 | import static java.util.concurrent.TimeUnit.MINUTES;
15 |
16 | /**
17 | * hazelcast集群 server 注册,程序启动后启动三个hazelcast节点并自动发现注册为集群
18 | */
19 | @Configuration
20 | public class HazelcastConfiguration {
21 |
22 | public static final String ID_GENERATOR = "idGenerator";
23 |
24 | @Bean
25 | public FlakeIdGeneratorConfig flakeIdGeneratorConfig(){
26 | FlakeIdGeneratorConfig idGeneratorConfig = new FlakeIdGeneratorConfig(ID_GENERATOR);
27 | idGeneratorConfig.setPrefetchCount(10)
28 | .setPrefetchValidityMillis(MINUTES.toMillis(10));
29 | return idGeneratorConfig;
30 | }
31 |
32 | @Bean
33 | public ClientFlakeIdGeneratorConfig clientFlakeIdGeneratorConfig(){
34 | ClientFlakeIdGeneratorConfig idGeneratorConfig = new ClientFlakeIdGeneratorConfig(ID_GENERATOR);
35 | idGeneratorConfig.setPrefetchCount(10)
36 | .setPrefetchValidityMillis(MINUTES.toMillis(10));
37 | return idGeneratorConfig;
38 | }
39 |
40 | /**
41 | * 本地启动嵌入式hazelcast集群配置,会在本地启动hazelcast服务器并组好集群
42 | * @return
43 | */
44 | @Bean
45 | public Config hazelCastConfig() {
46 | // 设置集群管理中心
47 | ManagementCenterConfig centerConfig = new ManagementCenterConfig();
48 | centerConfig.setUrl("http://localhost:8080/hazelcast-mancenter");
49 | centerConfig.setEnabled(true);
50 |
51 | FencedLockConfig fencedLockConfig = new FencedLockConfig();
52 | // 不可重入
53 | fencedLockConfig.disableReentrancy();
54 |
55 | Config config = new Config();
56 | config.getCPSubsystemConfig()
57 | .setCPMemberCount(3);
58 | // 设置为不可重入锁
59 | // .addLockConfig(fencedLockConfig);
60 |
61 | config.setManagementCenterConfig(centerConfig);
62 | config.addFlakeIdGeneratorConfig(flakeIdGeneratorConfig());
63 |
64 | return config;
65 | }
66 |
67 | /**
68 | * 客户端配置,连接远程hazelcast服务器集群
69 | * @return
70 | */
71 | @Bean
72 | public ClientConfig clientConfig(){
73 | ClientConfig clientConfig = new ClientConfig();
74 | //集群组名称
75 | clientConfig.getGroupConfig().setName("dev");
76 | //节点地址
77 | clientConfig.getNetworkConfig().addAddress("127.0.0.1:5701", "127.0.0.1:5702", "127.0.0.1:5703");
78 | clientConfig.addFlakeIdGeneratorConfig(clientFlakeIdGeneratorConfig());
79 | return clientConfig;
80 | }
81 |
82 |
83 | @Bean
84 | public HazelcastInstance hazelcastInstance1(){
85 | // return Hazelcast.newHazelcastInstance(hazelCastConfig()); // 本地启动hazelcast服务器
86 | return HazelcastClient.newHazelcastClient(clientConfig()); // 连接远程hazelcast服务器
87 | }
88 |
89 | @Bean
90 | public HazelcastInstance hazelcastInstance2(){
91 | // return Hazelcast.newHazelcastInstance(hazelCastConfig()); // 本地启动hazelcast服务器
92 | return HazelcastClient.newHazelcastClient(clientConfig()); // 连接远程hazelcast服务器
93 | }
94 |
95 | @Bean
96 | public HazelcastInstance hazelcastInstance3(){
97 | // return Hazelcast.newHazelcastInstance(hazelCastConfig()); // 本地启动hazelcast服务器
98 | return HazelcastClient.newHazelcastClient(clientConfig()); // 连接远程hazelcast服务器
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # 应用服务web访问端口
2 | server.port=8081
3 | spring.application.name=distributed-hazelcast-lock-example
4 |
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/test/java/net/ameizi/distributed/hazelcast/example/FlakeIdGeneratorSample.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.hazelcast.example;
2 |
3 | import com.hazelcast.client.HazelcastClient;
4 | import com.hazelcast.client.config.ClientConfig;
5 | import com.hazelcast.client.config.ClientFlakeIdGeneratorConfig;
6 | import com.hazelcast.core.HazelcastInstance;
7 | import com.hazelcast.flakeidgen.FlakeIdGenerator;
8 |
9 | import static java.util.concurrent.TimeUnit.MINUTES;
10 |
11 | public class FlakeIdGeneratorSample {
12 |
13 | public static void main(String[] args) {
14 | ClientConfig clientConfig = new ClientConfig()
15 | .addFlakeIdGeneratorConfig(new ClientFlakeIdGeneratorConfig("idGenerator")
16 | .setPrefetchCount(10)
17 | .setPrefetchValidityMillis(MINUTES.toMillis(10)));
18 | //集群组名称
19 | clientConfig.getGroupConfig().setName("dev");
20 | //节点地址
21 | clientConfig.getNetworkConfig().addAddress("127.0.0.1:5701", "127.0.0.1:5702", "127.0.0.1:5703");
22 | //客户端
23 | HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
24 |
25 | FlakeIdGenerator idGenerator = client.getFlakeIdGenerator("idGenerator");
26 | for (int i = 0; i < 10000; i++) {
27 | System.out.printf("Id: %s\n", idGenerator.newId());
28 | }
29 |
30 | client.getLifecycleService().shutdown();
31 | }
32 | }
--------------------------------------------------------------------------------
/distributed-hazelcast-lock-example/src/test/java/net/ameizi/distributed/hazelcast/example/HazelcastClientTest.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.hazelcast.example;
2 |
3 | import com.hazelcast.client.HazelcastClient;
4 | import com.hazelcast.client.config.ClientConfig;
5 | import com.hazelcast.core.HazelcastInstance;
6 | import com.hazelcast.core.IMap;
7 | import lombok.extern.slf4j.Slf4j;
8 |
9 | import java.util.concurrent.ExecutorService;
10 | import java.util.concurrent.Executors;
11 |
12 |
13 | @Slf4j
14 | public class HazelcastClientTest {
15 |
16 | public static void main(String[] args) {
17 |
18 | ClientConfig clientConfig = new ClientConfig();
19 | //集群组名称
20 | clientConfig.getGroupConfig().setName("dev");
21 | //节点地址
22 | clientConfig.getNetworkConfig().addAddress("127.0.0.1:5701", "127.0.0.1:5702", "127.0.0.1:5703");
23 | //客户端
24 | HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
25 |
26 | IMap