How-To: Invoke services using HTTP

Call between services using service invocation

This article describe how to deploy services each with an unique application ID, so that other services can discover and call endpoints on them using service invocation API.

Step 1: Choose an ID for your service

Dapr allows you to assign a global, unique ID for your app. This ID encapsulates the state for your application, regardless of the number of instances it may have.

In self hosted mode, set the --app-id flag:

  1. dapr run --app-id cart --app-port 5000 python app.py

If your app uses an SSL connection, you can tell Dapr to invoke your app over an insecure SSL connection:

  1. dapr run --app-id cart --app-port 5000 --app-ssl python app.py

Setup an ID using Kubernetes

In Kubernetes, set the dapr.io/app-id annotation on your pod:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: python-app
  5. namespace: default
  6. labels:
  7. app: python-app
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: python-app
  13. template:
  14. metadata:
  15. labels:
  16. app: python-app
  17. annotations:
  18. dapr.io/enabled: "true"
  19. dapr.io/app-id: "cart"
  20. dapr.io/app-port: "5000"
  21. ...

If your app uses an SSL connection, you can tell Dapr to invoke your app over an insecure SSL connection with the app-ssl: "true" annotation (full list here)

Step 2: Setup a service

The following is a Python example of a cart app. It can be written in any programming language.

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route('/add', methods=['POST'])
  4. def add():
  5. return "Added!"
  6. if __name__ == '__main__':
  7. app.run()

This Python app exposes an add() method via the /add endpoint.

Step 3: Invoke the service

Dapr uses a sidecar, decentralized architecture. To invoke an application using Dapr, you can use the invoke API on any Dapr instance.

The sidecar programming model encourages each applications to talk to its own instance of Dapr. The Dapr instances discover and communicate with one another.

From a terminal or command prompt run:

  1. curl http://localhost:3500/v1.0/invoke/cart/method/add -X POST

Since the add endpoint is a ‘POST’ method, we used -X POST in the curl command.

To invoke a ‘GET’ endpoint:

  1. curl http://localhost:3500/v1.0/invoke/cart/method/add

To invoke a ‘DELETE’ endpoint:

  1. curl http://localhost:3500/v1.0/invoke/cart/method/add -X DELETE

Dapr puts any payload returned by the called service in the HTTP response’s body.

  1. dapr invoke --app-id cart --method add

Namespaces

When running on namespace supported platforms, you include the namespace of the target app in the app ID: myApp.production

For example, invoking the example python service with a namespace would be:

  1. curl http://localhost:3500/v1.0/invoke/cart.production/method/add -X POST

See the Cross namespace API spec for more information on namespaces.

Step 4: View traces and logs

The example above showed you how to directly invoke a different service running locally or in Kubernetes. Dapr outputs metrics, tracing and logging information allowing you to visualize a call graph between services, log errors and optionally log the payload body.

For more information on tracing and logs see the observability article.

Related Links

Last modified March 18, 2021: Merge pull request #1321 from dapr/aacrawfi/logos (9a399d5)