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:
|
||||
|
||||
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
|
||||
|
||||
|
@ -184,18 +184,26 @@ Below are code examples that leverage Dapr SDKs for service invocation.
|
|||
{{% codetab %}}
|
||||
```csharp
|
||||
|
||||
//headers
|
||||
|
||||
//dependencies
|
||||
using Dapr.Client;
|
||||
using System.Net.Http;
|
||||
|
||||
//code
|
||||
|
||||
CancellationTokenSource source = new CancellationTokenSource();
|
||||
CancellationToken cancellationToken = source.Token;
|
||||
using var client = new DaprClientBuilder().Build();
|
||||
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
||||
await client.InvokeMethodAsync(result);
|
||||
namespace EventService
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
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 %}}
|
||||
|
@ -204,22 +212,27 @@ await client.InvokeMethodAsync(result);
|
|||
{{% codetab %}}
|
||||
```java
|
||||
|
||||
//headers
|
||||
|
||||
//dependencies
|
||||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.DaprClientBuilder;
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
|
||||
//code
|
||||
|
||||
DaprClient daprClient = new DaprClientBuilder().build();
|
||||
var result = daprClient.invokeMethod(
|
||||
"checkout",
|
||||
"checkout/" + orderId,
|
||||
null,
|
||||
HttpExtension.GET,
|
||||
String.class
|
||||
);
|
||||
@SpringBootApplication
|
||||
public class OrderProcessingServiceApplication {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
int orderId = 100;
|
||||
//Using Dapr SDK to invoke a method
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
var result = client.invokeMethod(
|
||||
"checkout",
|
||||
"checkout/" + orderId,
|
||||
null,
|
||||
HttpExtension.GET,
|
||||
String.class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
{{% /codetab %}}
|
||||
|
@ -227,19 +240,19 @@ var result = daprClient.invokeMethod(
|
|||
{{% codetab %}}
|
||||
```python
|
||||
|
||||
//headers
|
||||
|
||||
#dependencies
|
||||
from dapr.clients import DaprClient
|
||||
|
||||
//code
|
||||
|
||||
with DaprClient() as daprClient:
|
||||
result = daprClient.invoke_method(
|
||||
"checkout",
|
||||
f"checkout/{orderId}",
|
||||
data=b'',
|
||||
http_verb="GET"
|
||||
)
|
||||
#code
|
||||
orderId = 100
|
||||
#Using Dapr SDK to invoke a method
|
||||
with DaprClient() as client:
|
||||
result = client.invoke_method(
|
||||
"checkout",
|
||||
f"checkout/{orderId}",
|
||||
data=b'',
|
||||
http_verb="GET"
|
||||
)
|
||||
|
||||
```
|
||||
{{% /codetab %}}
|
||||
|
@ -247,21 +260,25 @@ with DaprClient() as daprClient:
|
|||
{{% codetab %}}
|
||||
```go
|
||||
|
||||
//headers
|
||||
//dependencies
|
||||
import (
|
||||
dapr "github.com/dapr/go-sdk/client"
|
||||
"strconv"
|
||||
dapr "github.com/dapr/go-sdk/client"
|
||||
|
||||
)
|
||||
|
||||
//code
|
||||
|
||||
client, err := dapr.NewClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func main() {
|
||||
orderId := 100
|
||||
//Using Dapr SDK to invoke a method
|
||||
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 %}}
|
||||
|
@ -269,15 +286,20 @@ result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(o
|
|||
{{% codetab %}}
|
||||
```javascript
|
||||
|
||||
//headers
|
||||
|
||||
//dependencies
|
||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||
|
||||
//code
|
||||
const daprHost = "127.0.0.1";
|
||||
|
||||
const daprHost = "127.0.0.1";
|
||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||
const result = await client.invoker.invoke('checkout' , "checkout/" + orderId , HttpMethod.GET);
|
||||
var main = function() {
|
||||
var orderId = 100;
|
||||
//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 %}}
|
||||
|
@ -341,4 +363,4 @@ For more information on tracing and logs see the [observability]({{< ref observa
|
|||
## Related Links
|
||||
|
||||
* [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:
|
||||
|
||||
{{< 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 %}}
|
||||
|
||||
```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 %}}
|
||||
|
@ -44,48 +70,7 @@ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "key1", "value":
|
|||
{{% codetab %}}
|
||||
|
||||
```powershell
|
||||
Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{"key": "key1", "value": "value1", "metadata": {"ttlInSeconds": "120"}}]' -Uri 'http://localhost:3500/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');
|
||||
});
|
||||
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 %}}
|
||||
|
@ -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 >}})
|
||||
- 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