Add code samples for distributed lock (#2610)

* add code samples for distributed lock

Signed-off-by: yaron2 <schneider.yaron@live.com>

* nit

Signed-off-by: yaron2 <schneider.yaron@live.com>

* changes

Signed-off-by: yaron2 <schneider.yaron@live.com>

* changes

Signed-off-by: yaron2 <schneider.yaron@live.com>

* Update daprdocs/content/en/developing-applications/building-blocks/distributed-lock/howto-use-distributed-lock.md

Co-authored-by: Mark Fussell <markfussell@gmail.com>

* Update daprdocs/content/en/developing-applications/building-blocks/distributed-lock/howto-use-distributed-lock.md

Co-authored-by: Mark Fussell <markfussell@gmail.com>

* Update daprdocs/content/en/developing-applications/building-blocks/distributed-lock/howto-use-distributed-lock.md

Co-authored-by: Mark Fussell <markfussell@gmail.com>

* changes

Signed-off-by: yaron2 <schneider.yaron@live.com>

* changes

Signed-off-by: yaron2 <schneider.yaron@live.com>

Co-authored-by: Mark Fussell <markfussell@gmail.com>
This commit is contained in:
Yaron Schneider 2022-07-06 13:03:57 -07:00 committed by GitHub
parent 493fbe5a7b
commit 8eeaf204dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 138 additions and 13 deletions

View File

@ -6,11 +6,19 @@ weight: 2000
description: "Learn how to use distributed locks to provide exclusive access to a resource"
---
Now that you've learned what the Dapr distributed lock API building block provides, learn how it can work in your service. The example below describes an application that aquires a lock. This example uses the Redis lock component to demonstrate how to lock resources.
Now that you've learned what the Dapr distributed lock API building block provides, learn how it can work in your service. The example below describes an application that aquires a lock. This example uses the Redis lock component to demonstrate how to lock resources. For a list of supported lock stores, see [this list](/reference/components-reference/supported-locks/).
The diagram below shows two instances of the same application acquiring a lock, where one instance is successful and the other is denied.
<img src="/images/building-block-lock-example.png" width=1000 alt="The diagram below shows two instances of the same application acquiring a lock, where one instance is successful and the other is denied">
The diagram below shows two instances of the same application, where one instance releases the lock and the other instance is then able to acquire the lock.
<img src="/images/building-block-lock-example.png" width=1000 alt="Diagram showing aquiring a lock from multiple instances of same application">
<img src="/images/building-block-lock-unlock-example.png" width=1000 alt="Diagram showing releasing a lock from multiple instances of same application">
<img src="/images/building-block-lock-multiple-example.png" width=1000 alt="Diagram showing aquiring multiple locks from different applications">
The diagram below shows two instances of different applications, acquiring different locks on the same resource.
<img src="/images/building-block-lock-multiple-example.png" width=1000 alt="The diagram below shows two instances of different applications, acquiring different locks on the same resource">
### Configure a lock component
@ -20,7 +28,7 @@ Save the following component file to the [default components folder]({{< ref "in
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: lock
name: lockstore
spec:
type: lock.redis
metadata:
@ -30,37 +38,154 @@ spec:
value: <PASSWORD>
```
### Get configuration items using Dapr SDKs
### Acquire lock
{{< tabs Dotnet Java Python>}}
{{< tabs HTTP Dotnet Go >}}
{{% codetab %}}
```bash
curl -X POST http://localhost:3500/v1.0-alpha1/lock/lockstore
-H 'Content-Type: application/json'
-d '{"resourceId":"my_file_name", "lockOwner":"random_id_abc123", "expiryInSeconds": 60}'
```
{{% /codetab %}}
{{% codetab %}}
```csharp
using System;
using Dapr.Client;
namespace LockService
{
class Program
{
static async Task Main(string[] args)
{
string DAPR_LOCK_NAME = "lockstore";
var client = new DaprClientBuilder().Build();
using (var fileLock = await client.Lock(DAPR_LOCK_NAME, "my_file_name", "random_id_abc123", 60))
{
if (fileLock.Success)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine($"Failed to lock {fileName}.");
}
}
}
}
}
```
{{% /codetab %}}
{{% codetab %}}
```java
```go
package main
```
import (
"fmt"
{{% /codetab %}}
dapr "github.com/dapr/go-sdk/client"
)
{{% codetab %}}
```python
func main() {
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
defer client.Close()
resp, err := client.TryLockAlpha1(ctx, "lockstore", &dapr.LockRequest{
LockOwner: "random_id_abc123",
ResourceID: "my_file_name",
ExpiryInSeconds: 60,
})
fmt.Println(resp.Success)
}
```
{{% /codetab %}}
{{< /tabs >}}
### Unlock existing lock
{{< tabs HTTP Dotnet Go >}}
{{% codetab %}}
```bash
curl -X POST http://localhost:3500/v1.0-alpha1/unlock/lockstore
-H 'Content-Type: application/json'
-d '{"resourceId":"my_file_name", "lockOwner":"random_id_abc123"}'
```
{{% /codetab %}}
{{% codetab %}}
```csharp
using System;
using Dapr.Client;
namespace LockService
{
class Program
{
static async Task Main(string[] args)
{
string DAPR_LOCK_NAME = "lockstore";
var client = new DaprClientBuilder().Build();
var response = await client.Unlock(DAPR_LOCK_NAME, "my_file_name", "random_id_abc123"));
Console.WriteLine(response.LockStatus);
}
}
}
```
{{% /codetab %}}
{{% codetab %}}
```go
package main
import (
"fmt"
dapr "github.com/dapr/go-sdk/client"
)
func main() {
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
defer client.Close()
resp, err := client.UnlockAlpha1(ctx, "lockstore", &UnlockRequest{
LockOwner: "random_id_abc123",
ResourceID: "my_file_name",
})
fmt.Println(resp.Status)
}
```
{{% /codetab %}}
{{< /tabs >}}
## Next steps
* Read [distributed lock API overview]({{< ref distributed-lock-api-overview.md >}})
* Read [distributed lock API overview]({{< ref distributed-lock-api-overview.md >}})