Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: lock demo #783

Merged
merged 8 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/en/start/lock/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ The layotto file will be generated in the directory, run it:
<!-- tabs:end -->

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

```shell
cd ${project_path}/demo/lock/common/
Expand All @@ -98,6 +100,46 @@ client2 succeeded in unlocking
Demo success!
```

### **Java**

Download java sdk and examples:

```shell @if.not.exist java-sdk
git clone https://github.com/layotto/java-sdk
```

Change directory:

```shell
cd java-sdk
```

Build:

```shell @if.not.exist examples-lock/target/examples-lock-jar-with-dependencies.jar
# build example jar
mvn -f examples-lock/pom.xml clean package
```

Run the examples:

```shell
java -jar examples-lock/target/examples-lock-jar-with-dependencies.jar
```

And you will see:

```bash
TryLockResponse{success=true}
TryLockResponse{success=true}
TryLockResponse{success=true}
UnlockResponse{status=SUCCESS}
TryLockResponse{success=true}
UnlockResponse{status=LOCK_UNEXIST}
```

<!-- tabs:end -->

## Next Step
### What did this client Demo do?
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.
Expand Down
66 changes: 53 additions & 13 deletions docs/zh/start/lock/start.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# 基于 Redis 使用分布式锁

## 快速开始
# 快速开始: 基于 Redis 使用分布式锁

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

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

![img.png](../../../img/lock/img.png)
### step 1. 部署 Redis 和 Layotto
## step 1. 部署 Redis 和 Layotto

<!-- tabs:start -->
#### **使用 Docker Compose**
### **使用 Docker Compose**
您可以用 docker-compose 启动 Redis 和 Layotto

```bash
Expand All @@ -19,13 +17,13 @@ cd docker/layotto-redis
docker-compose up -d
```

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

> [!TIP|label: 不适合 Windows 用户]
> Layotto 在 Windows 下会编译失败。建议 Windows 用户使用 docker-compose 部署

#### step 1.1. 用 Docker 运行 Redis
### step 1.1. 用 Docker 运行 Redis

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

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

#### step 1.2. 运行 Layotto
### step 1.2. 运行 Layotto

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

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

<!-- tabs:end -->

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

```shell
cd ${project_path}/demo/lock/common/
Expand All @@ -100,8 +100,48 @@ client2 succeeded in unlocking
Demo success!
```

### 下一步
#### 这个客户端程序做了什么?
### **Java**

下载 java sdk 和示例代码:

```shell @if.not.exist java-sdk
git clone https://github.com/layotto/java-sdk
```

切换目录:

```shell
cd java-sdk
```

构建:

```shell @if.not.exist examples-lock/target/examples-lock-jar-with-dependencies.jar
# build example jar
mvn -f examples-lock/pom.xml clean package
```

运行:

```shell
java -jar examples-lock/target/examples-lock-jar-with-dependencies.jar
```

打印出以下信息说明运行成功:

```bash
TryLockResponse{success=true}
TryLockResponse{success=true}
TryLockResponse{success=true}
UnlockResponse{status=SUCCESS}
TryLockResponse{success=true}
UnlockResponse{status=LOCK_UNEXIST}
```

<!-- tabs:end -->

## 下一步
### 这个客户端程序做了什么?
示例客户端程序中使用了Layotto提供的golang版本sdk,调用Layotto 分布式锁API,启动多个协程进行抢锁、解锁操作。

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


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

#### 了解分布式锁 API的实现原理
### 了解分布式锁 API的实现原理

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