Skip to content

Commit 559d2c8

Browse files
akkwXunzhuoseeflood
authored
docs: lock demo (#783)
Co-authored-by: Xunzhuo <[email protected]> Co-authored-by: seeflood <[email protected]>
1 parent 8cfe116 commit 559d2c8

File tree

2 files changed

+95
-13
lines changed

2 files changed

+95
-13
lines changed

docs/en/start/lock/start.md

+42
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ The layotto file will be generated in the directory, run it:
7676
<!-- tabs:end -->
7777

7878
## step 2. Run the client program, call Layotto to add, delete, modify and query
79+
<!-- tabs:start -->
80+
### **Go**
7981

8082
```shell
8183
cd ${project_path}/demo/lock/common/
@@ -98,6 +100,46 @@ client2 succeeded in unlocking
98100
Demo success!
99101
```
100102

103+
### **Java**
104+
105+
Download java sdk and examples:
106+
107+
```shell @if.not.exist java-sdk
108+
git clone https://github.com/layotto/java-sdk
109+
```
110+
111+
Change directory:
112+
113+
```shell
114+
cd java-sdk
115+
```
116+
117+
Build:
118+
119+
```shell @if.not.exist examples-lock/target/examples-lock-jar-with-dependencies.jar
120+
# build example jar
121+
mvn -f examples-lock/pom.xml clean package
122+
```
123+
124+
Run the examples:
125+
126+
```shell
127+
java -jar examples-lock/target/examples-lock-jar-with-dependencies.jar
128+
```
129+
130+
And you will see:
131+
132+
```bash
133+
TryLockResponse{success=true}
134+
TryLockResponse{success=true}
135+
TryLockResponse{success=true}
136+
UnlockResponse{status=SUCCESS}
137+
TryLockResponse{success=true}
138+
UnlockResponse{status=LOCK_UNEXIST}
139+
```
140+
141+
<!-- tabs:end -->
142+
101143
## Next Step
102144
### What did this client Demo do?
103145
The demo client program uses the golang version SDK provided by Layotto, calls the Layotto distributed lock API, and starts multiple goroutines to do locking and unlocking operations.

docs/zh/start/lock/start.md

+53-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
# 基于 Redis 使用分布式锁
2-
3-
## 快速开始
1+
# 快速开始: 基于 Redis 使用分布式锁
42

53
该示例展示了如何通过Layotto调用 Redis,进行分布式锁的抢锁、解锁操作。
64

75
该示例的架构如下图,启动的进程有:Redis、Layotto、一个演示用的client程序(其中包含两个协程,并发抢锁)
86

97
![img.png](../../../img/lock/img.png)
10-
### step 1. 部署 Redis 和 Layotto
8+
## step 1. 部署 Redis 和 Layotto
119

1210
<!-- tabs:start -->
13-
#### **使用 Docker Compose**
11+
### **使用 Docker Compose**
1412
您可以用 docker-compose 启动 Redis 和 Layotto
1513

1614
```bash
@@ -19,13 +17,13 @@ cd docker/layotto-redis
1917
docker-compose up -d
2018
```
2119

22-
#### **本地编译(不适合 Windows)**
20+
### **本地编译(不适合 Windows)**
2321
您可以使用 Docker 运行 Redis,然后本地编译、运行 Layotto。
2422

2523
> [!TIP|label: 不适合 Windows 用户]
2624
> Layotto 在 Windows 下会编译失败。建议 Windows 用户使用 docker-compose 部署
2725
28-
#### step 1.1. 用 Docker 运行 Redis
26+
### step 1.1. 用 Docker 运行 Redis
2927

3028
1. 取最新版的 Redis 镜像。
3129
这里我们拉取官方的最新版本的镜像:
@@ -55,7 +53,7 @@ docker run -itd --name redis-test -p 6380:6379 redis
5553

5654
-p 6380:6379:映射容器服务的 6379 端口到宿主机的 6380 端口。外部可以直接通过宿主机ip:6380 访问到 Redis 的服务。
5755

58-
#### step 1.2. 运行 Layotto
56+
### step 1.2. 运行 Layotto
5957

6058
将项目代码下载到本地后,切换代码目录:
6159

@@ -77,7 +75,9 @@ go build -o layotto
7775

7876
<!-- tabs:end -->
7977

80-
### step 2. 运行客户端程序,调用Layotto抢锁/解锁
78+
## step 2. 运行客户端程序,调用Layotto抢锁/解锁
79+
<!-- tabs:start -->
80+
### **Go**
8181

8282
```shell
8383
cd ${project_path}/demo/lock/common/
@@ -100,8 +100,48 @@ client2 succeeded in unlocking
100100
Demo success!
101101
```
102102

103-
### 下一步
104-
#### 这个客户端程序做了什么?
103+
### **Java**
104+
105+
下载 java sdk 和示例代码:
106+
107+
```shell @if.not.exist java-sdk
108+
git clone https://github.com/layotto/java-sdk
109+
```
110+
111+
切换目录:
112+
113+
```shell
114+
cd java-sdk
115+
```
116+
117+
构建:
118+
119+
```shell @if.not.exist examples-lock/target/examples-lock-jar-with-dependencies.jar
120+
# build example jar
121+
mvn -f examples-lock/pom.xml clean package
122+
```
123+
124+
运行:
125+
126+
```shell
127+
java -jar examples-lock/target/examples-lock-jar-with-dependencies.jar
128+
```
129+
130+
打印出以下信息说明运行成功:
131+
132+
```bash
133+
TryLockResponse{success=true}
134+
TryLockResponse{success=true}
135+
TryLockResponse{success=true}
136+
UnlockResponse{status=SUCCESS}
137+
TryLockResponse{success=true}
138+
UnlockResponse{status=LOCK_UNEXIST}
139+
```
140+
141+
<!-- tabs:end -->
142+
143+
## 下一步
144+
### 这个客户端程序做了什么?
105145
示例客户端程序中使用了Layotto提供的golang版本sdk,调用Layotto 分布式锁API,启动多个协程进行抢锁、解锁操作。
106146

107147
sdk位于`sdk`目录下,用户可以通过sdk调用Layotto提供的API。
@@ -111,9 +151,9 @@ sdk位于`sdk`目录下,用户可以通过sdk调用Layotto提供的API。
111151
其实sdk只是对grpc很薄的封装,用sdk约等于直接用grpc调。
112152

113153

114-
#### 细节以后再说,继续体验其他API
154+
### 细节以后再说,继续体验其他API
115155
通过左侧的导航栏,继续体验别的API吧!
116156

117-
#### 了解分布式锁 API的实现原理
157+
### 了解分布式锁 API的实现原理
118158

119159
如果您对实现原理感兴趣,或者想扩展一些功能,可以阅读[分布式锁 API的设计文档](zh/design/lock/lock-api-design.md)

0 commit comments

Comments
 (0)