mirror of https://github.com/dapr/docs.git
Merge pull request #2000 from amulyavarote/feature/state_management
Provide consistent examples of state management for all SDKs
This commit is contained in:
commit
4a8fbf10da
|
@ -10,9 +10,9 @@ This article describe how to deploy services each with an unique application ID,
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
|
||||||
The below code examples loosely describe an application that processes orders. In the examples, there are two services - an order processing service and a checkout service. Both services have Dapr sidecars and the order processing service uses Dapr to invoke the checkout method in the checkout service.
|
The below code examples loosely describes an application that processes orders. In the examples, there are two services - an order processing service and a checkout service. Both services have Dapr sidecars and the order processing service uses Dapr to invoke the checkout method in the checkout service.
|
||||||
|
|
||||||
<img src="/images/service_invocation_eg.png" width=1000 height=500 alt="Diagram showing service invocation of example service">
|
<img src="/images/building-block-service-invocation-example.png" width=1000 height=500 alt="Diagram showing service invocation of example service">
|
||||||
|
|
||||||
## Step 1: Choose an ID for your service
|
## Step 1: Choose an ID for your service
|
||||||
|
|
||||||
|
@ -184,18 +184,26 @@ Below are code examples that leverage Dapr SDKs for service invocation.
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```csharp
|
```csharp
|
||||||
|
|
||||||
//headers
|
//dependencies
|
||||||
|
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
namespace EventService
|
||||||
CancellationTokenSource source = new CancellationTokenSource();
|
{
|
||||||
CancellationToken cancellationToken = source.Token;
|
class Program
|
||||||
using var client = new DaprClientBuilder().Build();
|
{
|
||||||
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
static async Task Main(string[] args)
|
||||||
await client.InvokeMethodAsync(result);
|
{
|
||||||
|
int orderId = 100;
|
||||||
|
CancellationTokenSource source = new CancellationTokenSource();
|
||||||
|
CancellationToken cancellationToken = source.Token;
|
||||||
|
//Using Dapr SDK to invoke a method
|
||||||
|
using var client = new DaprClientBuilder().Build();
|
||||||
|
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
||||||
|
await client.InvokeMethodAsync(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -204,22 +212,27 @@ await client.InvokeMethodAsync(result);
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```java
|
```java
|
||||||
|
|
||||||
//headers
|
//dependencies
|
||||||
|
|
||||||
import io.dapr.client.DaprClient;
|
import io.dapr.client.DaprClient;
|
||||||
import io.dapr.client.DaprClientBuilder;
|
import io.dapr.client.DaprClientBuilder;
|
||||||
import io.dapr.client.domain.HttpExtension;
|
import io.dapr.client.domain.HttpExtension;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
DaprClient daprClient = new DaprClientBuilder().build();
|
public class OrderProcessingServiceApplication {
|
||||||
var result = daprClient.invokeMethod(
|
public static void main(String[] args) throws InterruptedException {
|
||||||
"checkout",
|
int orderId = 100;
|
||||||
"checkout/" + orderId,
|
//Using Dapr SDK to invoke a method
|
||||||
null,
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
HttpExtension.GET,
|
var result = client.invokeMethod(
|
||||||
String.class
|
"checkout",
|
||||||
);
|
"checkout/" + orderId,
|
||||||
|
null,
|
||||||
|
HttpExtension.GET,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -227,19 +240,19 @@ var result = daprClient.invokeMethod(
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```python
|
```python
|
||||||
|
|
||||||
//headers
|
#dependencies
|
||||||
|
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
|
||||||
//code
|
#code
|
||||||
|
orderId = 100
|
||||||
with DaprClient() as daprClient:
|
#Using Dapr SDK to invoke a method
|
||||||
result = daprClient.invoke_method(
|
with DaprClient() as client:
|
||||||
"checkout",
|
result = client.invoke_method(
|
||||||
f"checkout/{orderId}",
|
"checkout",
|
||||||
data=b'',
|
f"checkout/{orderId}",
|
||||||
http_verb="GET"
|
data=b'',
|
||||||
)
|
http_verb="GET"
|
||||||
|
)
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -247,21 +260,25 @@ with DaprClient() as daprClient:
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```go
|
```go
|
||||||
|
|
||||||
//headers
|
//dependencies
|
||||||
import (
|
import (
|
||||||
dapr "github.com/dapr/go-sdk/client"
|
"strconv"
|
||||||
|
dapr "github.com/dapr/go-sdk/client"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
func main() {
|
||||||
client, err := dapr.NewClient()
|
orderId := 100
|
||||||
if err != nil {
|
//Using Dapr SDK to invoke a method
|
||||||
panic(err)
|
client, err := dapr.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
ctx := context.Background()
|
||||||
|
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
|
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -269,15 +286,20 @@ result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(o
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
//headers
|
//dependencies
|
||||||
|
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
const daprHost = "127.0.0.1";
|
||||||
|
|
||||||
const daprHost = "127.0.0.1";
|
var main = function() {
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
var orderId = 100;
|
||||||
const result = await client.invoker.invoke('checkout' , "checkout/" + orderId , HttpMethod.GET);
|
//Using Dapr SDK to invoke a method
|
||||||
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
|
const result = await client.invoker.invoke('checkout' , "checkout/" + orderId , HttpMethod.GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -341,4 +363,4 @@ For more information on tracing and logs see the [observability]({{< ref observa
|
||||||
## Related Links
|
## Related Links
|
||||||
|
|
||||||
* [Service invocation overview]({{< ref service-invocation-overview.md >}})
|
* [Service invocation overview]({{< ref service-invocation-overview.md >}})
|
||||||
* [Service invocation API specification]({{< ref service_invocation_api.md >}})
|
* [Service invocation API specification]({{< ref service_invocation_api.md >}})
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -31,12 +31,38 @@ Please refer to the TTL column in the tables at [state store components]({{< ref
|
||||||
|
|
||||||
State TTL can be set in the metadata as part of the state store set request:
|
State TTL can be set in the metadata as part of the state store set request:
|
||||||
|
|
||||||
{{< tabs "HTTP API (Bash)" "HTTP API (PowerShell)" "Python SDK" "PHP SDK">}}
|
{{< tabs Python "HTTP API (Bash)" "HTTP API (PowerShell)">}}
|
||||||
|
|
||||||
|
{{% codetab %}}
|
||||||
|
|
||||||
|
```python
|
||||||
|
#dependencies
|
||||||
|
|
||||||
|
from dapr.clients import DaprClient
|
||||||
|
|
||||||
|
#code
|
||||||
|
|
||||||
|
DAPR_STORE_NAME = "statestore"
|
||||||
|
|
||||||
|
with DaprClient() as client:
|
||||||
|
client.save_state(DAPR_STORE_NAME, "order_1", str(orderId), metadata=(
|
||||||
|
('ttlInSeconds', '120')
|
||||||
|
))
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-grpc-port 60001 -- python3 OrderProcessingService.py
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -X POST -H "Content-Type: application/json" -d '[{ "key": "key1", "value": "value1", "metadata": { "ttlInSeconds": "120" } }]' http://localhost:3500/v1.0/state/statestore
|
curl -X POST -H "Content-Type: application/json" -d '[{ "key": "order_1", "value": "250", "metadata": { "ttlInSeconds": "120" } }]' http://localhost:3601/v1.0/state/statestore
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -44,48 +70,7 @@ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "key1", "value":
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{"key": "key1", "value": "value1", "metadata": {"ttlInSeconds": "120"}}]' -Uri 'http://localhost:3500/v1.0/state/statestore'
|
Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{"key": "order_1", "value": "250", "metadata": {"ttlInSeconds": "120"}}]' -Uri 'http://localhost:3601/v1.0/state/statestore'
|
||||||
```
|
|
||||||
|
|
||||||
{{% /codetab %}}
|
|
||||||
|
|
||||||
{{% codetab %}}
|
|
||||||
|
|
||||||
```python
|
|
||||||
from dapr.clients import DaprClient
|
|
||||||
|
|
||||||
with DaprClient() as d:
|
|
||||||
d.save_state(
|
|
||||||
store_name="statestore",
|
|
||||||
key="myFirstKey",
|
|
||||||
value="myFirstValue",
|
|
||||||
metadata=(
|
|
||||||
('ttlInSeconds', '120')
|
|
||||||
)
|
|
||||||
)
|
|
||||||
print("State has been stored")
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
{{% /codetab %}}
|
|
||||||
|
|
||||||
{{% codetab %}}
|
|
||||||
|
|
||||||
Save the following in `state-example.php`:
|
|
||||||
|
|
||||||
```php
|
|
||||||
<?php
|
|
||||||
require_once __DIR__.'/vendor/autoload.php';
|
|
||||||
|
|
||||||
$app = \Dapr\App::create();
|
|
||||||
$app->run(function(\Dapr\State\StateManager $stateManager, \Psr\Log\LoggerInterface $logger) {
|
|
||||||
$stateManager->save_state(store_name: 'statestore', item: new \Dapr\State\StateItem(
|
|
||||||
key: 'myFirstKey',
|
|
||||||
value: 'myFirstValue',
|
|
||||||
metadata: ['ttlInSeconds' => '120']
|
|
||||||
));
|
|
||||||
$logger->alert('State has been stored');
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -98,4 +83,4 @@ See [this guide]({{< ref state_api.md >}}) for a reference on the state API.
|
||||||
|
|
||||||
- Learn [how to use key value pairs to persist a state]({{< ref howto-get-save-state.md >}})
|
- Learn [how to use key value pairs to persist a state]({{< ref howto-get-save-state.md >}})
|
||||||
- List of [state store components]({{< ref supported-state-stores >}})
|
- List of [state store components]({{< ref supported-state-stores >}})
|
||||||
- Read the [API reference]({{< ref state_api.md >}})
|
- Read the [API reference]({{< ref state_api.md >}})
|
Before Width: | Height: | Size: 181 KiB After Width: | Height: | Size: 181 KiB |
Binary file not shown.
After Width: | Height: | Size: 120 KiB |
Loading…
Reference in New Issue