Dapr 调用API
默认情况下,Dapr 挎斗 (SideCar) 依赖于网关来限制对其公共 API 的访问。 因此,请清除“为 HTTPS 配置”复选框:
打开 NuGet 包管理器,添加以下包
Dapr.AspNetCore
打开 Program.cs 文件并注入 DaprClient
服务:
// Add services to the container. builder.Services.AddDaprClient();
调用服务:
using Dapr.Client; using Microsoft.AspNetCore.Mvc.RazorPages; namespace MyFrontEnd.Pages; public class IndexModel : PageModel { private readonly DaprClient _daprClient; public IndexModel(DaprClient daprClient) { _daprClient = daprClient; } public async Task OnGet() { var forecasts = await _daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>( HttpMethod.Get, "MyBackEnd", "weatherforecast"); ViewData["WeatherForecastData"] = forecasts; } }
使用 DaprClient.InvokeMethodAsync
方法来调用 MyBackEnd(应用程序 ID)
服务的 weatherforecast
方法
先后在每个项目上添加 Docker Compose ,最终Docker Compose文件如下:
version: '3.4' services: myfrontend: image: ${DOCKER_REGISTRY-}myfrontend build: context: . dockerfile: MyFrontEnd/Dockerfile mybackend: image: ${DOCKER_REGISTRY-}mybackend build: context: . dockerfile: MyBackEnd/Dockerfile
将 Dapr 挎斗容器添加到 Compose 文件。 请谨慎地将 docker-compose.yml 文件的内容更新为与以下示例相匹配。 请密切注意格式和间距,切勿使用制表符。
version: '3.4' services: myfrontend: image: ${DOCKER_REGISTRY-}myfrontend build: context: . dockerfile: MyFrontEnd/Dockerfile ports: - "53000:50001" myfrontend-dapr: image: "daprio/daprd:latest" command: [ "./daprd", "-app-id", "MyFrontEnd", "-app-port", "80" ] depends_on: - myfrontend network_mode: "service:myfrontend" mybackend: image: ${DOCKER_REGISTRY-}mybackend build: context: . dockerfile: MyBackEnd/Dockerfile ports: - "52000:50001" mybackend-dapr: image: "daprio/daprd:latest" command: [ "./daprd", "-app-id", "MyBackEnd", "-app-port", "80" ] depends_on: - mybackend network_mode: "service:mybackend"
更新后的文件中,我们分别为 myfrontend
和 mybackend
服务添加了 myfrontend-dapr
和 mybackend-dapr
挎斗。
标签:API,image,daprClient,访问,Dapr,mybackend,MyBackEnd,myfrontend From: https://www.cnblogs.com/friend/p/16823735.html