快速入门:服务调用

开始使用 Dapr 的服务调用构建块

通过Dapr的服务调用构建块,你的应用程序可以与其他应用程序进行可靠和安全的通信。

Diagram showing the steps of service invocation

Dapr 提供了几种服务调用方法,你可以根据你的方案选择这些方法。 在本快速入门中,你将启用 checkout 服务以HTTP 代理调用 order-processo 服务中的方法并且通过以下方式之一:

概述文章中了解更多关于Dapr的服务调用方法。

使用多应用程序运行

在继续快速入门之前,请选择您首选的语言。

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

从 Quickstart 克隆目录的根目录导航到 quickstart 目录。

  1. cd service_invocation/python/http

安装 order-processorcheckout 应用的依赖项:

  1. cd ./order-processor
  2. pip3 install -r requirements.txt
  3. cd ../checkout
  4. pip3 install -r requirements.txt
  5. cd ..

第3步:运行 order-processorcheckout 服务

通过以下命令,同时运行以下服务,并在其自己的 Dapr sidecar 旁边运行:

  • order-processor 服务
  • checkout服务
  1. dapr run -f .

注意:由于Python3.exe在Windows中未定义,您可能需要在运行dapr run -f .之前,将python3更改为python,请参考dapr.yaml文件。

预期输出

  1. == APP - order-processor == Order received : Order { orderId = 1 }
  2. == APP - checkout == Order passed: Order { OrderId = 1 }
  3. == APP - order-processor == Order received : Order { orderId = 2 }
  4. == APP - checkout == Order passed: Order { OrderId = 2 }
  5. == APP - order-processor == Order received : Order { orderId = 3 }
  6. == APP - checkout == Order passed: Order { OrderId = 3 }
  7. == APP - order-processor == Order received : Order { orderId = 4 }
  8. == APP - checkout == Order passed: Order { OrderId = 4 }
  9. == APP - order-processor == Order received : Order { orderId = 5 }
  10. == APP - checkout == Order passed: Order { OrderId = 5 }
  11. == APP - order-processor == Order received : Order { orderId = 6 }
  12. == APP - checkout == Order passed: Order { OrderId = 6 }
  13. == APP - order-processor == Order received : Order { orderId = 7 }
  14. == APP - checkout == Order passed: Order { OrderId = 7 }
  15. == APP - order-processor == Order received : Order { orderId = 8 }
  16. == APP - checkout == Order passed: Order { OrderId = 8 }
  17. == APP - order-processor == Order received : Order { orderId = 9 }
  18. == APP - checkout == Order passed: Order { OrderId = 9 }
  19. == APP - order-processor == Order received : Order { orderId = 10 }
  20. == APP - checkout == Order passed: Order { OrderId = 10 }
  21. == APP - order-processor == Order received : Order { orderId = 11 }
  22. == APP - checkout == Order passed: Order { OrderId = 11 }
  23. == APP - order-processor == Order received : Order { orderId = 12 }
  24. == APP - checkout == Order passed: Order { OrderId = 12 }
  25. == APP - order-processor == Order received : Order { orderId = 13 }
  26. == APP - checkout == Order passed: Order { OrderId = 13 }
  27. == APP - order-processor == Order received : Order { orderId = 14 }
  28. == APP - checkout == Order passed: Order { OrderId = 14 }
  29. == APP - order-processor == Order received : Order { orderId = 15 }
  30. == APP - checkout == Order passed: Order { OrderId = 15 }
  31. == APP - order-processor == Order received : Order { orderId = 16 }
  32. == APP - checkout == Order passed: Order { OrderId = 16 }
  33. == APP - order-processor == Order received : Order { orderId = 17 }
  34. == APP - checkout == Order passed: Order { OrderId = 17 }
  35. == APP - order-processor == Order received : Order { orderId = 18 }
  36. == APP - checkout == Order passed: Order { OrderId = 18 }
  37. == APP - order-processor == Order received : Order { orderId = 19 }
  38. == APP - checkout == Order passed: Order { OrderId = 19 }
  39. == APP - order-processor == Order received : Order { orderId = 20 }
  40. == APP - checkout == Order passed: Order { OrderId = 20 }
  41. Exited App successfully

发生了什么?

在此快速入门中运行 dapr run -f .,使用 dapr.yaml Multi-App Run 模板文件同时启动订阅者发布者应用程序。

dapr.yaml 多应用运行模板文件

使用 dapr run -f . 运行多应用运行模板文件,启动项目中的所有应用程序。 在这个快速入门中,dapr.yaml文件包含以下内容:

  1. version: 1
  2. apps:
  3. - appDirPath: ./order-processor/
  4. appID: order-processor
  5. appPort: 8001
  6. command: ["python3", "app.py"]
  7. - appID: checkout
  8. appDirPath: ./checkout/
  9. command: ["python3", "app.py"]
order-processor 服务

order-processor服务接收来自checkout服务的调用:

  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)

checkout服务

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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. )

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

从 Quickstart 克隆目录的根目录导航到 quickstart 目录。

  1. cd service_invocation/javascript/http

安装 order-processorcheckout 应用的依赖项:

  1. cd ./order-processor
  2. npm install
  3. cd ../checkout
  4. npm install
  5. cd ..

第3步:运行 order-processorcheckout 服务

通过以下命令,同时运行以下服务,并在其自己的 Dapr sidecar 旁边运行:

  • order-processor 服务
  • checkout服务
  1. dapr run -f .

预期输出

  1. == APP - order-processor == Order received : Order { orderId = 1 }
  2. == APP - checkout == Order passed: Order { OrderId = 1 }
  3. == APP - order-processor == Order received : Order { orderId = 2 }
  4. == APP - checkout == Order passed: Order { OrderId = 2 }
  5. == APP - order-processor == Order received : Order { orderId = 3 }
  6. == APP - checkout == Order passed: Order { OrderId = 3 }
  7. == APP - order-processor == Order received : Order { orderId = 4 }
  8. == APP - checkout == Order passed: Order { OrderId = 4 }
  9. == APP - order-processor == Order received : Order { orderId = 5 }
  10. == APP - checkout == Order passed: Order { OrderId = 5 }
  11. == APP - order-processor == Order received : Order { orderId = 6 }
  12. == APP - checkout == Order passed: Order { OrderId = 6 }
  13. == APP - order-processor == Order received : Order { orderId = 7 }
  14. == APP - checkout == Order passed: Order { OrderId = 7 }
  15. == APP - order-processor == Order received : Order { orderId = 8 }
  16. == APP - checkout == Order passed: Order { OrderId = 8 }
  17. == APP - order-processor == Order received : Order { orderId = 9 }
  18. == APP - checkout == Order passed: Order { OrderId = 9 }
  19. == APP - order-processor == Order received : Order { orderId = 10 }
  20. == APP - checkout == Order passed: Order { OrderId = 10 }
  21. == APP - order-processor == Order received : Order { orderId = 11 }
  22. == APP - checkout == Order passed: Order { OrderId = 11 }
  23. == APP - order-processor == Order received : Order { orderId = 12 }
  24. == APP - checkout == Order passed: Order { OrderId = 12 }
  25. == APP - order-processor == Order received : Order { orderId = 13 }
  26. == APP - checkout == Order passed: Order { OrderId = 13 }
  27. == APP - order-processor == Order received : Order { orderId = 14 }
  28. == APP - checkout == Order passed: Order { OrderId = 14 }
  29. == APP - order-processor == Order received : Order { orderId = 15 }
  30. == APP - checkout == Order passed: Order { OrderId = 15 }
  31. == APP - order-processor == Order received : Order { orderId = 16 }
  32. == APP - checkout == Order passed: Order { OrderId = 16 }
  33. == APP - order-processor == Order received : Order { orderId = 17 }
  34. == APP - checkout == Order passed: Order { OrderId = 17 }
  35. == APP - order-processor == Order received : Order { orderId = 18 }
  36. == APP - checkout == Order passed: Order { OrderId = 18 }
  37. == APP - order-processor == Order received : Order { orderId = 19 }
  38. == APP - checkout == Order passed: Order { OrderId = 19 }
  39. == APP - order-processor == Order received : Order { orderId = 20 }
  40. == APP - checkout == Order passed: Order { OrderId = 20 }
  41. Exited App successfully

发生了什么?

在此快速入门中运行 dapr run -f .,使用 dapr.yaml Multi-App Run 模板文件同时启动订阅者发布者应用程序。

dapr.yaml 多应用运行模板文件

使用 dapr run -f . 运行多应用运行模板文件,启动项目中的所有应用程序。 在这个快速入门中,dapr.yaml文件包含以下内容:

  1. version: 1
  2. apps:
  3. - appDirPath: ./order-processor/
  4. appID: order-processor
  5. appPort: 5001
  6. command: ["npm", "start"]
  7. - appID: checkout
  8. appDirPath: ./checkout/
  9. command: ["npm", "start"]
order-processor 服务

order-processor服务接收来自checkout服务的调用:

  1. app.post('/orders', (req, res) => {
  2. console.log("Order received:", req.body);
  3. res.sendStatus(200);
  4. });
checkout服务

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  1. let axiosConfig = {
  2. headers: {
  3. "dapr-app-id": "order-processor"
  4. }
  5. };
  6. const res = await axios.post(`${DAPR_HOST}:${DAPR_HTTP_PORT}/orders`, order , axiosConfig);
  7. console.log("Order passed: " + res.config.data);

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

从 Quickstart 克隆目录的根目录导航到 quickstart 目录。

  1. cd service_invocation/csharp/http

安装 order-processorcheckout 应用的依赖项:

  1. cd ./order-processor
  2. dotnet restore
  3. dotnet build
  4. cd ../checkout
  5. dotnet restore
  6. dotnet build
  7. cd ..

第3步:运行 order-processorcheckout 服务

通过以下命令,同时运行以下服务,并在其自己的 Dapr sidecar 旁边运行:

  • order-processor 服务
  • checkout服务
  1. dapr run -f .

预期输出

  1. == APP - order-processor == Order received : Order { orderId = 1 }
  2. == APP - checkout == Order passed: Order { OrderId = 1 }
  3. == APP - order-processor == Order received : Order { orderId = 2 }
  4. == APP - checkout == Order passed: Order { OrderId = 2 }
  5. == APP - order-processor == Order received : Order { orderId = 3 }
  6. == APP - checkout == Order passed: Order { OrderId = 3 }
  7. == APP - order-processor == Order received : Order { orderId = 4 }
  8. == APP - checkout == Order passed: Order { OrderId = 4 }
  9. == APP - order-processor == Order received : Order { orderId = 5 }
  10. == APP - checkout == Order passed: Order { OrderId = 5 }
  11. == APP - order-processor == Order received : Order { orderId = 6 }
  12. == APP - checkout == Order passed: Order { OrderId = 6 }
  13. == APP - order-processor == Order received : Order { orderId = 7 }
  14. == APP - checkout == Order passed: Order { OrderId = 7 }
  15. == APP - order-processor == Order received : Order { orderId = 8 }
  16. == APP - checkout == Order passed: Order { OrderId = 8 }
  17. == APP - order-processor == Order received : Order { orderId = 9 }
  18. == APP - checkout == Order passed: Order { OrderId = 9 }
  19. == APP - order-processor == Order received : Order { orderId = 10 }
  20. == APP - checkout == Order passed: Order { OrderId = 10 }
  21. == APP - order-processor == Order received : Order { orderId = 11 }
  22. == APP - checkout == Order passed: Order { OrderId = 11 }
  23. == APP - order-processor == Order received : Order { orderId = 12 }
  24. == APP - checkout == Order passed: Order { OrderId = 12 }
  25. == APP - order-processor == Order received : Order { orderId = 13 }
  26. == APP - checkout == Order passed: Order { OrderId = 13 }
  27. == APP - order-processor == Order received : Order { orderId = 14 }
  28. == APP - checkout == Order passed: Order { OrderId = 14 }
  29. == APP - order-processor == Order received : Order { orderId = 15 }
  30. == APP - checkout == Order passed: Order { OrderId = 15 }
  31. == APP - order-processor == Order received : Order { orderId = 16 }
  32. == APP - checkout == Order passed: Order { OrderId = 16 }
  33. == APP - order-processor == Order received : Order { orderId = 17 }
  34. == APP - checkout == Order passed: Order { OrderId = 17 }
  35. == APP - order-processor == Order received : Order { orderId = 18 }
  36. == APP - checkout == Order passed: Order { OrderId = 18 }
  37. == APP - order-processor == Order received : Order { orderId = 19 }
  38. == APP - checkout == Order passed: Order { OrderId = 19 }
  39. == APP - order-processor == Order received : Order { orderId = 20 }
  40. == APP - checkout == Order passed: Order { OrderId = 20 }
  41. Exited App successfully

发生了什么?

在此快速入门中运行 dapr run -f .,使用 dapr.yaml Multi-App Run 模板文件同时启动订阅者发布者应用程序。

dapr.yaml 多应用运行模板文件

使用 dapr run -f . 运行多应用运行模板文件,启动项目中的所有应用程序。 在这个快速入门中,dapr.yaml文件包含以下内容:

  1. version: 1
  2. apps:
  3. - appDirPath: ./order-processor/
  4. appID: order-processor
  5. appPort: 7001
  6. command: ["dotnet", "run"]
  7. - appID: checkout
  8. appDirPath: ./checkout/
  9. command: ["dotnet", "run"]
order-processor 服务

order-processor服务接收来自checkout服务的调用:

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

checkout服务的Program.cs文件中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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);

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

从 Quickstart 克隆目录的根目录导航到 quickstart 目录。

  1. cd service_invocation/java/http

安装 order-processorcheckout 应用的依赖项:

  1. cd ./order-processor
  2. mvn clean install
  3. cd ../checkout
  4. mvn clean install
  5. cd ..

第3步:运行 order-processorcheckout 服务

通过以下命令,同时运行以下服务,并在其自己的 Dapr sidecar 旁边运行:

  • order-processor 服务
  • checkout服务
  1. dapr run -f .

预期输出

  1. == APP - order-processor == Order received : Order { orderId = 1 }
  2. == APP - checkout == Order passed: Order { OrderId = 1 }
  3. == APP - order-processor == Order received : Order { orderId = 2 }
  4. == APP - checkout == Order passed: Order { OrderId = 2 }
  5. == APP - order-processor == Order received : Order { orderId = 3 }
  6. == APP - checkout == Order passed: Order { OrderId = 3 }
  7. == APP - order-processor == Order received : Order { orderId = 4 }
  8. == APP - checkout == Order passed: Order { OrderId = 4 }
  9. == APP - order-processor == Order received : Order { orderId = 5 }
  10. == APP - checkout == Order passed: Order { OrderId = 5 }
  11. == APP - order-processor == Order received : Order { orderId = 6 }
  12. == APP - checkout == Order passed: Order { OrderId = 6 }
  13. == APP - order-processor == Order received : Order { orderId = 7 }
  14. == APP - checkout == Order passed: Order { OrderId = 7 }
  15. == APP - order-processor == Order received : Order { orderId = 8 }
  16. == APP - checkout == Order passed: Order { OrderId = 8 }
  17. == APP - order-processor == Order received : Order { orderId = 9 }
  18. == APP - checkout == Order passed: Order { OrderId = 9 }
  19. == APP - order-processor == Order received : Order { orderId = 10 }
  20. == APP - checkout == Order passed: Order { OrderId = 10 }
  21. == APP - order-processor == Order received : Order { orderId = 11 }
  22. == APP - checkout == Order passed: Order { OrderId = 11 }
  23. == APP - order-processor == Order received : Order { orderId = 12 }
  24. == APP - checkout == Order passed: Order { OrderId = 12 }
  25. == APP - order-processor == Order received : Order { orderId = 13 }
  26. == APP - checkout == Order passed: Order { OrderId = 13 }
  27. == APP - order-processor == Order received : Order { orderId = 14 }
  28. == APP - checkout == Order passed: Order { OrderId = 14 }
  29. == APP - order-processor == Order received : Order { orderId = 15 }
  30. == APP - checkout == Order passed: Order { OrderId = 15 }
  31. == APP - order-processor == Order received : Order { orderId = 16 }
  32. == APP - checkout == Order passed: Order { OrderId = 16 }
  33. == APP - order-processor == Order received : Order { orderId = 17 }
  34. == APP - checkout == Order passed: Order { OrderId = 17 }
  35. == APP - order-processor == Order received : Order { orderId = 18 }
  36. == APP - checkout == Order passed: Order { OrderId = 18 }
  37. == APP - order-processor == Order received : Order { orderId = 19 }
  38. == APP - checkout == Order passed: Order { OrderId = 19 }
  39. == APP - order-processor == Order received : Order { orderId = 20 }
  40. == APP - checkout == Order passed: Order { OrderId = 20 }
  41. Exited App successfully

发生了什么?

在此快速入门中运行 dapr run -f .,使用 dapr.yaml Multi-App Run 模板文件同时启动订阅者发布者应用程序。

dapr.yaml 多应用运行模板文件

使用 dapr run -f . 运行多应用运行模板文件,启动项目中的所有应用程序。 在这个快速入门中,dapr.yaml文件包含以下内容:

  1. version: 1
  2. apps:
  3. - appDirPath: ./order-processor/
  4. appID: order-processor
  5. appPort: 9001
  6. command: ["java", "-jar", "target/OrderProcessingService-0.0.1-SNAPSHOT.jar"]
  7. - appID: checkout
  8. appDirPath: ./checkout/
  9. command: ["java", "-jar", "target/CheckoutService-0.0.1-SNAPSHOT.jar"]
order-processor 服务

order-processor服务接收来自checkout服务的调用:

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

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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)

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

从 Quickstart 克隆目录的根目录导航到 quickstart 目录。

  1. cd service_invocation/go/http

安装 order-processorcheckout 应用的依赖项:

  1. cd ./order-processor
  2. go build .
  3. cd ../checkout
  4. go build .
  5. cd ..

第3步:运行 order-processorcheckout 服务

通过以下命令,同时运行以下服务,并在其自己的 Dapr sidecar 旁边运行:

  • order-processor 服务
  • checkout服务
  1. dapr run -f .

预期输出

  1. == APP - order-processor == Order received : Order { orderId = 1 }
  2. == APP - checkout == Order passed: Order { OrderId = 1 }
  3. == APP - order-processor == Order received : Order { orderId = 2 }
  4. == APP - checkout == Order passed: Order { OrderId = 2 }
  5. == APP - order-processor == Order received : Order { orderId = 3 }
  6. == APP - checkout == Order passed: Order { OrderId = 3 }
  7. == APP - order-processor == Order received : Order { orderId = 4 }
  8. == APP - checkout == Order passed: Order { OrderId = 4 }
  9. == APP - order-processor == Order received : Order { orderId = 5 }
  10. == APP - checkout == Order passed: Order { OrderId = 5 }
  11. == APP - order-processor == Order received : Order { orderId = 6 }
  12. == APP - checkout == Order passed: Order { OrderId = 6 }
  13. == APP - order-processor == Order received : Order { orderId = 7 }
  14. == APP - checkout == Order passed: Order { OrderId = 7 }
  15. == APP - order-processor == Order received : Order { orderId = 8 }
  16. == APP - checkout == Order passed: Order { OrderId = 8 }
  17. == APP - order-processor == Order received : Order { orderId = 9 }
  18. == APP - checkout == Order passed: Order { OrderId = 9 }
  19. == APP - order-processor == Order received : Order { orderId = 10 }
  20. == APP - checkout == Order passed: Order { OrderId = 10 }
  21. == APP - order-processor == Order received : Order { orderId = 11 }
  22. == APP - checkout == Order passed: Order { OrderId = 11 }
  23. == APP - order-processor == Order received : Order { orderId = 12 }
  24. == APP - checkout == Order passed: Order { OrderId = 12 }
  25. == APP - order-processor == Order received : Order { orderId = 13 }
  26. == APP - checkout == Order passed: Order { OrderId = 13 }
  27. == APP - order-processor == Order received : Order { orderId = 14 }
  28. == APP - checkout == Order passed: Order { OrderId = 14 }
  29. == APP - order-processor == Order received : Order { orderId = 15 }
  30. == APP - checkout == Order passed: Order { OrderId = 15 }
  31. == APP - order-processor == Order received : Order { orderId = 16 }
  32. == APP - checkout == Order passed: Order { OrderId = 16 }
  33. == APP - order-processor == Order received : Order { orderId = 17 }
  34. == APP - checkout == Order passed: Order { OrderId = 17 }
  35. == APP - order-processor == Order received : Order { orderId = 18 }
  36. == APP - checkout == Order passed: Order { OrderId = 18 }
  37. == APP - order-processor == Order received : Order { orderId = 19 }
  38. == APP - checkout == Order passed: Order { OrderId = 19 }
  39. == APP - order-processor == Order received : Order { orderId = 20 }
  40. == APP - checkout == Order passed: Order { OrderId = 20 }
  41. Exited App successfully

发生了什么?

在此快速入门中运行 dapr run -f .,使用 dapr.yaml Multi-App Run 模板文件同时启动订阅者发布者应用程序。

dapr.yaml 多应用运行模板文件

使用 dapr run -f . 运行多应用运行模板文件,启动项目中的所有应用程序。 在这个快速入门中,dapr.yaml文件包含以下内容:

  1. version: 1
  2. apps:
  3. - appDirPath: ./order-processor/
  4. appID: order-processor
  5. appPort: 6006
  6. command: ["go", "run", "."]
  7. - appID: checkout
  8. appDirPath: ./checkout/
  9. command: ["go", "run", "."]
order-processor 服务

order-processo服务中,每个订单都通过HTTP POST请求接收,并由getOrder函数处理。

  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. }
checkout服务

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

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

一次只运行一个应用程序

在继续快速入门之前,请选择您首选的语言。

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

第3步:运行 order-processor 服务

在终端窗口中,从 Quickstart 克隆目录的根目录 导航到 order-processor 目录。

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

安装依赖项并构建应用程序:

  1. pip3 install -r requirements.txt

与 Dapr sidecar 一起运行 order-processor 服务。

  1. dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py

注意:由于Python3.exe在Windows中未定义,您可能需要使用python app.py而不是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)

步骤4:运行checkout服务

在新的终端窗口中,从 Quickstart 克隆目录的根目录导航到 checkout 目录。

  1. cd service_invocation/python/http/checkout

安装依赖项并构建应用程序:

  1. pip3 install -r requirements.txt

与 Dapr sidecar 一起运行 checkout 服务。

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

注意:由于Python3.exe在Windows中未定义,您可能需要使用python app.py而不是python3 app.py

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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. )

第5步:与多应用程序运行一起使用

您可以使用Dapr多应用程序运行模板 在此快速入门中运行Dapr应用程序。 而不是运行两个单独的 dapr run 命令 order-processorcheckout 应用程序,请运行以下命令:

  1. dapr run -f .

要停止所有应用程序,请运行:

  1. dapr stop -f .

第6步:查看服务调用输出

Dapr 在任何 Dapr 实例上调用应用程序。 在代码中,Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话通信。 Dapr 实例随后会相互发现并进行通信。

checkout服务输出:

  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服务输出:

  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}

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

第3步:运行 order-processor 服务

在终端窗口中,从 Quickstart 克隆目录的根目录 导航到 order-processor 目录。

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

安装依赖项:

  1. npm install

与 Dapr sidecar 一起运行 order-processor 服务。

  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. });

步骤4:运行checkout服务

在新的终端窗口中,从 Quickstart 克隆目录的根目录导航到 checkout 目录。

  1. cd service_invocation/javascript/http/checkout

安装依赖项:

  1. npm install

与 Dapr sidecar 一起运行 checkout 服务。

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

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  1. let axiosConfig = {
  2. headers: {
  3. "dapr-app-id": "order-processor"
  4. }
  5. };
  6. const res = await axios.post(`${DAPR_HOST}:${DAPR_HTTP_PORT}/orders`, order , axiosConfig);
  7. console.log("Order passed: " + res.config.data);

第5步:与多应用程序运行一起使用

您可以使用Dapr多应用程序运行模板 在此快速入门中运行Dapr应用程序。 而不是运行两个单独的 dapr run 命令 order-processorcheckout 应用程序,请运行以下命令:

  1. dapr run -f .

要停止所有应用程序,请运行:

  1. dapr stop -f .

第6步:查看服务调用输出

Dapr 在任何 Dapr 实例上调用应用程序。 在代码中,Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话通信。 Dapr 实例随后会相互发现并进行通信。

checkout服务输出:

  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服务输出:

  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}

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

第3步:运行 order-processor 服务

在终端窗口中,从 Quickstart 克隆目录的根目录 导航到 order-processor 目录。

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

安装依赖项:

  1. dotnet restore
  2. dotnet build

与 Dapr sidecar 一起运行 order-processor 服务。

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

下面是订单处理器的工作代码块 Program.cs 文件。

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

步骤4:运行checkout服务

在新的终端窗口中,从 Quickstart 克隆目录的根目录导航到 checkout 目录。

  1. cd service_invocation/csharp/http/checkout

安装依赖项:

  1. dotnet restore
  2. dotnet build

与 Dapr sidecar 一起运行 checkout 服务。

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

checkout服务的Program.cs文件中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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);

第5步:与多应用程序运行一起使用

您可以使用Dapr多应用程序运行模板 在此快速入门中运行Dapr应用程序。 而不是运行两个单独的 dapr run 命令 order-processorcheckout 应用程序,请运行以下命令:

  1. dapr run -f .

要停止所有应用程序,请运行:

  1. dapr stop -f .

第6步:查看服务调用输出

Dapr 在任何 Dapr 实例上调用应用程序。 在代码中,Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话通信。 Dapr 实例随后会相互发现并进行通信。

checkout服务输出:

  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服务输出:

  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: 8 }
  9. == APP == Order received: Order { OrderId: 9 }
  10. == APP == Order received: Order { OrderId: 10 }

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

第3步:运行 order-processor 服务

在终端窗口中,从 Quickstart 克隆目录的根目录 导航到 order-processor 目录。

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

安装依赖项:

  1. mvn clean install

与 Dapr sidecar 一起运行 order-processor 服务。

  1. dapr run --app-id order-processor --app-port 9001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
  1. public String processOrders(@RequestBody Order body) {
  2. System.out.println("Order received: "+ body.getOrderId());
  3. return "CID" + body.getOrderId();
  4. }

步骤4:运行checkout服务

在新的终端窗口中,从 Quickstart 克隆目录的根目录导航到 checkout 目录。

  1. cd service_invocation/java/http/checkout

安装依赖项:

  1. mvn clean install

与 Dapr sidecar 一起运行 checkout 服务。

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

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

  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)

第5步:与多应用程序运行一起使用

您可以使用Dapr多应用程序运行模板 在此快速入门中运行Dapr应用程序。 而不是运行两个单独的 dapr run 命令 order-processorcheckout 应用程序,请运行以下命令:

  1. dapr run -f .

要停止所有应用程序,请运行:

  1. dapr stop -f .

第6步:查看服务调用输出

Dapr 在任何 Dapr 实例上调用应用程序。 在代码中,Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话通信。 Dapr 实例随后会相互发现并进行通信。

checkout服务输出:

  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服务输出:

  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

第1步:先决条件

对于此示例,您将需要:

第2步:设置环境

克隆在Quickstarts存储库中提供的示例

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

第3步:运行 order-processor 服务

在终端窗口中,从 Quickstart 克隆目录的根目录 导航到 order-processor 目录。

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

安装依赖项:

  1. go build .

与 Dapr sidecar 一起运行 order-processor 服务。

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

每个订单都通过HTTP POST请求接收,并由 getOrder函数处理。

  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. }

步骤4:运行checkout服务

在新的终端窗口中,从 Quickstart 克隆目录的根目录导航到 checkout 目录。

  1. cd service_invocation/go/http/checkout

安装依赖项:

  1. go build .

与 Dapr sidecar 一起运行 checkout 服务。

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

checkout服务中,您会注意到无需重写您的应用程序代码即可使用Dapr的服务调用。 您可以通过简单地添加 dapr-app-id 标头来启用服务调用,该标头指定目标服务的ID。

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

第5步:与多应用程序运行一起使用

您可以使用Dapr多应用程序运行模板 在此快速入门中运行Dapr应用程序。 而不是运行两个单独的 dapr run 命令 order-processorcheckout 应用程序,请运行以下命令:

  1. dapr run -f .

要停止所有应用程序,请运行:

  1. dapr stop -f .

第6步:查看服务调用输出

Dapr 在任何 Dapr 实例上调用应用程序。 在代码中,Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话通信。 Dapr 实例随后会相互发现并进行通信。

checkout服务输出:

  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服务输出:

  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}

告诉我们您的想法

我们一直在努力改进我们的快速入门示例,并重视您的反馈。 您觉得此快速入门有帮助吗? 您有改进的建议吗?

加入我们的discord频道参与讨论。

下一步

  • 概述文章中了解更多关于Dapr的服务调用方法。
  • 了解更多关于如何调用 Dapr 的服务调用:

探索 Dapr 教程 >>