1 原理
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210514160519646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5,size_16,color_FFFFFF,t_70)
2 工程
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210514160537343.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5,size_16,color_FFFFFF,t_70)
2.1 pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!--zk-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2.2 application.yml
server:
port: 8000
zookeeper:
address: 192.168.38.80:2181
2.3 ZookeeperConfig
@Configuration
public class ZookeeperConfig {
@Value("${zookeeper.address}")
private String address;
@Bean(initMethod = "start")
public CuratorFramework curatorFramework() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
return CuratorFrameworkFactory.newClient(address, retryPolicy);
}
}
2.4 ZookeeperService
@Service
public class ZookeeperService {
@Autowired
private CuratorFramework curatorFramework;
private static final Integer threadSize = 50;
private static Integer number = 0;
public Integer getNumber() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(threadSize);
for (int i = 1; i <= threadSize; i++) {
new Thread(() -> {
InterProcessMutex interProcessMutex = new InterProcessMutex(curatorFramework, "/lock_");
try {
interProcessMutex.acquire();
for (int j = 1; j <= 100; j++) {
number++;
}
System.out.println(Thread.currentThread().getName() + "->" + number);
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
interProcessMutex.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "t" + i).start();
}
countDownLatch.await();
return number;
}
}
2.5 ZookeeperApplication
@SpringBootApplication
public class ZookeeperApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperApplication.class);
}
}
2.6 测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZookeeperApplication.class)
public class MainTest {
@Autowired
private ZookeeperService zookeeperService;
@Test
public void test() throws InterruptedException {
Integer number = zookeeperService.getNumber();
System.out.println(number);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/202105141607157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5,size_16,color_FFFFFF,t_70)