mirror of https://github.com/dapr/docs.git
Added complete code snippets
This commit is contained in:
parent
ad20a76b32
commit
d635712989
|
@ -185,17 +185,41 @@ Below are code examples that leverage Dapr SDKs for service invocation.
|
||||||
```csharp
|
```csharp
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
|
using System;
|
||||||
using Dapr.Client;
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
namespace EventService
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while(true) {
|
||||||
|
System.Threading.Thread.Sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.Next(1,1000);
|
||||||
|
|
||||||
CancellationTokenSource source = new CancellationTokenSource();
|
CancellationTokenSource source = new CancellationTokenSource();
|
||||||
CancellationToken cancellationToken = source.Token;
|
CancellationToken cancellationToken = source.Token;
|
||||||
using var client = new DaprClientBuilder().Build();
|
//Using Dapr SDK to invoke a method
|
||||||
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
using var client = new DaprClientBuilder().Build();
|
||||||
await client.InvokeMethodAsync(result);
|
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
||||||
|
await client.InvokeMethodAsync(result);
|
||||||
|
|
||||||
|
Console.WriteLine("Order requested: " + orderId);
|
||||||
|
Console.WriteLine("Result: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -205,21 +229,45 @@ await client.InvokeMethodAsync(result);
|
||||||
```java
|
```java
|
||||||
|
|
||||||
//dependencies
|
//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;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
DaprClient daprClient = new DaprClientBuilder().build();
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
var result = daprClient.invokeMethod(
|
|
||||||
"checkout",
|
public static void main(String[] args) throws InterruptedException{
|
||||||
"checkout/" + orderId,
|
|
||||||
null,
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
HttpExtension.GET,
|
while(true) {
|
||||||
String.class
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
);
|
Random random = new Random();
|
||||||
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
|
|
||||||
|
//Using Dapr SDK to invoke a method
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
var result = client.invokeMethod(
|
||||||
|
"checkout",
|
||||||
|
"checkout/" + orderId,
|
||||||
|
null,
|
||||||
|
HttpExtension.GET,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
log.info("Order requested: " + orderId);
|
||||||
|
log.info("Result: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -228,18 +276,31 @@ var result = daprClient.invokeMethod(
|
||||||
```python
|
```python
|
||||||
|
|
||||||
#dependencies
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
|
||||||
#code
|
#code
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
|
||||||
|
#Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while True:
|
||||||
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
|
orderId = random.randint(1, 1000)
|
||||||
|
|
||||||
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"
|
||||||
|
)
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
logging.info('Order requested: ' + str(orderId))
|
||||||
|
logging.info('Result: ' + str(result))
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -249,19 +310,37 @@ with DaprClient() as daprClient:
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
import (
|
import (
|
||||||
dapr "github.com/dapr/go-sdk/client"
|
"context"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
"strconv"
|
||||||
|
dapr "github.com/dapr/go-sdk/client"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
func main() {
|
||||||
|
|
||||||
client, err := dapr.NewClient()
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
if err != nil {
|
for i := 0; i < 10; i++ {
|
||||||
panic(err)
|
time.Sleep(5000)
|
||||||
|
orderId := rand.Intn(1000-1) + 1
|
||||||
|
|
||||||
|
//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")
|
||||||
|
|
||||||
|
log.Println("Order requested: " + strconv.Itoa(orderId))
|
||||||
|
log.Println("Result: ")
|
||||||
|
log.Println(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
|
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
@ -270,14 +349,32 @@ result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(o
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
|
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
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);
|
//code
|
||||||
|
var main = function() {
|
||||||
|
|
||||||
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
for(var i=0;i<10;i++) {
|
||||||
|
sleep(5000);
|
||||||
|
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
|
||||||
|
|
||||||
|
//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);
|
||||||
|
|
||||||
|
console.log("Order requested: " + orderId);
|
||||||
|
console.log("Result: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
|
@ -66,7 +66,7 @@ See the instructions [here]({{< ref "setup-state-store" >}}) on how to setup dif
|
||||||
|
|
||||||
## Step 2: Save and retrieve a single state
|
## Step 2: Save and retrieve a single state
|
||||||
|
|
||||||
The following example shows how to a single key/value pair using the Dapr state building block.
|
The following example shows how to save and retrieve a single key/value pair using the Dapr state building block.
|
||||||
|
|
||||||
{{% alert title="Note" color="warning" %}}
|
{{% alert title="Note" color="warning" %}}
|
||||||
It is important to set an app-id, as the state keys are prefixed with this value. If you don't set it one is generated for you at runtime, and the next time you run the command a new one will be generated and you will no longer be able to access previously saved state.
|
It is important to set an app-id, as the state keys are prefixed with this value. If you don't set it one is generated for you at runtime, and the next time you run the command a new one will be generated and you will no longer be able to access previously saved state.
|
||||||
|
@ -79,18 +79,43 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving a sing
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
namespace EventService
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
string DAPR_STORE_NAME = "statestore";
|
||||||
|
|
||||||
string DAPR_STORE_NAME = "statestore";
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while(true) {
|
||||||
|
System.Threading.Thread.Sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.Next(1,1000);
|
||||||
|
|
||||||
using var client = new DaprClientBuilder().Build();
|
//Using Dapr SDK to save and get state
|
||||||
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
|
using var client = new DaprClientBuilder().Build();
|
||||||
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
|
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
|
||||||
Console.WriteLine("Result after get: " + result);
|
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
|
||||||
|
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
|
||||||
|
Console.WriteLine("Result after get: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -103,23 +128,46 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```java
|
```java
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//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.State;
|
||||||
|
import io.dapr.client.domain.TransactionalStateOperation;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
private static final String STATE_STORE_NAME = "statestore";
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
|
|
||||||
DaprClient client = new DaprClientBuilder().build();
|
public static void main(String[] args) throws InterruptedException{
|
||||||
client.saveState(STATE_STORE_NAME, "order_1", Integer.toString(orderId)).block();
|
String STATE_STORE_NAME = "statestore";
|
||||||
Mono<State<String>> result = client.getState(STATE_STORE_NAME, "order_1", String.class);
|
|
||||||
log.info("Result after get" + result);
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while(true) {
|
||||||
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
|
|
||||||
|
//Using Dapr SDK to save and get state
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
client.saveState(STATE_STORE_NAME, "order_1", Integer.toString(orderId)).block();
|
||||||
|
client.saveState(STATE_STORE_NAME, "order_2", Integer.toString(orderId)).block();
|
||||||
|
Mono<State<String>> result = client.getState(STATE_STORE_NAME, "order_1", String.class);
|
||||||
|
log.info("Result after get" + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -135,15 +183,28 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#dependencies
|
|
||||||
|
|
||||||
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
from dapr.clients.grpc._state import StateItem
|
||||||
|
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
|
||||||
|
|
||||||
#code
|
#code
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
|
||||||
DAPR_STORE_NAME = "statestore"
|
DAPR_STORE_NAME = "statestore"
|
||||||
|
|
||||||
with DaprClient() as client:
|
#Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while True:
|
||||||
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
|
orderId = random.randint(1, 1000)
|
||||||
|
|
||||||
|
#Using Dapr SDK to save and get state
|
||||||
|
with DaprClient() as client:
|
||||||
client.save_state(DAPR_STORE_NAME, "order_1", str(orderId))
|
client.save_state(DAPR_STORE_NAME, "order_1", str(orderId))
|
||||||
result = client.get_state(DAPR_STORE_NAME, "order_1")
|
result = client.get_state(DAPR_STORE_NAME, "order_1")
|
||||||
logging.info('Result after get: ' + str(result))
|
logging.info('Result after get: ' + str(result))
|
||||||
|
@ -162,35 +223,49 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import (
|
import (
|
||||||
dapr "github.com/dapr/go-sdk/client"
|
"context"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
"strconv"
|
||||||
|
dapr "github.com/dapr/go-sdk/client"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
func main() {
|
||||||
|
|
||||||
client, err := dapr.NewClient()
|
STATE_STORE_NAME := "statestore"
|
||||||
STATE_STORE_NAME := "statestore"
|
|
||||||
|
|
||||||
if err != nil {
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
panic(err)
|
for i := 0; i < 10; i++ {
|
||||||
|
time.Sleep(5000)
|
||||||
|
orderId := rand.Intn(1000-1) + 1
|
||||||
|
|
||||||
|
//Using Dapr SDK to save and get state
|
||||||
|
client, err := dapr.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if err := client.SaveState(ctx, STATE_STORE_NAME, "order_1", []byte(strconv.Itoa(orderId))); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := client.GetState(ctx, STATE_STORE_NAME, "order_1")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Result after get: ")
|
||||||
|
log.Println(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
if err := client.SaveState(ctx, store, "order_1", []byte(strconv.Itoa(orderId))); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := client.GetState(ctx, store, "order_2")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Result after get: ")
|
|
||||||
log.Println(result)
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -206,24 +281,43 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
|
||||||
const STATE_STORE_NAME = "statestore";
|
|
||||||
|
|
||||||
const daprHost = "127.0.0.1";
|
const daprHost = "127.0.0.1";
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
|
||||||
await client.state.save(STATE_STORE_NAME, [
|
|
||||||
{
|
|
||||||
key: "order_1",
|
|
||||||
value: orderId.toString()
|
|
||||||
}]);
|
|
||||||
var result = await client.state.get(STATE_STORE_NAME, "order_1");
|
|
||||||
console.log("Result after get: " + result);
|
|
||||||
|
|
||||||
|
var main = function() {
|
||||||
|
const STATE_STORE_NAME = "statestore";
|
||||||
|
|
||||||
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
for(var i=0;i<10;i++) {
|
||||||
|
sleep(5000);
|
||||||
|
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
|
||||||
|
|
||||||
|
//Using Dapr SDK to save and get state
|
||||||
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
|
await client.state.save(STATE_STORE_NAME, [
|
||||||
|
{
|
||||||
|
key: "order_1",
|
||||||
|
value: orderId.toString()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "order_2",
|
||||||
|
value: orderId.toString()
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
var result = await client.state.get(STATE_STORE_NAME, "order_1");
|
||||||
|
console.log("Result after get: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -290,15 +384,32 @@ Below are code examples that leverage Dapr SDKs for deleting the state.
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
namespace EventService
|
||||||
string DAPR_STORE_NAME = "statestore";
|
{
|
||||||
|
class Program
|
||||||
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
|
{
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
string DAPR_STORE_NAME = "statestore";
|
||||||
|
//Using Dapr SDK to delete the state
|
||||||
|
using var client = new DaprClientBuilder().Build();
|
||||||
|
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -314,18 +425,33 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```java
|
```java
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//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.State;
|
||||||
|
import io.dapr.client.domain.TransactionalStateOperation;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
private static final String STATE_STORE_NAME = "statestore";
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
|
|
||||||
DaprClient client = new DaprClientBuilder().build();
|
public static void main(String[] args) throws InterruptedException{
|
||||||
String storedEtag = client.getState(STATE_STORE_NAME, "order_1", String.class).block().getEtag();
|
String STATE_STORE_NAME = "statestore";
|
||||||
client.deleteState(STATE_STORE_NAME, "order_1", storedEtag, null).block();
|
|
||||||
|
//Using Dapr SDK to delete the state
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
String storedEtag = client.getState(STATE_STORE_NAME, "order_1", String.class).block().getEtag();
|
||||||
|
client.deleteState(STATE_STORE_NAME, "order_1", storedEtag, null).block();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -341,16 +467,24 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#dependencies
|
|
||||||
|
|
||||||
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
from dapr.clients.grpc._state import StateItem
|
||||||
|
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
|
||||||
|
|
||||||
#code
|
#code
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
|
||||||
DAPR_STORE_NAME = "statestore"
|
DAPR_STORE_NAME = "statestore"
|
||||||
|
|
||||||
|
#Using Dapr SDK to delete the state
|
||||||
with DaprClient() as client:
|
with DaprClient() as client:
|
||||||
client.delete_state(store_name=DAPR_STORE_NAME, key="order_1")
|
client.delete_state(store_name=DAPR_STORE_NAME, key="order_1")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -366,25 +500,30 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import (
|
import (
|
||||||
dapr "github.com/dapr/go-sdk/client"
|
"context"
|
||||||
|
dapr "github.com/dapr/go-sdk/client"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
func main() {
|
||||||
|
|
||||||
client, err := dapr.NewClient()
|
STATE_STORE_NAME := "statestore"
|
||||||
STATE_STORE_NAME := "statestore"
|
|
||||||
|
|
||||||
if err != nil {
|
//Using Dapr SDK to delete the state
|
||||||
panic(err)
|
client, err := dapr.NewClient()
|
||||||
}
|
if err != nil {
|
||||||
defer client.Close()
|
panic(err)
|
||||||
ctx := context.Background()
|
}
|
||||||
|
defer client.Close()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
if err := client.DeleteState(ctx, store, "order_1"); err != nil {
|
if err := client.DeleteState(ctx, STATE_STORE_NAME, "order_1"); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -401,17 +540,26 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
const daprHost = "127.0.0.1";
|
||||||
|
|
||||||
const STATE_STORE_NAME = "statestore";
|
var main = function() {
|
||||||
|
const STATE_STORE_NAME = "statestore";
|
||||||
|
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
//Using Dapr SDK to save and get state
|
||||||
await client.state.delete(STATE_STORE_NAME, "order_1");
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
|
await client.state.delete(STATE_STORE_NAME, "order_1");
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -450,22 +598,41 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving multip
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```java
|
```java
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import io.dapr.client.DaprClient;
|
import io.dapr.client.DaprClient;
|
||||||
import io.dapr.client.DaprClientBuilder;
|
import io.dapr.client.DaprClientBuilder;
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
import io.dapr.client.domain.State;
|
import io.dapr.client.domain.State;
|
||||||
import java.util.List;
|
import io.dapr.client.domain.TransactionalStateOperation;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
private static final String STATE_STORE_NAME = "statestore";
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
|
|
||||||
DaprClient client = new DaprClientBuilder().build();
|
public static void main(String[] args) throws InterruptedException{
|
||||||
Mono<List<State<String>>> resultBulk = client.getBulkState(STATE_STORE_NAME,
|
String STATE_STORE_NAME = "statestore";
|
||||||
Arrays.asList("order_1", "order_2"), String.class);
|
|
||||||
log.info("Result after get bulk" + resultBulk);
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while(true) {
|
||||||
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
|
|
||||||
|
//Using Dapr SDK to retrieve multiple states
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
Mono<List<State<String>>> resultBulk = client.getBulkState(STATE_STORE_NAME,
|
||||||
|
Arrays.asList("order_1", "order_2"), String.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -481,19 +648,31 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#dependencies
|
|
||||||
|
|
||||||
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
from dapr.clients.grpc._state import StateItem
|
from dapr.clients.grpc._state import StateItem
|
||||||
|
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
|
||||||
|
|
||||||
#code
|
#code
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
|
||||||
DAPR_STORE_NAME = "statestore"
|
DAPR_STORE_NAME = "statestore"
|
||||||
|
|
||||||
with DaprClient() as client:
|
#Calling service multiple times with 5 seconds gap in between the calls
|
||||||
client.save_bulk_state(store_name=DAPR_STORE_NAME, states=[StateItem(key="order_2", value=str(orderId))])
|
while True:
|
||||||
result = client.get_bulk_state(store_name=DAPR_STORE_NAME, keys=["order_1", "order_2"], states_metadata={"metakey": "metavalue"}).items
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
logging.info('Result after get bulk: ' + str(result))
|
orderId = random.randint(1, 1000)
|
||||||
|
|
||||||
|
#Using Dapr SDK to save and retrieve multiple states
|
||||||
|
with DaprClient() as client:
|
||||||
|
client.save_bulk_state(store_name=DAPR_STORE_NAME, states=[StateItem(key="order_2", value=str(orderId))])
|
||||||
|
result = client.get_bulk_state(store_name=DAPR_STORE_NAME, keys=["order_1", "order_2"], states_metadata={"metakey": "metavalue"}).items
|
||||||
|
logging.info('Result after get bulk: ' + str(result))
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -510,26 +689,43 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
const daprHost = "127.0.0.1";
|
||||||
|
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
var main = function() {
|
||||||
const STATE_STORE_NAME = "statestore";
|
const STATE_STORE_NAME = "statestore";
|
||||||
await client.state.save(STATE_STORE_NAME, [
|
|
||||||
{
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
key: "order_1",
|
for(var i=0;i<10;i++) {
|
||||||
value: orderId.toString()
|
sleep(5000);
|
||||||
},
|
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
|
||||||
{
|
|
||||||
key: "order_2",
|
//Using Dapr SDK to save and retrieve multiple states
|
||||||
value: orderId.toString()
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
}
|
await client.state.save(STATE_STORE_NAME, [
|
||||||
]);
|
{
|
||||||
result = await client.state.getBulk(STATE_STORE_NAME, ["order_1", "order_2"]);
|
key: "order_1",
|
||||||
console.log("Result after get bulk: " + result);
|
value: orderId.toString()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "order_2",
|
||||||
|
value: orderId.toString()
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
result = await client.state.getBulk(STATE_STORE_NAME, ["order_1", "order_2"]);
|
||||||
|
console.log("Result after get bulk: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -581,24 +777,46 @@ Below are code examples that leverage Dapr SDKs for performing state transaction
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
namespace EventService
|
||||||
string DAPR_STORE_NAME = "statestore";
|
|
||||||
using var client = new DaprClientBuilder().Build();
|
|
||||||
var requests = new List<StateTransactionRequest>()
|
|
||||||
{
|
{
|
||||||
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
|
class Program
|
||||||
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
|
{
|
||||||
};
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
string DAPR_STORE_NAME = "statestore";
|
||||||
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
|
while(true) {
|
||||||
|
System.Threading.Thread.Sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.Next(1,1000);
|
||||||
|
|
||||||
CancellationTokenSource source = new CancellationTokenSource();
|
//Using Dapr SDK to perform the state transactions
|
||||||
CancellationToken cancellationToken = source.Token;
|
using var client = new DaprClientBuilder().Build();
|
||||||
|
var requests = new List<StateTransactionRequest>()
|
||||||
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
|
{
|
||||||
|
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
|
||||||
|
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
|
||||||
|
};
|
||||||
|
CancellationTokenSource source = new CancellationTokenSource();
|
||||||
|
CancellationToken cancellationToken = source.Token;
|
||||||
|
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -614,24 +832,48 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```java
|
```java
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//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.State;
|
import io.dapr.client.domain.State;
|
||||||
import io.dapr.client.domain.TransactionalStateOperation;
|
import io.dapr.client.domain.TransactionalStateOperation;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
@SpringBootApplication
|
||||||
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
private static final String STATE_STORE_NAME = "statestore";
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
|
|
||||||
DaprClient client = new DaprClientBuilder().build();
|
public static void main(String[] args) throws InterruptedException{
|
||||||
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
|
String STATE_STORE_NAME = "statestore";
|
||||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
|
||||||
new State<>("order_3", Integer.toString(orderId), "")));
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE,
|
while(true) {
|
||||||
new State<>("order_2")));
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
|
Random random = new Random();
|
||||||
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
|
|
||||||
|
//Using Dapr SDK to perform the state transactions
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
|
||||||
|
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
||||||
|
new State<>("order_3", Integer.toString(orderId), "")));
|
||||||
|
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE,
|
||||||
|
new State<>("order_2")));
|
||||||
|
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -647,28 +889,40 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#dependencies
|
|
||||||
|
|
||||||
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
from dapr.clients.grpc._state import StateItem
|
from dapr.clients.grpc._state import StateItem
|
||||||
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
|
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
|
||||||
|
|
||||||
#code
|
#code
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
|
||||||
DAPR_STORE_NAME = "statestore"
|
DAPR_STORE_NAME = "statestore"
|
||||||
with DaprClient() as client:
|
|
||||||
client.execute_state_transaction(store_name=DAPR_STORE_NAME, operations=[
|
#Calling service multiple times with 5 seconds gap in between the calls
|
||||||
TransactionalStateOperation(
|
while True:
|
||||||
operation_type=TransactionOperationType.upsert,
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
key="order_3",
|
orderId = random.randint(1, 1000)
|
||||||
data=str(orderId)),
|
|
||||||
TransactionalStateOperation(key="order_3", data=str(orderId)),
|
#Using Dapr SDK to perform the state transactions
|
||||||
TransactionalStateOperation(
|
with DaprClient() as client:
|
||||||
operation_type=TransactionOperationType.delete,
|
client.execute_state_transaction(store_name=DAPR_STORE_NAME, operations=[
|
||||||
key="order_2",
|
TransactionalStateOperation(
|
||||||
data=str(orderId)),
|
operation_type=TransactionOperationType.upsert,
|
||||||
TransactionalStateOperation(key="order_2", data=str(orderId))
|
key="order_3",
|
||||||
])
|
data=str(orderId)),
|
||||||
|
TransactionalStateOperation(key="order_3", data=str(orderId)),
|
||||||
|
TransactionalStateOperation(
|
||||||
|
operation_type=TransactionOperationType.delete,
|
||||||
|
key="order_2",
|
||||||
|
data=str(orderId)),
|
||||||
|
TransactionalStateOperation(key="order_2", data=str(orderId))
|
||||||
|
])
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -684,30 +938,46 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//dependencies
|
|
||||||
|
|
||||||
|
//dependencies
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
const daprHost = "127.0.0.1";
|
||||||
|
|
||||||
const STATE_STORE_NAME = "statestore";
|
var main = function() {
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
const STATE_STORE_NAME = "statestore";
|
||||||
await client.state.transaction(STATE_STORE_NAME, [
|
|
||||||
{
|
//Calling service multiple times with 5 seconds gap in between the calls
|
||||||
operation: "upsert",
|
for(var i=0;i<10;i++) {
|
||||||
request: {
|
sleep(5000);
|
||||||
key: "order_3",
|
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
|
||||||
value: orderId.toString()
|
|
||||||
|
//Using Dapr SDK to save and retrieve multiple states
|
||||||
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
|
await client.state.transaction(STATE_STORE_NAME, [
|
||||||
|
{
|
||||||
|
operation: "upsert",
|
||||||
|
request: {
|
||||||
|
key: "order_3",
|
||||||
|
value: orderId.toString()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
operation: "delete",
|
||||||
|
request: {
|
||||||
|
key: "order_2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
{
|
|
||||||
operation: "delete",
|
function sleep(ms) {
|
||||||
request: {
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
key: "order_2"
|
}
|
||||||
}
|
|
||||||
}
|
main();
|
||||||
]);
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ You can then interact with the server using the client port: `localhost:4222`.
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
Install NATS on Kubernetes by using the [kubectl](https://docs.nats.io/nats-on-kubernetes/minimal-setup):
|
Install NATS on Kubernetes by using the [kubectl](https://docs.nats.io/running-a-nats-service/introduction/running/nats-kubernetes/minimal-setup#minimal-nats-setup):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Single server NATS
|
# Single server NATS
|
||||||
|
|
Loading…
Reference in New Issue