├── Screenshots
├── hazelcast_management_center.png
├── hazelcast_management_center_map.png
└── hazelcast_management_center_idgenerator.png
├── distributed-hazelcast-lock-example
├── src
│ ├── main
│ │ ├── resources
│ │ │ └── application.properties
│ │ └── java
│ │ │ └── net
│ │ │ └── ameizi
│ │ │ └── distributed
│ │ │ └── hazelcast
│ │ │ └── example
│ │ │ ├── LockController.java
│ │ │ ├── DistributedHazelcastLockExampleApplication.java
│ │ │ └── config
│ │ │ └── HazelcastConfiguration.java
│ └── test
│ │ └── java
│ │ └── net
│ │ └── ameizi
│ │ └── distributed
│ │ └── hazelcast
│ │ └── example
│ │ ├── FlakeIdGeneratorSample.java
│ │ └── HazelcastClientTest.java
├── hazelcast.xml
├── docker-compose.yaml
├── pom.xml
└── README.md
├── distributed-redis-lock-example
├── src
│ └── main
│ │ ├── resources
│ │ ├── application.properties
│ │ ├── redisson-sentinel.yaml
│ │ ├── redisson-cluster.yaml
│ │ ├── redisson-master-slave.yaml
│ │ └── redisson-single.yaml
│ │ └── java
│ │ └── net
│ │ └── ameizi
│ │ └── distributed
│ │ └── lock
│ │ └── redis
│ │ └── example
│ │ └── DistributedRedisLockExampleApplication.java
└── pom.xml
├── distributed-zookeeper-lock-example
├── src
│ ├── main
│ │ ├── resources
│ │ │ └── application.properties
│ │ └── java
│ │ │ └── net
│ │ │ └── ameizi
│ │ │ └── distributed
│ │ │ └── lock
│ │ │ └── zookeeper
│ │ │ └── example
│ │ │ ├── config
│ │ │ ├── ZookeeperProperties.java
│ │ │ └── ZookeeperConfig.java
│ │ │ └── DistributedZookeeperLockExampleApplication.java
│ └── test
│ │ └── java
│ │ └── net
│ │ └── ameizi
│ │ ├── zookeeper
│ │ └── leader
│ │ │ └── examples
│ │ │ ├── LeaderSelectorListener.java
│ │ │ ├── LeaderSelectorTest.java
│ │ │ └── LeaderLatchTest.java
│ │ └── distributed
│ │ └── lock
│ │ └── zookeeper
│ │ └── example
│ │ ├── BlockingLockTest.java
│ │ ├── NonBlockingLockTest.java
│ │ └── DistributedLockTest.java
└── pom.xml
├── .gitignore
├── spring-integration-distributed-lock-examples
├── src
│ └── main
│ │ ├── resources
│ │ └── application.properties
│ │ └── java
│ │ └── spring
│ │ └── integration
│ │ └── distributed
│ │ └── lock
│ │ └── examples
│ │ ├── config
│ │ ├── JdbcConfiguration.java
│ │ ├── RedisLockConfiguration.java
│ │ └── ZookeeperLockConfiguration.java
│ │ └── DistributedLockRegistryApplication.java
└── pom.xml
├── pom.xml
└── README.md
/Screenshots/hazelcast_management_center.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/HEAD/Screenshots/hazelcast_management_center.png
--------------------------------------------------------------------------------
/Screenshots/hazelcast_management_center_map.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/HEAD/Screenshots/hazelcast_management_center_map.png
--------------------------------------------------------------------------------
/Screenshots/hazelcast_management_center_idgenerator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/v5tech/distributed-lock-examples/HEAD/Screenshots/hazelcast_management_center_idgenerator.png
--------------------------------------------------------------------------------
/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-redis-lock-example/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=distributed-redis-lock-example
2 | # 应用服务web访问端口
3 | server.port=8080
4 | spring.redis.host=localhost
5 | spring.redis.port=6379
6 | spring.redis.database=0
7 | spring.redis.password=
8 | spring.redis.redisson.config=classpath:redisson-single.yaml
9 |
--------------------------------------------------------------------------------
/distributed-zookeeper-lock-example/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=distributed-zookeeper-lock-example
2 | # 应用服务web访问端口
3 | server.port=8080
4 |
5 | #Zookeeper Server地址,如果有多个使用,分隔
6 | zookeeper.address=127.0.0.1:2181
7 | #重试次数
8 | zookeeper.retryCount=5
9 | #重试间隔时间
10 | zookeeper.elapsedTimeMs=5000
11 | #session超时时间
12 | zookeeper.sessionTimeoutMs=30000
13 | #连接超时时间
14 | zookeeper.connectionTimeoutMs=10000
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/spring-integration-distributed-lock-examples/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=spring-integration-distributed-lock-examples
2 | # 应用服务web访问端口
3 | server.port=8086
4 |
5 | spring.datasource.url=jdbc:h2:file:./h2lock
6 | spring.datasource.driverClassName=org.h2.Driver
7 | spring.datasource.username=sa
8 | spring.datasource.password=
9 |
10 | spring.h2.console.enabled=true
11 | spring.h2.console.settings.trace=true
12 | spring.h2.console.settings.web-allow-others=true
13 | spring.h2.console.path=/h2-console
--------------------------------------------------------------------------------
/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-zookeeper-lock-example/src/main/java/net/ameizi/distributed/lock/zookeeper/example/config/ZookeeperProperties.java:
--------------------------------------------------------------------------------
1 | package net.ameizi.distributed.lock.zookeeper.example.config;
2 |
3 | import lombok.Data;
4 | import org.springframework.boot.context.properties.ConfigurationProperties;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | /**
8 | * 从配置文件中读取 Zookeeper Server 连接参数
9 | */
10 | @Data
11 | @Configuration
12 | @ConfigurationProperties(prefix = "zookeeper")
13 | public class ZookeeperProperties {
14 |
15 | /** 重试次数 */
16 | private int retryCount;
17 |
18 | /** 重试间隔时间 */
19 | private int elapsedTimeMs;
20 |
21 | /**连接地址 */
22 | private String address;
23 |
24 | /**Session过期时间 */
25 | private int sessionTimeoutMs;
26 |
27 | /**连接超时时间 */
28 | private int connectionTimeoutMs;
29 |
30 | }
--------------------------------------------------------------------------------
/spring-integration-distributed-lock-examples/src/main/java/spring/integration/distributed/lock/examples/config/JdbcConfiguration.java:
--------------------------------------------------------------------------------
1 | package spring.integration.distributed.lock.examples.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.integration.jdbc.lock.DefaultLockRepository;
6 | import org.springframework.integration.jdbc.lock.JdbcLockRegistry;
7 | import org.springframework.integration.jdbc.lock.LockRepository;
8 |
9 | import javax.sql.DataSource;
10 |
11 | @Configuration
12 | public class JdbcConfiguration {
13 |
14 | @Bean
15 | public DefaultLockRepository lockRepository(DataSource dataSource){
16 | return new DefaultLockRepository(dataSource);
17 | }
18 |
19 | @Bean
20 | public JdbcLockRegistry jdbcLockRegistry(LockRepository lockRepository){
21 | return new JdbcLockRegistry(lockRepository);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/distributed-redis-lock-example/src/main/resources/redisson-sentinel.yaml:
--------------------------------------------------------------------------------
1 | sentinelServersConfig:
2 | idleConnectionTimeout: 10000
3 | pingTimeout: 1000
4 | connectTimeout: 10000
5 | timeout: 3000
6 | retryAttempts: 3
7 | retryInterval: 1500
8 | reconnectionTimeout: 3000
9 | failedAttempts: 3
10 | password: null
11 | subscriptionsPerConnection: 5
12 | clientName: null
13 | loadBalancer: ! {}
14 | slaveSubscriptionConnectionMinimumIdleSize: 1
15 | slaveSubscriptionConnectionPoolSize: 50
16 | slaveConnectionMinimumIdleSize: 32
17 | slaveConnectionPoolSize: 64
18 | masterConnectionMinimumIdleSize: 32
19 | masterConnectionPoolSize: 64
20 | readMode: "SLAVE"
21 | sentinelAddresses:
22 | - "redis://127.0.0.1:7001"
23 | - "redis://127.0.0.1:7002"
24 | masterName: "master"
25 | database: 0
26 | threads: 0
27 | nettyThreads: 0
28 | codec: ! {}
29 | "transportMode":"NIO"
--------------------------------------------------------------------------------
/distributed-redis-lock-example/src/main/resources/redisson-cluster.yaml:
--------------------------------------------------------------------------------
1 | clusterServersConfig:
2 | idleConnectionTimeout: 10000
3 | pingTimeout: 1000
4 | connectTimeout: 10000
5 | timeout: 3000
6 | retryAttempts: 3
7 | retryInterval: 1500
8 | reconnectionTimeout: 3000
9 | failedAttempts: 3
10 | password: null
11 | subscriptionsPerConnection: 5
12 | clientName: null
13 | loadBalancer: ! {}
14 | slaveSubscriptionConnectionMinimumIdleSize: 1
15 | slaveSubscriptionConnectionPoolSize: 50
16 | slaveConnectionMinimumIdleSize: 32
17 | slaveConnectionPoolSize: 64
18 | masterConnectionMinimumIdleSize: 32
19 | masterConnectionPoolSize: 64
20 | readMode: "SLAVE"
21 | nodeAddresses:
22 | - "redis://127.0.0.1:7001"
23 | - "redis://127.0.0.1:7002"
24 | - "redis://127.0.0.1:7003"
25 | scanInterval: 1000
26 | threads: 0
27 | nettyThreads: 0
28 | codec: ! {}
29 | "transportMode":"NIO"
--------------------------------------------------------------------------------
/distributed-redis-lock-example/src/main/resources/redisson-master-slave.yaml:
--------------------------------------------------------------------------------
1 | masterSlaveServersConfig:
2 | idleConnectionTimeout: 10000
3 | pingTimeout: 1000
4 | connectTimeout: 10000
5 | timeout: 3000
6 | retryAttempts: 3
7 | retryInterval: 1500
8 | reconnectionTimeout: 3000
9 | failedAttempts: 3
10 | password: null
11 | subscriptionsPerConnection: 5
12 | clientName: null
13 | loadBalancer: ! {}
14 | slaveSubscriptionConnectionMinimumIdleSize: 1
15 | slaveSubscriptionConnectionPoolSize: 50
16 | slaveConnectionMinimumIdleSize: 32
17 | slaveConnectionPoolSize: 64
18 | masterConnectionMinimumIdleSize: 32
19 | masterConnectionPoolSize: 64
20 | readMode: "SLAVE"
21 | slaveAddresses:
22 | - "redis://127.0.0.1:6381"
23 | - "redis://127.0.0.1:6380"
24 | masterAddress: "redis://127.0.0.1:6379"
25 | database: 0
26 | threads: 0
27 | nettyThreads: 0
28 | codec: ! {}
29 | "transportMode":"NIO"
--------------------------------------------------------------------------------
/distributed-redis-lock-example/src/main/resources/redisson-single.yaml:
--------------------------------------------------------------------------------
1 | #单机模式
2 | singleServerConfig:
3 | # 连接空闲超时,单位:毫秒
4 | idleConnectionTimeout: 10000
5 | # 心跳检测时间间隔
6 | pingTimeout: 1000
7 | # 连接超时,单位:毫秒
8 | connectTimeout: 10000
9 | # 命令等待超时,单位:毫秒
10 | timeout: 3000
11 | # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
12 | # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
13 | retryAttempts: 3
14 | # 命令重试发送时间间隔,单位:毫秒
15 | retryInterval: 1000
16 | # 重新连接时间间隔,单位:毫秒
17 | reconnectionTimeout: 3000
18 | # 执行失败最大次数
19 | failedAttempts: 3
20 | # 密码
21 | password: null
22 | # 单个连接最大订阅数量
23 | subscriptionsPerConnection: 5
24 | # 客户端名称
25 | clientName: null
26 | # 节点地址
27 | address: redis://127.0.0.1:6379
28 | # 发布和订阅连接的最小空闲连接数
29 | subscriptionConnectionMinimumIdleSize: 1
30 | # 发布和订阅连接池大小
31 | subscriptionConnectionPoolSize: 50
32 | # 最小空闲连接数
33 | connectionMinimumIdleSize: 32
34 | # 连接池大小
35 | connectionPoolSize: 64
36 | # 数据库编号
37 | database: 0
38 | # DNS监测时间间隔,单位:毫秒
39 | dnsMonitoringInterval: 5000
40 | # 线程池数量,默认值: 当前处理核数量 * 2
41 | #threads: 0
42 | # Netty线程池数量,默认值: 当前处理核数量 * 2
43 | #nettyThreads: 0
44 | # 编码
45 | codec: ! {}
46 | # 传输模式
47 | transportMode : "NIO"
--------------------------------------------------------------------------------
/spring-integration-distributed-lock-examples/src/main/java/spring/integration/distributed/lock/examples/config/RedisLockConfiguration.java:
--------------------------------------------------------------------------------
1 | package spring.integration.distributed.lock.examples.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.data.redis.connection.RedisConnectionFactory;
6 | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
7 | import org.springframework.integration.redis.util.RedisLockRegistry;
8 | import redis.embedded.RedisServer;
9 |
10 | import javax.annotation.PostConstruct;
11 | import javax.annotation.PreDestroy;
12 |
13 | /**
14 | * 嵌入式RedisServer
15 | */
16 | @Configuration
17 | public class RedisLockConfiguration {
18 |
19 | private RedisServer redisServer = new RedisServer(6379);
20 |
21 | @PostConstruct
22 | public void postConstruct() {
23 | redisServer.start();
24 | }
25 |
26 | @PreDestroy
27 | public void preDestroy() {
28 | redisServer.stop();
29 | }
30 |
31 | @Bean
32 | public LettuceConnectionFactory redisConnectionFactory() {
33 | return new LettuceConnectionFactory("127.0.0.1",6379);
34 | }
35 |
36 | @Bean
37 | public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
38 | return new RedisLockRegistry(redisConnectionFactory, "redis-lock");
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | org.springframework.boot
6 | spring-boot-starter-parent
7 | 2.2.7.RELEASE
8 |
9 |
10 | net.ameizi
11 | distributed-lock-examples
12 | 1.0
13 | pom
14 | distributed-lock-examples
15 | Demo project for Spring Boot
16 |
17 | distributed-redis-lock-example
18 | distributed-zookeeper-lock-example
19 | distributed-hazelcast-lock-example
20 | spring-integration-distributed-lock-examples
21 |
22 |
23 | 1.8
24 |
25 |
26 |
27 |
28 | com.github.ekryd.sortpom
29 | sortpom-maven-plugin
30 | 2.11.0
31 |
32 |
33 | verify
34 |
35 | sort
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/spring-integration-distributed-lock-examples/src/main/java/spring/integration/distributed/lock/examples/config/ZookeeperLockConfiguration.java:
--------------------------------------------------------------------------------
1 | package spring.integration.distributed.lock.examples.config;
2 |
3 | import org.apache.curator.RetryPolicy;
4 | import org.apache.curator.framework.CuratorFramework;
5 | import org.apache.curator.framework.CuratorFrameworkFactory;
6 | import org.apache.curator.retry.ExponentialBackoffRetry;
7 | import org.apache.curator.test.TestingServer;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
11 |
12 | /**
13 | * 嵌入式Zookeeper
14 | */
15 | @Configuration
16 | public class ZookeeperLockConfiguration {
17 |
18 | @Bean
19 | public CuratorFramework curatorFramework() {
20 | CuratorFramework curatorFramework = null;
21 | try {
22 | TestingServer testingServer = new TestingServer();
23 | // 重试策略,重试时间1s,重试3次
24 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
25 | // 通过工厂创建 Curator
26 | curatorFramework = CuratorFrameworkFactory.newClient(testingServer.getConnectString(), retryPolicy);
27 | curatorFramework.start();
28 | }catch (Exception e){
29 | e.printStackTrace();
30 | }
31 | return curatorFramework;
32 | }
33 |
34 | @Bean
35 | public ZookeeperLockRegistry zookeeperLockRegistry(CuratorFramework curatorFramework) {
36 | return new ZookeeperLockRegistry(curatorFramework);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/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/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