Quickstart: Service Invocation

With Dapr’s Service Invocation building block, your application can communicate reliably and securely with other applications.

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.

Learn more about Dapr’s methods for service invocation in the .

Select your preferred language before proceeding with the Quickstart.

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

Step 3: Run order-processor service

In a terminal window, from the root of the Quickstart clone directory navigate to order-processor directory.

  1. cd service_invocation/python/http/order-processor

Install the dependencies and build the application:

  1. pip3 install -r requirements.txt

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py
  1. @app.route('/orders', methods=['POST'])
  2. def getOrder():
  3. data = request.json
  4. print('Order received : ' + json.dumps(data), flush=True)
  5. return json.dumps({'success': True}), 200, {
  6. 'ContentType': 'application/json'}
  7. app.run(port=8001)

Step 4: Run checkout service

In a new terminal window, from the root of the Quickstart clone directory navigate to the checkout directory.

  1. cd service_invocation/python/http/checkout

Install the dependencies and build the application:

  1. pip3 install -r requirements.txt

Run the checkout service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- python3 app.py

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.

  1. headers = {'dapr-app-id': 'order-processor'}
  2. result = requests.post(
  3. url='%s/orders' % (base_url),
  4. data=json.dumps(order),
  5. headers=headers
  6. )

Step 5: Use with Multi-App Run

You can run the Dapr applications in this quickstart with the Multi-App Run template. Instead of running two separate dapr run commands for the order-processor and checkout applications, run the following command:

  1. dapr run -f .

To stop all applications, run:

  1. dapr stop -f .

Step 6: View the Service Invocation outputs

Dapr invokes an application on any Dapr instance. In the code, the sidecar programming model encourages each application to talk to its own instance of Dapr. The Dapr instances then discover and communicate with one another.

checkout service output:

  1. == APP == Order passed: {"orderId": 1}
  2. == APP == Order passed: {"orderId": 2}
  3. == APP == Order passed: {"orderId": 3}
  4. == APP == Order passed: {"orderId": 4}
  5. == APP == Order passed: {"orderId": 5}
  6. == APP == Order passed: {"orderId": 6}
  7. == APP == Order passed: {"orderId": 7}
  8. == APP == Order passed: {"orderId": 8}
  9. == APP == Order passed: {"orderId": 9}
  10. == APP == Order passed: {"orderId": 10}

order-processor service output:

  1. == APP == Order received: {"orderId": 1}
  2. == APP == Order received: {"orderId": 2}
  3. == APP == Order received: {"orderId": 3}
  4. == APP == Order received: {"orderId": 4}
  5. == APP == Order received: {"orderId": 5}
  6. == APP == Order received: {"orderId": 6}
  7. == APP == Order received: {"orderId": 7}
  8. == APP == Order received: {"orderId": 9}
  9. == APP == Order received: {"orderId": 10}

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the .

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Run order-processor service

In a terminal window, from the root of the Quickstart clone directory navigate to order-processor directory.

  1. cd service_invocation/javascript/http/order-processor

Install the dependencies:

  1. npm install

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-port 5001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- npm start
  1. app.post('/orders', (req, res) => {
  2. console.log("Order received:", req.body);
  3. res.sendStatus(200);
  4. });

Step 4: Run checkout service

  1. cd service_invocation/javascript/http/checkout

Install the dependencies:

  1. npm install

Run the checkout service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- npm start

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 header, which specifies the ID of the target service.

You can run the Dapr applications in this quickstart with the . Instead of running two separate dapr run commands for the order-processor and checkout applications, run the following command:

  1. dapr run -f .

To stop all applications, run:

  1. dapr stop -f .

Step 6: View the Service Invocation outputs

Dapr invokes an application on any Dapr instance. In the code, the sidecar programming model encourages each application to talk to its own instance of Dapr. The Dapr instances then discover and communicate with one another.

checkout service output:

  1. == APP == Order passed: {"orderId": 1}
  2. == APP == Order passed: {"orderId": 2}
  3. == APP == Order passed: {"orderId": 3}
  4. == APP == Order passed: {"orderId": 4}
  5. == APP == Order passed: {"orderId": 5}
  6. == APP == Order passed: {"orderId": 6}
  7. == APP == Order passed: {"orderId": 7}
  8. == APP == Order passed: {"orderId": 8}
  9. == APP == Order passed: {"orderId": 9}
  10. == APP == Order passed: {"orderId": 10}

order-processor service output:

  1. == APP == Order received: {"orderId": 1}
  2. == APP == Order received: {"orderId": 2}
  3. == APP == Order received: {"orderId": 3}
  4. == APP == Order received: {"orderId": 4}
  5. == APP == Order received: {"orderId": 5}
  6. == APP == Order received: {"orderId": 6}
  7. == APP == Order received: {"orderId": 7}
  8. == APP == Order received: {"orderId": 8}
  9. == APP == Order received: {"orderId": 9}
  10. == APP == Order received: {"orderId": 10}

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Run order-processor service

In a terminal window, from the root of the Quickstart clone directory navigate to order-processor directory.

  1. cd service_invocation/csharp/http/order-processor

Install the dependencies:

  1. dotnet restore
  2. dotnet build

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-port 7001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run

Below is the working code block from the order processor’s Program.cs file.

  1. app.MapPost("/orders", (Order order) =>
  2. {
  3. Console.WriteLine("Order received : " + order);
  4. return order.ToString();
  5. });

Step 4: Run checkout service

In a new terminal window, from the root of the Quickstart clone directory navigate to the checkout directory.

  1. cd service_invocation/csharp/http/checkout

Install the dependencies:

  1. dotnet restore
  2. dotnet build

Run the checkout service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- dotnet run

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.

  1. var client = new HttpClient();
  2. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  3. client.DefaultRequestHeaders.Add("dapr-app-id", "order-processor");
  4. var response = await client.PostAsync($"{baseURL}/orders", content);
  5. Console.WriteLine("Order passed: " + order);

Step 5: Use with Multi-App Run

You can run the Dapr applications in this quickstart with the Multi-App Run template. Instead of running two separate dapr run commands for the order-processor and checkout applications, run the following command:

  1. dapr run -f .

To stop all applications, run:

  1. dapr stop -f .

Step 6: View the Service Invocation outputs

Dapr invokes an application on any Dapr instance. In the code, the sidecar programming model encourages each application to talk to its own instance of Dapr. The Dapr instances then discover and communicate with one another.

checkout service output:

  1. == APP == Order passed: Order { OrderId: 1 }
  2. == APP == Order passed: Order { OrderId: 2 }
  3. == APP == Order passed: Order { OrderId: 3 }
  4. == APP == Order passed: Order { OrderId: 4 }
  5. == APP == Order passed: Order { OrderId: 5 }
  6. == APP == Order passed: Order { OrderId: 6 }
  7. == APP == Order passed: Order { OrderId: 7 }
  8. == APP == Order passed: Order { OrderId: 8 }
  9. == APP == Order passed: Order { OrderId: 9 }
  10. == APP == Order passed: Order { OrderId: 10 }

order-processor service output:

  1. == APP == Order received: Order { OrderId: 1 }
  2. == APP == Order received: Order { OrderId: 2 }
  3. == APP == Order received: Order { OrderId: 3 }
  4. == APP == Order received: Order { OrderId: 4 }
  5. == APP == Order received: Order { OrderId: 5 }
  6. == APP == Order received: Order { OrderId: 6 }
  7. == APP == Order received: Order { OrderId: 7 }
  8. == APP == Order received: Order { OrderId: 9 }
  9. == APP == Order received: Order { OrderId: 10 }

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

In a terminal window, from the root of the Quickstart clone directory navigate to order-processor directory.

  1. cd service_invocation/java/http/order-processor

Run the order-processor service alongside a Dapr sidecar.

  1. public String processOrders(@RequestBody Order body) {
  2. System.out.println("Order received: "+ body.getOrderId());
  3. return "CID" + body.getOrderId();
  4. }

Step 4: Run checkout service

In a new terminal window, from the root of the Quickstart clone directory navigate to the checkout directory.

  1. cd service_invocation/java/http/checkout

Install the dependencies:

  1. mvn clean install

Run the checkout service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar

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.

  1. .header("Content-Type", "application/json")
  2. .header("dapr-app-id", "order-processor")
  3. HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
  4. System.out.println("Order passed: "+ orderId)

Step 5: Use with Multi-App Run

You can run the Dapr applications in this quickstart with the Multi-App Run template. Instead of running two separate dapr run commands for the order-processor and checkout applications, run the following command:

  1. dapr run -f .

To stop all applications, run:

  1. dapr stop -f .

Step 6: View the Service Invocation outputs

Dapr invokes an application on any Dapr instance. In the code, the sidecar programming model encourages each application to talk to its own instance of Dapr. The Dapr instances then discover and communicate with one another.

checkout service output:

  1. == APP == Order passed: 1
  2. == APP == Order passed: 2
  3. == APP == Order passed: 3
  4. == APP == Order passed: 4
  5. == APP == Order passed: 5
  6. == APP == Order passed: 6
  7. == APP == Order passed: 7
  8. == APP == Order passed: 8
  9. == APP == Order passed: 9
  10. == APP == Order passed: 10

order-processor service output:

  1. == APP == Order received: 1
  2. == APP == Order received: 2
  3. == APP == Order received: 3
  4. == APP == Order received: 4
  5. == APP == Order received: 5
  6. == APP == Order received: 6
  7. == APP == Order received: 7
  8. == APP == Order received: 8
  9. == APP == Order received: 9
  10. == APP == Order received: 10

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the .

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Run order-processor service

In a terminal window, from the root of the Quickstart clone directory navigate to order-processor directory.

  1. cd service_invocation/go/http/order-processor

Install the dependencies:

  1. go build .

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-port 6001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- go run .

Each order is received via an HTTP POST request and processed by the getOrder function.

  1. func getOrder(w http.ResponseWriter, r *http.Request) {
  2. data, err := ioutil.ReadAll(r.Body)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. log.Printf("Order received : %s", string(data))
  7. }

Step 4: Run checkout service

In a new terminal window, from the root of the Quickstart clone directory navigate to the checkout directory.

  1. cd service_invocation/go/http/checkout

Install the dependencies:

  1. go build .

Run the checkout service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- go run .

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.

  1. req.Header.Add("dapr-app-id", "order-processor")
  2. response, err := client.Do(req)

Step 5: Use with Multi-App Run

You can run the Dapr applications in this quickstart with the . Instead of running two separate dapr run commands for the order-processor and checkout applications, run the following command:

  1. dapr run -f .

To stop all applications, run:

  1. dapr stop -f .

Step 6: View the Service Invocation outputs

Dapr invokes an application on any Dapr instance. In the code, the sidecar programming model encourages each application to talk to its own instance of Dapr. The Dapr instances then discover and communicate with one another.

checkout service output:

order-processor service output:

  1. == APP == Order received : {"orderId":1}
  2. == APP == Order received : {"orderId":2}
  3. == APP == Order received : {"orderId":3}
  4. == APP == Order received : {"orderId":4}
  5. == APP == Order received : {"orderId":5}
  6. == APP == Order received : {"orderId":6}
  7. == APP == Order received : {"orderId":7}
  8. == APP == Order received : {"orderId":8}
  9. == APP == Order received : {"orderId":9}
  10. == APP == Order received : {"orderId":10}

We’re continuously working to improve our Quickstart examples and value your feedback. Did you find this Quickstart helpful? Do you have suggestions for improvement?

Join the discussion in our discord channel.

Next Steps