mirror of https://github.com/dapr/docs.git
add multi-app run
Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com>
This commit is contained in:
parent
0f24ef1459
commit
3706c74591
|
@ -14,6 +14,761 @@ Let's take a look at Dapr's [Publish and Subscribe (Pub/sub) building block]({{<
|
|||
|
||||
<img src="/images/pubsub-quickstart/pubsub-diagram.png" width=800 style="padding-bottom:15px;">
|
||||
|
||||
You can try out this pub/sub quickstart by either:
|
||||
|
||||
- [Running all applications in this sample simultaneously with the Multi-App Run template file]({{< ref "#run-using-multi-app-run" >}}), or
|
||||
- [Running one application at a time]({{< ref "#run-one-application-at-a-time" >}})
|
||||
|
||||
## Run using Multi-App Run
|
||||
|
||||
{{% alert title="Note" color="primary" %}}
|
||||
[Multi-App Run]({{< ref multi-app-dapr-run >}}) is currently a preview feature only supported in Linux/MacOS.
|
||||
{{% /alert %}}
|
||||
|
||||
Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
<!-- Python -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Python 3.7+ installed](https://www.python.org/downloads/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/pub_sub).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstarts directory, navigate into the pub/sub directory:
|
||||
|
||||
```bash
|
||||
cd pub_sub/python/sdk
|
||||
```
|
||||
|
||||
### Step 3: Run the publisher and subscriber
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` subscriber
|
||||
- The `checkout` publisher
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 1 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 1 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 2 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 2 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 3 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 3 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 4 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 4 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 5 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 5 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 6 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 6 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 7 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 7 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 8 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 8 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 9 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 9 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 10 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 10 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
When you ran `dapr init` during Dapr install, the following YAML files were generated in the `.dapr/components` directory:
|
||||
- [`dapr.yaml` Multi-App Run template file]({{< ref "#dapryaml-multi-app-run-template-file" >}})
|
||||
- [`pubsub.yaml` component file]({{< ref "#pubsubyaml-component-file" >}})
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-subscriber" >}}) and [publisher]({{< ref "#checkout-publisher" >}}) applications.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../components/
|
||||
apps:
|
||||
- appID: order-processor-sdk
|
||||
appDirPath: ./order-processor/
|
||||
appPort: 6001
|
||||
command: ["uvicorn", "app:app"]
|
||||
- appID: checkout-sdk
|
||||
appDirPath: ./checkout/
|
||||
command: ["python3", "app.py"]
|
||||
```
|
||||
|
||||
#### `pubsub.yaml` component file
|
||||
|
||||
With the `pubsub.yaml` component, you can easily swap out underlying components without application code changes.
|
||||
|
||||
The Redis `pubsub.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: orderpubsub
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
```
|
||||
|
||||
In the component YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component.
|
||||
- `spec/metadata` defines the connection to the instance of the component.
|
||||
- `scopes` specify which application can use the component.
|
||||
|
||||
#### `order-processor` subscriber
|
||||
|
||||
In the `order-processor` subscriber, you subscribe to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. This enables your app code to talk to the Redis component instance through the Dapr sidecar.
|
||||
|
||||
```py
|
||||
# Register Dapr pub/sub subscriptions
|
||||
@app.route('/dapr/subscribe', methods=['GET'])
|
||||
def subscribe():
|
||||
subscriptions = [{
|
||||
'pubsubname': 'orderpubsub',
|
||||
'topic': 'orders',
|
||||
'route': 'orders'
|
||||
}]
|
||||
print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions))
|
||||
return jsonify(subscriptions)
|
||||
|
||||
|
||||
# Dapr subscription in /dapr/subscribe sets up this route
|
||||
@app.route('/orders', methods=['POST'])
|
||||
def orders_subscriber():
|
||||
event = from_http(request.headers, request.get_data())
|
||||
print('Subscriber received : ' + event.data['orderid'], flush=True)
|
||||
return json.dumps({'success': True}), 200, {
|
||||
'ContentType': 'application/json'}
|
||||
|
||||
|
||||
app.run(port=5001)
|
||||
```
|
||||
|
||||
#### `checkout` publisher
|
||||
|
||||
In the `checkout` publisher, you publish the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
|
||||
|
||||
```python
|
||||
with DaprClient() as client:
|
||||
# Publish an event/message using Dapr PubSub
|
||||
result = client.publish_event(
|
||||
pubsub_name='orderpubsub',
|
||||
topic_name='orders',
|
||||
data=json.dumps(order),
|
||||
data_content_type='application/json',
|
||||
)
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- JavaScript -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest Node.js installed](https://nodejs.org/download/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/pub_sub).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstarts directory, navigate into the pub/sub directory:
|
||||
|
||||
```bash
|
||||
cd pub_sub/javascript/sdk
|
||||
```
|
||||
|
||||
### Step 3: Run the publisher and subscriber
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` subscriber
|
||||
- The `checkout` publisher
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 1 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 1 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 2 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 2 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 3 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 3 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 4 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 4 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 5 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 5 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 6 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 6 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 7 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 7 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 8 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 8 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 9 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 9 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 10 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 10 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
When you ran `dapr init` during Dapr install, the following YAML files were generated in the `.dapr/components` directory:
|
||||
- [`dapr.yaml` Multi-App Run template file]({{< ref "#dapryaml-multi-app-run-template-file" >}})
|
||||
- [`pubsub.yaml` component file]({{< ref "#pubsubyaml-component-file" >}})
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-subscriber" >}}) and [publisher]({{< ref "#checkout-publisher" >}}) applications.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../components/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
appPort: 5002
|
||||
command: ["npm", "run", "start"]
|
||||
- appID: checkout-sdk
|
||||
appDirPath: ./checkout/
|
||||
command: ["npm", "run", "start"]
|
||||
```
|
||||
|
||||
#### `pubsub.yaml` component file
|
||||
|
||||
With the `pubsub.yaml` component, you can easily swap out underlying components without application code changes.
|
||||
|
||||
The Redis `pubsub.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: orderpubsub
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
```
|
||||
|
||||
In the component YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component.
|
||||
- `spec/metadata` defines the connection to the instance of the component.
|
||||
- `scopes` specify which application can use the component.
|
||||
|
||||
#### `order-processor` subscriber
|
||||
|
||||
In the `order-processor` subscriber, you subscribe to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. This enables your app code to talk to the Redis component instance through the Dapr sidecar.
|
||||
|
||||
```js
|
||||
server.pubsub.subscribe("orderpubsub", "orders", (data) => console.log("Subscriber received: " + JSON.stringify(data)));
|
||||
```
|
||||
|
||||
#### `checkout` publisher
|
||||
|
||||
In the `checkout` publisher service, you publish the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
|
||||
|
||||
```js
|
||||
const client = new DaprClient();
|
||||
|
||||
await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order);
|
||||
console.log("Published data: " + JSON.stringify(order));
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- .NET -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [.NET SDK or .NET 6 SDK installed](https://dotnet.microsoft.com/download).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/pub_sub).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstarts directory, navigate into the pub/sub directory:
|
||||
|
||||
```bash
|
||||
cd pub_sub/csharp/sdk
|
||||
```
|
||||
|
||||
### Step 3: Run the publisher and subscriber
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` subscriber
|
||||
- The `checkout` publisher
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 1 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 1 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 2 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 2 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 3 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 3 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 4 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 4 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 5 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 5 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 6 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 6 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 7 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 7 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 8 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 8 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 9 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 9 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 10 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 10 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
When you ran `dapr init` during Dapr install, the following YAML files were generated in the `.dapr/components` directory:
|
||||
- [`dapr.yaml` Multi-App Run template file]({{< ref "#dapryaml-multi-app-run-template-file" >}})
|
||||
- [`pubsub.yaml` component file]({{< ref "#pubsubyaml-component-file" >}})
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-subscriber" >}}) and [publisher]({{< ref "#checkout-publisher" >}}) applications.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../components/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
appPort: 7006
|
||||
command: ["dotnet", "run"]
|
||||
- appID: checkout-sdk
|
||||
appDirPath: ./checkout/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `pubsub.yaml` component file
|
||||
|
||||
With the `pubsub.yaml` component, you can easily swap out underlying components without application code changes.
|
||||
|
||||
The Redis `pubsub.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: orderpubsub
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
```
|
||||
|
||||
In the component YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component.
|
||||
- `spec/metadata` defines the connection to the instance of the component.
|
||||
- `scopes` specify which application can use the component.
|
||||
|
||||
#### `order-processor` subscriber
|
||||
|
||||
In the `order-processor` subscriber, you subscribe to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. This enables your app code to talk to the Redis component instance through the Dapr sidecar.
|
||||
|
||||
```cs
|
||||
// Dapr subscription in [Topic] routes orders topic to this route
|
||||
app.MapPost("/orders", [Topic("orderpubsub", "orders")] (Order order) => {
|
||||
Console.WriteLine("Subscriber received : " + order);
|
||||
return Results.Ok(order);
|
||||
});
|
||||
|
||||
public record Order([property: JsonPropertyName("orderId")] int OrderId);
|
||||
```
|
||||
|
||||
#### `checkout` publisher
|
||||
|
||||
In the `checkout` publisher, you publish the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
|
||||
|
||||
```cs
|
||||
using var client = new DaprClientBuilder().Build();
|
||||
await client.PublishEventAsync("orderpubsub", "orders", order);
|
||||
Console.WriteLine("Published data: " + order);
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Java -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- Java JDK 11 (or greater):
|
||||
- [Oracle JDK](https://www.oracle.com/java/technologies/downloads), or
|
||||
- OpenJDK
|
||||
- [Apache Maven](https://maven.apache.org/install.html), version 3.x.
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/pub_sub).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstarts directory, navigate into the pub/sub directory:
|
||||
|
||||
```bash
|
||||
cd pub_sub/java/sdk
|
||||
```
|
||||
|
||||
### Step 3: Run the publisher and subscriber
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` subscriber
|
||||
- The `checkout` publisher
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 1 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 1 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 2 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 2 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 3 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 3 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 4 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 4 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 5 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 5 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 6 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 6 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 7 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 7 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 8 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 8 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 9 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 9 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 10 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 10 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
When you ran `dapr init` during Dapr install, the following YAML files were generated in the `.dapr/components` directory:
|
||||
- [`dapr.yaml` Multi-App Run template file]({{< ref "#dapryaml-multi-app-run-template-file" >}})
|
||||
- [`pubsub.yaml` component file]({{< ref "#pubsubyaml-component-file" >}})
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-subscriber" >}}) and [publisher]({{< ref "#checkout-publisher" >}}) applications.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../components/
|
||||
apps:
|
||||
- appID: order-processor-sdk
|
||||
appDirPath: ./order-processor/target/
|
||||
appPort: 8080
|
||||
command: ["java", "-jar", "OrderProcessingService-0.0.1-SNAPSHOT.jar"]
|
||||
- appID: checkout-sdk
|
||||
appDirPath: ./checkout/target/
|
||||
command: ["java", "-jar", "CheckoutService-0.0.1-SNAPSHOT.jar"]
|
||||
```
|
||||
|
||||
#### `pubsub.yaml` component file
|
||||
|
||||
With the `pubsub.yaml` component, you can easily swap out underlying components without application code changes.
|
||||
|
||||
The Redis `pubsub.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: orderpubsub
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
```
|
||||
|
||||
In the component YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component.
|
||||
- `spec/metadata` defines the connection to the instance of the component.
|
||||
- `scopes` specify which application can use the component.
|
||||
|
||||
#### `order-processor` subscriber
|
||||
|
||||
In the `order-processor` subscriber, you subscribe to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. This enables your app code to talk to the Redis component instance through the Dapr sidecar.
|
||||
|
||||
```java
|
||||
@Topic(name = "orders", pubsubName = "orderpubsub")
|
||||
@PostMapping(path = "/orders", consumes = MediaType.ALL_VALUE)
|
||||
public Mono<ResponseEntity> getCheckout(@RequestBody(required = false) CloudEvent<Order> cloudEvent) {
|
||||
return Mono.fromSupplier(() -> {
|
||||
try {
|
||||
logger.info("Subscriber received: " + cloudEvent.getData().getOrderId());
|
||||
return ResponseEntity.ok("SUCCESS");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
#### `checkout` publisher
|
||||
|
||||
In the `checkout` publisher, you publish the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
|
||||
|
||||
```java
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
client.publishEvent(
|
||||
PUBSUB_NAME,
|
||||
TOPIC_NAME,
|
||||
order).block();
|
||||
logger.info("Published data: " + order.getOrderId());
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Go -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest version of Go](https://go.dev/dl/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/pub_sub).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstarts directory, navigate into the pub/sub directory:
|
||||
|
||||
```bash
|
||||
cd pub_sub/go/sdk
|
||||
```
|
||||
|
||||
### Step 3: Run the publisher and subscriber
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` subscriber
|
||||
- The `checkout` publisher
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 1 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 1 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 2 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 2 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 3 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 3 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 4 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 4 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 5 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 5 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 6 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 6 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 7 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 7 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 8 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 8 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 9 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 9 }
|
||||
== APP - checkout-sdk == Published data: Order { OrderId = 10 }
|
||||
== APP - order-processor == Subscriber received : Order { OrderId = 10 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
When you ran `dapr init` during Dapr install, the following YAML files were generated in the `.dapr/components` directory:
|
||||
- [`dapr.yaml` Multi-App Run template file]({{< ref "#dapryaml-multi-app-run-template-file" >}})
|
||||
- [`pubsub.yaml` component file]({{< ref "#pubsubyaml-component-file" >}})
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-subscriber" >}}) and [publisher]({{< ref "#checkout-publisher" >}}) applications.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../components/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
appPort: 6005
|
||||
command: ["go", "run", "."]
|
||||
- appID: checkout-sdk
|
||||
appDirPath: ./checkout/
|
||||
command: ["go", "run", "."]
|
||||
```
|
||||
|
||||
#### `pubsub.yaml` component file
|
||||
|
||||
With the `pubsub.yaml` component, you can easily swap out underlying components without application code changes.
|
||||
|
||||
The Redis `pubsub.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: orderpubsub
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
```
|
||||
|
||||
In the component YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component.
|
||||
- `spec/metadata` defines the connection to the instance of the component.
|
||||
- `scopes` specify which application can use the component.
|
||||
|
||||
#### `order-processor` subscriber
|
||||
|
||||
In the `order-processor` subscriber, you subscribe to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. This enables your app code to talk to the Redis component instance through the Dapr sidecar.
|
||||
|
||||
```go
|
||||
func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
|
||||
fmt.Println("Subscriber received: ", e.Data)
|
||||
return false, nil
|
||||
}
|
||||
```
|
||||
|
||||
#### `checkout` publisher
|
||||
|
||||
In the `checkout` publisher, you publish the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
|
||||
|
||||
```go
|
||||
client, err := dapr.NewClient()
|
||||
|
||||
if err := client.PublishEvent(ctx, PUBSUB_NAME, PUBSUB_TOPIC, []byte(order)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Published data: ", order)
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
{{< /tabs >}}
|
||||
|
||||
## Run one application at a time
|
||||
|
||||
Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
|
|
|
@ -10,10 +10,682 @@ With [Dapr's Service Invocation building block](https://docs.dapr.io/developing-
|
|||
|
||||
<img src="/images/serviceinvocation-quickstart/service-invocation-overview.png" width=800 alt="Diagram showing the steps of service invocation" style="padding-bottom:25px;">
|
||||
|
||||
Dapr offers several methods for service invocation, which you can choose depending on your scenario. For this Quickstart, you'll enable the checkout service to invoke a method using HTTP proxy in the order-processor service.
|
||||
Dapr offers several methods for service invocation, which you can choose depending on your scenario. For this Quickstart, you'll enable the checkout service to invoke a method using HTTP proxy in the order-processor service and by either:
|
||||
- [Running all applications in this sample simultaneously with the Multi-App Run template file]({{< ref "#run-using-multi-app-run" >}}), or
|
||||
- [Running one application at a time]({{< ref "#run-one-application-at-a-time" >}})
|
||||
|
||||
Learn more about Dapr's methods for service invocation in the [overview article]({{< ref service-invocation-overview.md >}}).
|
||||
|
||||
## Run using Multi-App Run
|
||||
|
||||
{{% alert title="Note" color="primary" %}}
|
||||
[Multi-App Run]({{< ref multi-app-dapr-run >}}) is currently a preview feature only supported in Linux/MacOS.
|
||||
{{% /alert %}}
|
||||
|
||||
Select your preferred language before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
<!-- Python -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Python 3.7+ installed](https://www.python.org/downloads/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/service_invocation).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstart clone directory, navigate to the quickstart directory.
|
||||
|
||||
```bash
|
||||
cd service_invocation/python/http
|
||||
```
|
||||
|
||||
### Step 3: Run the `order-processor` and `checkout` services
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` service
|
||||
- The `checkout` service
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - order-processor == Order received : Order { orderId = 1 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 1 }
|
||||
== APP - order-processor == Order received : Order { orderId = 2 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 2 }
|
||||
== APP - order-processor == Order received : Order { orderId = 3 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 3 }
|
||||
== APP - order-processor == Order received : Order { orderId = 4 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 4 }
|
||||
== APP - order-processor == Order received : Order { orderId = 5 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 5 }
|
||||
== APP - order-processor == Order received : Order { orderId = 6 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 6 }
|
||||
== APP - order-processor == Order received : Order { orderId = 7 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 7 }
|
||||
== APP - order-processor == Order received : Order { orderId = 8 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 8 }
|
||||
== APP - order-processor == Order received : Order { orderId = 9 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 9 }
|
||||
== APP - order-processor == Order received : Order { orderId = 10 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 10 }
|
||||
== APP - order-processor == Order received : Order { orderId = 11 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 11 }
|
||||
== APP - order-processor == Order received : Order { orderId = 12 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 12 }
|
||||
== APP - order-processor == Order received : Order { orderId = 13 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 13 }
|
||||
== APP - order-processor == Order received : Order { orderId = 14 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 14 }
|
||||
== APP - order-processor == Order received : Order { orderId = 15 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 15 }
|
||||
== APP - order-processor == Order received : Order { orderId = 16 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 16 }
|
||||
== APP - order-processor == Order received : Order { orderId = 17 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 17 }
|
||||
== APP - order-processor == Order received : Order { orderId = 18 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 18 }
|
||||
== APP - order-processor == Order received : Order { orderId = 19 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 19 }
|
||||
== APP - order-processor == Order received : Order { orderId = 20 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 20 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-service" >}}) and [publisher]({{< ref "#checkout-service" >}}) applications using the `dapr.yaml` Multi-App Run template file.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
apps:
|
||||
- appDirPath: ./order-processor/
|
||||
appID: order-processor
|
||||
appPort: 8001
|
||||
command: ["python3", "app.py"]
|
||||
- appID: checkout
|
||||
appDirPath: ./checkout/
|
||||
command: ["python3", "app.py"]
|
||||
```
|
||||
|
||||
#### `order-processor` service
|
||||
|
||||
The `order-processor` service receives the call from the `checkout` service:
|
||||
|
||||
```py
|
||||
@app.route('/orders', methods=['POST'])
|
||||
def getOrder():
|
||||
data = request.json
|
||||
print('Order received : ' + json.dumps(data), flush=True)
|
||||
return json.dumps({'success': True}), 200, {
|
||||
'ContentType': 'application/json'}
|
||||
|
||||
|
||||
app.run(port=8001)
|
||||
```
|
||||
|
||||
#### `checkout` service
|
||||
|
||||
In the `checkout` service, you'll notice there's no need to rewrite your app code to use Dapr's service invocation. You can enable service invocation by simply adding the `dapr-app-id` header, which specifies the ID of the target service.
|
||||
|
||||
```python
|
||||
headers = {'dapr-app-id': 'order-processor'}
|
||||
|
||||
result = requests.post(
|
||||
url='%s/orders' % (base_url),
|
||||
data=json.dumps(order),
|
||||
headers=headers
|
||||
)
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- JavaScript -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest Node.js installed](https://nodejs.org/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/service_invocation).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstart clone directory, navigate to the quickstart directory.
|
||||
|
||||
```bash
|
||||
cd service_invocation/javascript/http
|
||||
```
|
||||
|
||||
### Step 3: Run the `order-processor` and `checkout` services
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` service
|
||||
- The `checkout` service
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - order-processor == Order received : Order { orderId = 1 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 1 }
|
||||
== APP - order-processor == Order received : Order { orderId = 2 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 2 }
|
||||
== APP - order-processor == Order received : Order { orderId = 3 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 3 }
|
||||
== APP - order-processor == Order received : Order { orderId = 4 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 4 }
|
||||
== APP - order-processor == Order received : Order { orderId = 5 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 5 }
|
||||
== APP - order-processor == Order received : Order { orderId = 6 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 6 }
|
||||
== APP - order-processor == Order received : Order { orderId = 7 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 7 }
|
||||
== APP - order-processor == Order received : Order { orderId = 8 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 8 }
|
||||
== APP - order-processor == Order received : Order { orderId = 9 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 9 }
|
||||
== APP - order-processor == Order received : Order { orderId = 10 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 10 }
|
||||
== APP - order-processor == Order received : Order { orderId = 11 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 11 }
|
||||
== APP - order-processor == Order received : Order { orderId = 12 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 12 }
|
||||
== APP - order-processor == Order received : Order { orderId = 13 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 13 }
|
||||
== APP - order-processor == Order received : Order { orderId = 14 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 14 }
|
||||
== APP - order-processor == Order received : Order { orderId = 15 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 15 }
|
||||
== APP - order-processor == Order received : Order { orderId = 16 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 16 }
|
||||
== APP - order-processor == Order received : Order { orderId = 17 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 17 }
|
||||
== APP - order-processor == Order received : Order { orderId = 18 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 18 }
|
||||
== APP - order-processor == Order received : Order { orderId = 19 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 19 }
|
||||
== APP - order-processor == Order received : Order { orderId = 20 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 20 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-service" >}}) and [publisher]({{< ref "#checkout-service" >}}) applications using the `dapr.yaml` Multi-App Run template file.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
apps:
|
||||
- appDirPath: ./order-processor/
|
||||
appID: order-processor
|
||||
appPort: 5001
|
||||
command: ["npm", "start"]
|
||||
- appID: checkout
|
||||
appDirPath: ./checkout/
|
||||
command: ["npm", "start"]
|
||||
```
|
||||
|
||||
#### `order-processor` service
|
||||
|
||||
The `order-processor` service receives the call from the `checkout` service:
|
||||
|
||||
```javascript
|
||||
app.post('/orders', (req, res) => {
|
||||
console.log("Order received:", req.body);
|
||||
res.sendStatus(200);
|
||||
});
|
||||
```
|
||||
|
||||
#### `checkout` service
|
||||
|
||||
In the `checkout` service, you'll notice there's no need to rewrite your app code to use Dapr's service invocation. You can enable service invocation by simply adding the `dapr-app-id` header, which specifies the ID of the target service.
|
||||
|
||||
```javascript
|
||||
let axiosConfig = {
|
||||
headers: {
|
||||
"dapr-app-id": "order-processor"
|
||||
}
|
||||
};
|
||||
const res = await axios.post(`${DAPR_HOST}:${DAPR_HTTP_PORT}/orders`, order , axiosConfig);
|
||||
console.log("Order passed: " + res.config.data);
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- .NET -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [.NET SDK or .NET 7 SDK installed](https://dotnet.microsoft.com/download).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/service_invocation).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstart clone directory, navigate to the quickstart directory.
|
||||
|
||||
```bash
|
||||
cd service_invocation/csharp/http
|
||||
```
|
||||
|
||||
### Step 3: Run the `order-processor` and `checkout` services
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` service
|
||||
- The `checkout` service
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - order-processor == Order received : Order { orderId = 1 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 1 }
|
||||
== APP - order-processor == Order received : Order { orderId = 2 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 2 }
|
||||
== APP - order-processor == Order received : Order { orderId = 3 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 3 }
|
||||
== APP - order-processor == Order received : Order { orderId = 4 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 4 }
|
||||
== APP - order-processor == Order received : Order { orderId = 5 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 5 }
|
||||
== APP - order-processor == Order received : Order { orderId = 6 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 6 }
|
||||
== APP - order-processor == Order received : Order { orderId = 7 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 7 }
|
||||
== APP - order-processor == Order received : Order { orderId = 8 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 8 }
|
||||
== APP - order-processor == Order received : Order { orderId = 9 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 9 }
|
||||
== APP - order-processor == Order received : Order { orderId = 10 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 10 }
|
||||
== APP - order-processor == Order received : Order { orderId = 11 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 11 }
|
||||
== APP - order-processor == Order received : Order { orderId = 12 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 12 }
|
||||
== APP - order-processor == Order received : Order { orderId = 13 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 13 }
|
||||
== APP - order-processor == Order received : Order { orderId = 14 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 14 }
|
||||
== APP - order-processor == Order received : Order { orderId = 15 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 15 }
|
||||
== APP - order-processor == Order received : Order { orderId = 16 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 16 }
|
||||
== APP - order-processor == Order received : Order { orderId = 17 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 17 }
|
||||
== APP - order-processor == Order received : Order { orderId = 18 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 18 }
|
||||
== APP - order-processor == Order received : Order { orderId = 19 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 19 }
|
||||
== APP - order-processor == Order received : Order { orderId = 20 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 20 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-service" >}}) and [publisher]({{< ref "#checkout-service" >}}) applications using the `dapr.yaml` Multi-App Run template file.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
apps:
|
||||
- appDirPath: ./order-processor/
|
||||
appID: order-processor
|
||||
appPort: 7001
|
||||
command: ["dotnet", "run"]
|
||||
- appID: checkout
|
||||
appDirPath: ./checkout/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `order-processor` service
|
||||
|
||||
The `order-processor` service receives the call from the `checkout` service:
|
||||
|
||||
```csharp
|
||||
app.MapPost("/orders", (Order order) =>
|
||||
{
|
||||
Console.WriteLine("Order received : " + order);
|
||||
return order.ToString();
|
||||
});
|
||||
```
|
||||
|
||||
#### `checkout` service
|
||||
|
||||
In the Program.cs file for the `checkout` service, you'll notice there's no need to rewrite your app code to use Dapr's service invocation. You can enable service invocation by simply adding the `dapr-app-id` header, which specifies the ID of the target service.
|
||||
|
||||
```csharp
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
client.DefaultRequestHeaders.Add("dapr-app-id", "order-processor");
|
||||
|
||||
var response = await client.PostAsync($"{baseURL}/orders", content);
|
||||
Console.WriteLine("Order passed: " + order);
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Java -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- Java JDK 11 (or greater):
|
||||
- [Oracle JDK](https://www.oracle.com/java/technologies/downloads), or
|
||||
- OpenJDK
|
||||
- [Apache Maven](https://maven.apache.org/install.html), version 3.x.
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/service_invocation).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstart clone directory, navigate to the quickstart directory.
|
||||
|
||||
```bash
|
||||
cd service_invocation/java/http
|
||||
```
|
||||
|
||||
### Step 3: Run the `order-processor` and `checkout` services
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` service
|
||||
- The `checkout` service
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - order-processor == Order received : Order { orderId = 1 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 1 }
|
||||
== APP - order-processor == Order received : Order { orderId = 2 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 2 }
|
||||
== APP - order-processor == Order received : Order { orderId = 3 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 3 }
|
||||
== APP - order-processor == Order received : Order { orderId = 4 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 4 }
|
||||
== APP - order-processor == Order received : Order { orderId = 5 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 5 }
|
||||
== APP - order-processor == Order received : Order { orderId = 6 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 6 }
|
||||
== APP - order-processor == Order received : Order { orderId = 7 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 7 }
|
||||
== APP - order-processor == Order received : Order { orderId = 8 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 8 }
|
||||
== APP - order-processor == Order received : Order { orderId = 9 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 9 }
|
||||
== APP - order-processor == Order received : Order { orderId = 10 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 10 }
|
||||
== APP - order-processor == Order received : Order { orderId = 11 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 11 }
|
||||
== APP - order-processor == Order received : Order { orderId = 12 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 12 }
|
||||
== APP - order-processor == Order received : Order { orderId = 13 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 13 }
|
||||
== APP - order-processor == Order received : Order { orderId = 14 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 14 }
|
||||
== APP - order-processor == Order received : Order { orderId = 15 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 15 }
|
||||
== APP - order-processor == Order received : Order { orderId = 16 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 16 }
|
||||
== APP - order-processor == Order received : Order { orderId = 17 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 17 }
|
||||
== APP - order-processor == Order received : Order { orderId = 18 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 18 }
|
||||
== APP - order-processor == Order received : Order { orderId = 19 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 19 }
|
||||
== APP - order-processor == Order received : Order { orderId = 20 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 20 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-service" >}}) and [publisher]({{< ref "#checkout-service" >}}) applications using the `dapr.yaml` Multi-App Run template file.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
apps:
|
||||
- appDirPath: ./order-processor/
|
||||
appID: order-processor
|
||||
appPort: 9001
|
||||
command: ["java", "-jar", "target/OrderProcessingService-0.0.1-SNAPSHOT.jar"]
|
||||
- appID: checkout
|
||||
appDirPath: ./checkout/
|
||||
command: ["java", "-jar", "target/CheckoutService-0.0.1-SNAPSHOT.jar"]
|
||||
```
|
||||
|
||||
#### `order-processor` service
|
||||
|
||||
The `order-processor` service receives the call from the `checkout` service:
|
||||
|
||||
```java
|
||||
public String processOrders(@RequestBody Order body) {
|
||||
System.out.println("Order received: "+ body.getOrderId());
|
||||
return "CID" + body.getOrderId();
|
||||
}
|
||||
```
|
||||
|
||||
#### `checkout` service
|
||||
|
||||
In the `checkout` service, you'll notice there's no need to rewrite your app code to use Dapr's service invocation. You can enable service invocation by simply adding the `dapr-app-id` header, which specifies the ID of the target service.
|
||||
|
||||
```java
|
||||
.header("Content-Type", "application/json")
|
||||
.header("dapr-app-id", "order-processor")
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println("Order passed: "+ orderId)
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Go -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Step 1: Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest version of Go](https://go.dev/dl/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 2: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/service_invocation).
|
||||
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
From the root of the Quickstart clone directory, navigate to the quickstart directory.
|
||||
|
||||
```bash
|
||||
cd service_invocation/go/http
|
||||
```
|
||||
|
||||
### Step 3: Run the `order-processor` and `checkout` services
|
||||
|
||||
With the following command, simultaneously run the following services alongside their own Dapr sidecars:
|
||||
- The `order-processor` service
|
||||
- The `checkout` service
|
||||
|
||||
```bash
|
||||
dapr run -f .
|
||||
```
|
||||
|
||||
**Expected output**
|
||||
|
||||
```
|
||||
== APP - order-processor == Order received : Order { orderId = 1 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 1 }
|
||||
== APP - order-processor == Order received : Order { orderId = 2 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 2 }
|
||||
== APP - order-processor == Order received : Order { orderId = 3 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 3 }
|
||||
== APP - order-processor == Order received : Order { orderId = 4 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 4 }
|
||||
== APP - order-processor == Order received : Order { orderId = 5 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 5 }
|
||||
== APP - order-processor == Order received : Order { orderId = 6 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 6 }
|
||||
== APP - order-processor == Order received : Order { orderId = 7 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 7 }
|
||||
== APP - order-processor == Order received : Order { orderId = 8 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 8 }
|
||||
== APP - order-processor == Order received : Order { orderId = 9 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 9 }
|
||||
== APP - order-processor == Order received : Order { orderId = 10 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 10 }
|
||||
== APP - order-processor == Order received : Order { orderId = 11 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 11 }
|
||||
== APP - order-processor == Order received : Order { orderId = 12 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 12 }
|
||||
== APP - order-processor == Order received : Order { orderId = 13 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 13 }
|
||||
== APP - order-processor == Order received : Order { orderId = 14 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 14 }
|
||||
== APP - order-processor == Order received : Order { orderId = 15 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 15 }
|
||||
== APP - order-processor == Order received : Order { orderId = 16 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 16 }
|
||||
== APP - order-processor == Order received : Order { orderId = 17 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 17 }
|
||||
== APP - order-processor == Order received : Order { orderId = 18 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 18 }
|
||||
== APP - order-processor == Order received : Order { orderId = 19 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 19 }
|
||||
== APP - order-processor == Order received : Order { orderId = 20 }
|
||||
== APP - checkout == Order passed: Order { OrderId = 20 }
|
||||
Exited App successfully
|
||||
```
|
||||
|
||||
### What happened?
|
||||
|
||||
Running `dapr run -f .` in this Quickstart started both the [subscriber]({{< ref "#order-processor-service" >}}) and [publisher]({{< ref "#checkout-service" >}}) applications using the `dapr.yaml` Multi-App Run template file.
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
Running the [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) with `dapr run -f .` starts all applications in your project. In this Quickstart, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
apps:
|
||||
- appDirPath: ./order-processor/
|
||||
appID: order-processor
|
||||
appPort: 6006
|
||||
command: ["go", "run", "."]
|
||||
- appID: checkout
|
||||
appDirPath: ./checkout/
|
||||
command: ["go", "run", "."]
|
||||
```
|
||||
|
||||
#### `order-processor` service
|
||||
|
||||
In the `order-processor` service, each order is received via an HTTP POST request and processed by the `getOrder` function.
|
||||
|
||||
```go
|
||||
func getOrder(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("Order received : %s", string(data))
|
||||
}
|
||||
```
|
||||
|
||||
#### `checkout` service
|
||||
|
||||
In the `checkout` service, you'll notice there's no need to rewrite your app code to use Dapr's service invocation. You can enable service invocation by simply adding the `dapr-app-id` header, which specifies the ID of the target service.
|
||||
|
||||
```go
|
||||
req.Header.Add("dapr-app-id", "order-processor")
|
||||
|
||||
response, err := client.Do(req)
|
||||
```
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
{{% /tabs %}}
|
||||
|
||||
## Run one application at a time
|
||||
|
||||
Select your preferred language before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
|
|
|
@ -6,10 +6,692 @@ weight: 72
|
|||
description: "Get started with Dapr's State Management building block"
|
||||
---
|
||||
|
||||
Let's take a look at Dapr's [State Management building block]({{< ref state-management >}}). In this Quickstart, you will save, get, and delete state using a Redis state store, but you can swap this out for any one of the [supported state stores]({{< ref supported-state-stores.md >}}).
|
||||
Let's take a look at Dapr's [State Management building block]({{< ref state-management >}}). In this Quickstart, you will save, get, and delete state using a Redis state store by either:
|
||||
- [Running all applications simultaneously with the Multi-App Run template file]({{< ref "#run-using-multi-app-run" >}}), or
|
||||
- [Running a single application at a time]({{< ref "#run-one-application-at-a-time" >}})
|
||||
|
||||
<img src="/images/state-management-quickstart.png" width=1000 style="padding-bottom:15px;">
|
||||
|
||||
While this sample uses Redis, you can swap it out for any one of the [supported state stores]({{< ref supported-state-stores.md >}}).
|
||||
|
||||
## Run using Multi-App Run
|
||||
|
||||
{{% alert title="Note" color="primary" %}}
|
||||
[Multi-App Run]({{< ref multi-app-dapr-run >}}) is currently a preview feature only supported in Linux/MacOS.
|
||||
{{% /alert %}}
|
||||
|
||||
Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
<!-- Python -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Python 3.7+ installed](https://www.python.org/downloads/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 1: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/state_management).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
### Step 2: Manipulate service state
|
||||
|
||||
In a terminal window, navigate to the `order-processor` directory.
|
||||
|
||||
```bash
|
||||
cd state_management/python/sdk/order-processor
|
||||
```
|
||||
|
||||
Install the dependencies:
|
||||
|
||||
```bash
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
|
||||
Run the `order-processor` service alongside a Dapr sidecar using [Multi-App Run]({{< ref multi-app-dapr-run >}}).
|
||||
|
||||
```bash
|
||||
dapr run -f
|
||||
```
|
||||
|
||||
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
|
||||
|
||||
```python
|
||||
with DaprClient() as client:
|
||||
|
||||
# Save state into the state store
|
||||
client.save_state(DAPR_STORE_NAME, orderId, str(order))
|
||||
logging.info('Saving Order: %s', order)
|
||||
|
||||
# Get state from the state store
|
||||
result = client.get_state(DAPR_STORE_NAME, orderId)
|
||||
logging.info('Result after get: ' + str(result.data))
|
||||
|
||||
# Delete state from the state store
|
||||
client.delete_state(store_name=DAPR_STORE_NAME, key=orderId)
|
||||
logging.info('Deleting Order: %s', order)
|
||||
```
|
||||
|
||||
### Step 3: View the order-processor outputs
|
||||
|
||||
Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.
|
||||
|
||||
Order-processor output:
|
||||
```
|
||||
== APP == INFO:root:Saving Order: {'orderId': '1'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '1'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '1'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '2'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '2'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '2'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '3'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '3'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '3'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '4'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '4'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '4'}
|
||||
```
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default [Multi-App Run template file]({{< ref multi-app-dapr-run >}}) named `dapr.yaml`. Running `dapr run -f` starts all applications in your project. In this sample, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../../resources/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `statestore.yaml` component file
|
||||
|
||||
When you run `dapr init`, Dapr also creates a default Redis `statestore.yaml` and runs a Redis container on your local machine, located:
|
||||
|
||||
- On Windows, under `%UserProfile%\.dapr\components\statestore.yaml`
|
||||
- On Linux/MacOS, under `~/.dapr/components/statestore.yaml`
|
||||
|
||||
With the `statestore.yaml` component, you can easily swap out the [state store](/reference/components-reference/supported-state-stores/) without making code changes.
|
||||
|
||||
The Redis `statestore.yaml` file included for this quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
- name: actorStateStore
|
||||
value: "true"
|
||||
```
|
||||
|
||||
In the YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component (called `DAPR_STORE_NAME` in the code sample).
|
||||
- `spec/metadata` defines the connection to the Redis instance used by the component.
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- JavaScript -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest Node.js installed](https://nodejs.org/download/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 1: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/state_management).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
### Step 2: Manipulate service state
|
||||
|
||||
In a terminal window, navigate to the `order-processor` directory.
|
||||
|
||||
```bash
|
||||
cd state_management/javascript/sdk/order-processor
|
||||
```
|
||||
|
||||
Install dependencies, which will include the `@dapr/dapr` package from the JavaScript SDK:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
Verify you have the following files included in the service directory:
|
||||
|
||||
- `package.json`
|
||||
- `package-lock.json`
|
||||
|
||||
Run the `order-processor` service alongside a Dapr sidecar.
|
||||
|
||||
```bash
|
||||
dapr run -f
|
||||
```
|
||||
|
||||
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
|
||||
|
||||
```js
|
||||
const client = new DaprClient()
|
||||
|
||||
// Save state into a state store
|
||||
await client.state.save(DAPR_STATE_STORE_NAME, order)
|
||||
console.log("Saving Order: ", order)
|
||||
|
||||
// Get state from a state store
|
||||
const savedOrder = await client.state.get(DAPR_STATE_STORE_NAME, order.orderId)
|
||||
console.log("Getting Order: ", savedOrder)
|
||||
|
||||
// Delete state from the state store
|
||||
await client.state.delete(DAPR_STATE_STORE_NAME, order.orderId)
|
||||
console.log("Deleting Order: ", order)
|
||||
```
|
||||
### Step 3: View the order-processor outputs
|
||||
|
||||
Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.
|
||||
|
||||
Order-processor output:
|
||||
```
|
||||
== APP == > order-processor@1.0.0 start
|
||||
== APP == > node index.js
|
||||
== APP == Saving Order: { orderId: 1 }
|
||||
== APP == Saving Order: { orderId: 2 }
|
||||
== APP == Saving Order: { orderId: 3 }
|
||||
== APP == Saving Order: { orderId: 4 }
|
||||
== APP == Saving Order: { orderId: 5 }
|
||||
== APP == Getting Order: { orderId: 1 }
|
||||
== APP == Deleting Order: { orderId: 1 }
|
||||
== APP == Getting Order: { orderId: 2 }
|
||||
== APP == Deleting Order: { orderId: 2 }
|
||||
== APP == Getting Order: { orderId: 3 }
|
||||
== APP == Deleting Order: { orderId: 3 }
|
||||
== APP == Getting Order: { orderId: 4 }
|
||||
== APP == Deleting Order: { orderId: 4 }
|
||||
== APP == Getting Order: { orderId: 5 }
|
||||
== APP == Deleting Order: { orderId: 5 }
|
||||
```
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Multi-App Run template file named `dapr.yaml`. Running `dapr run -f` starts all applications in your project. In this sample, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../../resources/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `statestore.yaml` component file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Redis `statestore.yaml` and runs a Redis container on your local machine, located:
|
||||
|
||||
- On Windows, under `%UserProfile%\.dapr\components\statestore.yaml`
|
||||
- On Linux/MacOS, under `~/.dapr/components/statestore.yaml`
|
||||
|
||||
With the `statestore.yaml` component, you can easily swap out the [state store](/reference/components-reference/supported-state-stores/) without making code changes.
|
||||
|
||||
The Redis `statestore.yaml` file included for this quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
- name: actorStateStore
|
||||
value: "true"
|
||||
```
|
||||
|
||||
In the YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component (called `DAPR_STORE_NAME` in the code sample).
|
||||
- `spec/metadata` defines the connection to the Redis instance used by the component.
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- .NET -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [.NET SDK or .NET 6 SDK installed](https://dotnet.microsoft.com/download).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 1: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/state_management).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
### Step 2: Manipulate service state
|
||||
|
||||
In a terminal window, navigate to the `order-processor` directory.
|
||||
|
||||
```bash
|
||||
cd state_management/csharp/sdk/order-processor
|
||||
```
|
||||
|
||||
Recall NuGet packages:
|
||||
|
||||
```bash
|
||||
dotnet restore
|
||||
dotnet build
|
||||
```
|
||||
|
||||
Run the `order-processor` service alongside a Dapr sidecar.
|
||||
|
||||
```bash
|
||||
dapr run -f
|
||||
```
|
||||
|
||||
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
|
||||
|
||||
```cs
|
||||
var client = new DaprClientBuilder().Build();
|
||||
|
||||
// Save state into the state store
|
||||
await client.SaveStateAsync(DAPR_STORE_NAME, orderId.ToString(), order.ToString());
|
||||
Console.WriteLine("Saving Order: " + order);
|
||||
|
||||
// Get state from the state store
|
||||
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
|
||||
Console.WriteLine("Getting Order: " + result);
|
||||
|
||||
// Delete state from the state store
|
||||
await client.DeleteStateAsync(DAPR_STORE_NAME, orderId.ToString());
|
||||
Console.WriteLine("Deleting Order: " + order);
|
||||
```
|
||||
### Step 3: View the order-processor outputs
|
||||
|
||||
Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.
|
||||
|
||||
Order-processor output:
|
||||
```
|
||||
== APP == Saving Order: Order { orderId = 1 }
|
||||
== APP == Getting Order: Order { orderId = 1 }
|
||||
== APP == Deleting Order: Order { orderId = 1 }
|
||||
== APP == Saving Order: Order { orderId = 2 }
|
||||
== APP == Getting Order: Order { orderId = 2 }
|
||||
== APP == Deleting Order: Order { orderId = 2 }
|
||||
== APP == Saving Order: Order { orderId = 3 }
|
||||
== APP == Getting Order: Order { orderId = 3 }
|
||||
== APP == Deleting Order: Order { orderId = 3 }
|
||||
== APP == Saving Order: Order { orderId = 4 }
|
||||
== APP == Getting Order: Order { orderId = 4 }
|
||||
== APP == Deleting Order: Order { orderId = 4 }
|
||||
== APP == Saving Order: Order { orderId = 5 }
|
||||
== APP == Getting Order: Order { orderId = 5 }
|
||||
== APP == Deleting Order: Order { orderId = 5 }
|
||||
```
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Multi-App Run template file named `dapr.yaml`. Running `dapr run -f` starts all applications in your project. In this sample, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../../resources/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `statestore.yaml` component file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Redis `statestore.yaml` and runs a Redis container on your local machine, located:
|
||||
|
||||
- On Windows, under `%UserProfile%\.dapr\components\statestore.yaml`
|
||||
- On Linux/MacOS, under `~/.dapr/components/statestore.yaml`
|
||||
|
||||
With the `statestore.yaml` component, you can easily swap out the [state store](/reference/components-reference/supported-state-stores/) without making code changes.
|
||||
|
||||
The Redis `statestore.yaml` file included for this quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
- name: actorStateStore
|
||||
value: "true"
|
||||
```
|
||||
|
||||
In the YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component (called `DAPR_STORE_NAME` in the code sample).
|
||||
- `spec/metadata` defines the connection to the Redis instance used by the component.
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Java -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- Java JDK 11 (or greater):
|
||||
- [Oracle JDK](https://www.oracle.com/java/technologies/downloads), or
|
||||
- OpenJDK
|
||||
- [Apache Maven](https://maven.apache.org/install.html), version 3.x.
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 1: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/state_management).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
### Step 2: Manipulate service state
|
||||
|
||||
In a terminal window, navigate to the `order-processor` directory.
|
||||
|
||||
```bash
|
||||
cd state_management/java/sdk/order-processor
|
||||
```
|
||||
|
||||
Install the dependencies:
|
||||
|
||||
```bash
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
Run the `order-processor` service alongside a Dapr sidecar.
|
||||
|
||||
```bash
|
||||
dapr run -f
|
||||
```
|
||||
|
||||
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
|
||||
|
||||
```java
|
||||
try (DaprClient client = new DaprClientBuilder().build()) {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
int orderId = i;
|
||||
Order order = new Order();
|
||||
order.setOrderId(orderId);
|
||||
|
||||
// Save state into the state store
|
||||
client.saveState(DAPR_STATE_STORE, String.valueOf(orderId), order).block();
|
||||
LOGGER.info("Saving Order: " + order.getOrderId());
|
||||
|
||||
// Get state from the state store
|
||||
State<Order> response = client.getState(DAPR_STATE_STORE, String.valueOf(orderId), Order.class).block();
|
||||
LOGGER.info("Getting Order: " + response.getValue().getOrderId());
|
||||
|
||||
// Delete state from the state store
|
||||
client.deleteState(DAPR_STATE_STORE, String.valueOf(orderId)).block();
|
||||
LOGGER.info("Deleting Order: " + orderId);
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
}
|
||||
```
|
||||
### Step 3: View the order-processor outputs
|
||||
|
||||
Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.
|
||||
|
||||
Order-processor output:
|
||||
```
|
||||
== APP == INFO:root:Saving Order: {'orderId': '1'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '1'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '1'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '2'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '2'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '2'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '3'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '3'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '3'}
|
||||
== APP == INFO:root:Saving Order: {'orderId': '4'}
|
||||
== APP == INFO:root:Result after get: b"{'orderId': '4'}"
|
||||
== APP == INFO:root:Deleting Order: {'orderId': '4'}
|
||||
```
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Multi-App Run template file named `dapr.yaml`. Running `dapr run -f` starts all applications in your project. In this sample, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../../resources/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `statestore.yaml` component file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Redis `statestore.yaml` and runs a Redis container on your local machine, located:
|
||||
|
||||
- On Windows, under `%UserProfile%\.dapr\components\statestore.yaml`
|
||||
- On Linux/MacOS, under `~/.dapr/components/statestore.yaml`
|
||||
|
||||
With the `statestore.yaml` component, you can easily swap out the [state store](/reference/components-reference/supported-state-stores/) without making code changes.
|
||||
|
||||
The Redis `statestore.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
- name: actorStateStore
|
||||
value: "true"
|
||||
```
|
||||
|
||||
In the YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component (called `DAPR_STORE_NAME` in the code sample).
|
||||
- `spec/metadata` defines the connection to the Redis instance used by the component.
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
<!-- Go -->
|
||||
{{% codetab %}}
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
For this example, you will need:
|
||||
|
||||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started).
|
||||
- [Latest version of Go](https://go.dev/dl/).
|
||||
<!-- IGNORE_LINKS -->
|
||||
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
|
||||
<!-- END_IGNORE -->
|
||||
|
||||
### Step 1: Set up the environment
|
||||
|
||||
Clone the [sample provided in the Quickstarts repo](https://github.com/dapr/quickstarts/tree/master/state_management).
|
||||
|
||||
```bash
|
||||
git clone https://github.com/dapr/quickstarts.git
|
||||
```
|
||||
|
||||
### Step 2: Manipulate service state
|
||||
|
||||
In a terminal window, navigate to the `order-processor` directory.
|
||||
|
||||
```bash
|
||||
cd state_management/go/sdk/order-processor
|
||||
```
|
||||
|
||||
Install the dependencies and build the application:
|
||||
|
||||
```bash
|
||||
go build .
|
||||
```
|
||||
|
||||
Run the `order-processor` service alongside a Dapr sidecar.
|
||||
|
||||
```bash
|
||||
dapr run -f
|
||||
```
|
||||
|
||||
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
|
||||
|
||||
```go
|
||||
client, err := dapr.NewClient()
|
||||
|
||||
// Save state into the state store
|
||||
_ = client.SaveState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), []byte(order))
|
||||
log.Print("Saving Order: " + string(order))
|
||||
|
||||
// Get state from the state store
|
||||
result, _ := client.GetState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
|
||||
fmt.Println("Getting Order: " + string(result.Value))
|
||||
|
||||
// Delete state from the state store
|
||||
_ = client.DeleteState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
|
||||
log.Print("Deleting Order: " + string(order))
|
||||
```
|
||||
|
||||
### Step 3: View the order-processor outputs
|
||||
|
||||
Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.
|
||||
|
||||
Order-processor output:
|
||||
```
|
||||
== APP == dapr client initializing for: 127.0.0.1:53689
|
||||
== APP == 2022/04/01 09:16:03 Saving Order: {"orderId":1}
|
||||
== APP == Getting Order: {"orderId":1}
|
||||
== APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":1}
|
||||
== APP == 2022/04/01 09:16:03 Saving Order: {"orderId":2}
|
||||
== APP == Getting Order: {"orderId":2}
|
||||
== APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":2}
|
||||
== APP == 2022/04/01 09:16:03 Saving Order: {"orderId":3}
|
||||
== APP == Getting Order: {"orderId":3}
|
||||
== APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":3}
|
||||
== APP == 2022/04/01 09:16:03 Saving Order: {"orderId":4}
|
||||
== APP == Getting Order: {"orderId":4}
|
||||
== APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":4}
|
||||
== APP == 2022/04/01 09:16:03 Saving Order: {"orderId":5}
|
||||
== APP == Getting Order: {"orderId":5}
|
||||
== APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":5}
|
||||
```
|
||||
|
||||
#### `dapr.yaml` Multi-App Run template file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Multi-App Run template file named `dapr.yaml`. Running `dapr run -f` starts all applications in your project. In this sample, the `dapr.yaml` file contains the following:
|
||||
|
||||
```yml
|
||||
version: 1
|
||||
common:
|
||||
resourcesPath: ../../../resources/
|
||||
apps:
|
||||
- appID: order-processor
|
||||
appDirPath: ./order-processor/
|
||||
command: ["dotnet", "run"]
|
||||
```
|
||||
|
||||
#### `statestore.yaml` component file
|
||||
|
||||
When you run `dapr init`, Dapr creates a default Redis `statestore.yaml` and runs a Redis container on your local machine, located:
|
||||
|
||||
- On Windows, under `%UserProfile%\.dapr\components\statestore.yaml`
|
||||
- On Linux/MacOS, under `~/.dapr/components/statestore.yaml`
|
||||
|
||||
With the `statestore.yaml` component, you can easily swap out the [state store](/reference/components-reference/supported-state-stores/) without making code changes.
|
||||
|
||||
The Redis `statestore.yaml` file included for this Quickstart contains the following:
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
||||
- name: actorStateStore
|
||||
value: "true"
|
||||
```
|
||||
|
||||
In the YAML file:
|
||||
|
||||
- `metadata/name` is how your application talks to the component (called `DAPR_STORE_NAME` in the code sample).
|
||||
- `spec/metadata` defines the connection to the Redis instance used by the component.
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
{{< /tabs >}}
|
||||
|
||||
|
||||
## Run one application at a time
|
||||
|
||||
Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.
|
||||
|
||||
{{< tabs "Python" "JavaScript" ".NET" "Java" "Go" >}}
|
||||
|
|
Loading…
Reference in New Issue