Skip to content

Commit 19716e9

Browse files
committed
Init project
0 parents  commit 19716e9

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target

Diff for: pom.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>2.3.6.RELEASE</version>
11+
</parent>
12+
<groupId>com.github.helllogithub</groupId>
13+
<artifactId>curator-springboot-demo</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-integration</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.integration</groupId>
31+
<artifactId>spring-integration-zookeeper</artifactId>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<finalName>curator-springboot-demo</finalName>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</project>

Diff for: src/main/java/com/github/hellogithub/Boot.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.hellogithub;
2+
import org.springframework.boot.SpringApplication;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
/**
6+
* @author: junjiexun
7+
* @date: 2020/12/8 4:09 下午
8+
* @description:
9+
*/
10+
@SpringBootApplication(scanBasePackages = {"com.github"})
11+
public class Boot {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(Boot.class, args);
15+
}
16+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.hellogithub;
2+
3+
import org.springframework.integration.support.locks.LockRegistry;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import javax.annotation.Resource;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.locks.Lock;
10+
11+
/**
12+
* @author: junjiexun
13+
* @date: 2020/12/8 4:31 下午
14+
* @description:
15+
*/
16+
@RestController
17+
public class TestController {
18+
19+
@Resource
20+
private LockRegistry lockRegistry;
21+
22+
@GetMapping("debug")
23+
public String dubug() {
24+
System.out.println(lockRegistry);
25+
return "SSSS";
26+
}
27+
28+
@GetMapping("/lock10")
29+
public String lock10() {
30+
System.out.println("lock10 start " + System.currentTimeMillis());
31+
final Lock lock = lockRegistry.obtain("lock");
32+
try {
33+
lock.lock();
34+
System.out.println("lock10 get lock success " + System.currentTimeMillis());
35+
TimeUnit.SECONDS.sleep(10);
36+
} catch (Exception e) {
37+
} finally {
38+
lock.unlock();
39+
}
40+
return "OK";
41+
}
42+
43+
@GetMapping("/immediate")
44+
public String immediate() {
45+
System.out.println("immediate start " + System.currentTimeMillis());
46+
final Lock lock = lockRegistry.obtain("lock");
47+
try {
48+
lock.lock();
49+
System.out.println("immediate get lock success " + System.currentTimeMillis());
50+
} finally {
51+
lock.unlock();
52+
}
53+
return "immediate return";
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.hellogithub;
2+
3+
import org.apache.curator.framework.CuratorFramework;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.integration.zookeeper.config.CuratorFrameworkFactoryBean;
8+
import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
9+
10+
/**
11+
* @author: junjiexun
12+
* @date: 2020/12/8 4:34 下午
13+
* @description:
14+
*/
15+
@Configuration
16+
public class ZookeeperLockConfiguration {
17+
@Value("${zookeeper.host:127.0.0.1:2181}")
18+
private String zkUrl;
19+
20+
@Bean
21+
public CuratorFrameworkFactoryBean curatorFrameworkFactoryBean() {
22+
return new CuratorFrameworkFactoryBean(zkUrl);
23+
}
24+
25+
@Bean
26+
public ZookeeperLockRegistry zookeeperLockRegistry(CuratorFramework curatorFramework) {
27+
28+
return new ZookeeperLockRegistry(curatorFramework, "/HG-lock");
29+
}
30+
}

Diff for: src/main/resources/application.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server:
2+
port: 9999

0 commit comments

Comments
 (0)