The documentation you are viewing is for Dapr v1.7 which is an older version of Dapr. For up-to-date documentation, see the latest version.

入门指南:发现并调用服务

入门指南指导如何使用 Dapr 服务在分布式应用程序中调用其它服务

本文介绍如何使用唯一的应用程序 ID 部署每个服务,以便其他服务可以使用服务调用 API 发现和调用这些终结点。

步骤 1: 为服务选择标识

Dapr 允许您为您的应用分配一个全局唯一ID。 此 ID 为您的应用程序封装了状态,不管它可能有多少实例。


在自托管方式下,设置 --app-id 标记:

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

如果您的应用使用 SSL 连接,您可以告诉Dapr 在不安全的 SSL 连接中调用您的应用:

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

使用 Kubernetes 设置标识

在 Kubernetes 中,在您的pod 上设置 dapr.io/app-id 注解:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-app
  namespace: default
  labels:
    app: python-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: python-app
  template:
    metadata:
      labels:
        app: python-app
      annotations:
        dapr.io/enabled: "true"
        dapr.io/app-id: "cart"
        dapr.io/app-port: "5000"
...

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)

步骤 2: 设置服务

以下是购物车应用的 Python 示例。 它可以用任何编程语言编写。

from flask import Flask
app = Flask(__name__)

@app.route('/add', methods=['POST'])
def add():
    return "Added!"

if __name__ == '__main__':
    app.run()

此 Python 应用程序通过 /add() 端点暴露了一个 add() 方法。

步骤 3: 调用服务

Dapr 采用边车(sidecar)、去中心化的架构。 要使用 Dapr 调用应用程序,您可以在任意 Dapr 实例中使用 调用 API。

sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话。 Dapr 实例会相互发现并进行通信。


从一个终端或命令提示运行:

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

由于 add 端点是一个“POST”方法,我们在 curl 命令中使用了 -X POST

要调用 “GET” 端点:

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

要调用 “DELETE” 端点:

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

Dapr 将调用的服务返回的任何有效负载放在 HTTP 响应的消息体中。


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

命名空间

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

例如,调用包含名称空间的示例 python 服务:

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

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

步骤 4: 查看跟踪和日志

上面的示例显示了如何直接调用本地或 Kubernetes 中运行的其他服务。 Dapr 输出指标、跟踪和日志记录信息,允许您可视化服务之间的调用图、日志错误和可选地记录有效负载正文。

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

相关链接