前言
上一篇對Dapr進行了了解,並搭建了Dapr環境。接下來就對Dapr的各個構建塊類型的了解、應用實際案例。
一、服務調用:
在許多具有多個需要相互通信的服務的環境中,都會面臨着很多問題。 如:
- 維護其他服務的地址。
- 如何安全地調用服務。
- 在發生短暫的 暫時性錯誤 時如何處理重試。
- 分布式應用程序調用鏈路追蹤。
服務調用構建塊通過使用 Dapr 挎斗作為服務的 反向代理 來解決這些難題。
調用邏輯:
服務調用就是應用程序可以使用 gRPC 或 HTTP 這樣的標准協議來發現並可靠地與其他應用程序通信。
Dapr的服務調用如何工作的總覽圖,如下:
-
服務 A 對服務 B 發起HTTP/gRPC的調用。
-
Dapr使用在給定主機平台上運行的名稱解析組件發現服務B的位置。
-
Dapr 將消息轉發至服務B的Dapr Sidecar
注: Dapr Sidecar 之間的所有調用考慮到性能都優先使用 gRPC。 僅服務與 Dapr Sidecar之間的調用可以是 HTTP 或 gRPC
-
服務 B的 Dapr Sidecar 將請求轉發至服務 B 上的特定端點 (或方法) 。 服務 B 隨后運行其業務邏輯代碼。
-
服務 B 發送響應給服務 A。 響應將轉至服務 B 的Sidecar。
-
Dapr 將消息轉發至服務 A 的 Dapr Sidecar。
-
服務 A 接收響應。
服務調用特性:
由於調用經過Sidecar,Dapr 可以注入一些有其他行為:
- 失敗時自動重試調用。
- 通過相互 (mTLS) 身份驗證(包括自動證書滾動更新),在服務之間進行調用。
- 使用訪問控制策略控制客戶端可以執行的操作。
- 捕獲服務間所有調用的跟蹤和指標,以提供分布式調用鏈路追蹤與診斷。
二、調用示例:
1、創建兩個項目:DaprFrontEnd(前端示例)、DaprBackEnd(后端示例)
DaprFrontEnd項目:Razor項目用於展示從后端示例中調用結果內容
DaprBackEnd項目:Api項目用於獲取數據
2、DaprFrontEnd項目調整運行:
a) 修改綁定端口:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.UseUrls("http://0.0.0.0:8220"); });
b)使用Dapr cli 命令啟動項目:
//dapr命令 dapr run --dapr-http-port 3511 --app-port 5000 --app-id backend dotnet .\DaprBackEnd.dl //輸出結果 Starting Dapr with id backend. HTTP Port: 3511. gRPC Port: 1030 time="2021-10-24T19:00:48.5801709+08:00" level=info msg="starting Dapr Runtime -- version 1.4.3 -- commit a8ee30180e1183e2a2e4d00c283448af6d73d0d0" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.5831673+08:00" level=info msg="log level set to: info" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.5890733+08:00" level=info msg="metrics server started on :1033/" app_id=backend instance=Coder scope=dapr.metrics type=log ver=1.4.3 time="2021-10-24T19:00:48.6178099+08:00" level=info msg="standalone mode configured" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.6178272+08:00" level=info msg="app id: backend" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.6205808+08:00" level=info msg="mTLS is disabled. Skipping certificate request and tls validation" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.6835042+08:00" level=info msg="local service entry announced: backend -> 192.168.0.109:1038" app_id=backend instance=Coder scope=dapr.contrib type=log ver=1.4.3 time="2021-10-24T19:00:48.6835042+08:00" level=info msg="Initialized name resolution to mdns" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.6855038+08:00" level=info msg="loading components" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7225025+08:00" level=info msg="component loaded. name: pubsub, type: pubsub.redis/v1" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7228271+08:00" level=info msg="waiting for all outstanding components to be processed" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7310234+08:00" level=info msg="component loaded. name: statestore, type: state.redis/v1" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.732082+08:00" level=info msg="all outstanding components processed" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7374914+08:00" level=info msg="enabled gRPC tracing middleware" app_id=backend instance=Coder scope=dapr.runtime.grpc.api type=log ver=1.4.3 time="2021-10-24T19:00:48.7384898+08:00" level=info msg="enabled gRPC metrics middleware" app_id=backend instance=Coder scope=dapr.runtime.grpc.api type=log ver=1.4.3 time="2021-10-24T19:00:48.7434866+08:00" level=info msg="API gRPC server is running on port 1030" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7444869+08:00" level=info msg="enabled metrics http middleware" app_id=backend instance=Coder scope=dapr.runtime.http type=log ver=1.4.3 time="2021-10-24T19:00:48.7444869+08:00" level=info msg="enabled tracing http middleware" app_id=backend instance=Coder scope=dapr.runtime.http type=log ver=1.4.3 time="2021-10-24T19:00:48.7464879+08:00" level=info msg="http server is running on port 3511" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7464879+08:00" level=info msg="The request body size parameter is: 4" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7474861+08:00" level=info msg="enabled gRPC tracing middleware" app_id=backend instance=Coder scope=dapr.runtime.grpc.internal type=log ver=1.4.3 time="2021-10-24T19:00:48.7474861+08:00" level=info msg="enabled gRPC metrics middleware" app_id=backend instance=Coder scope=dapr.runtime.grpc.internal type=log ver=1.4.3 time="2021-10-24T19:00:48.7484875+08:00" level=info msg="internal gRPC server is running on port 1038" app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:00:48.7494884+08:00" level=info msg="application protocol: http. waiting on port 5000. This will block until the app is listening on that port." app_id=backend instance=Coder scope=dapr.runtime type=log ver=1.4.3 Updating metadata for app command: dotnet .\DaprBackEnd.dll You're up and running! Both Dapr and your app logs will appear here. == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Now listening on: http://0.0.0.0:8220 == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Application started. Press Ctrl+C to shut down. == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Hosting environment: Production == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Content root path: F:\coding\DaprMultiContainer\DaprBackEnd\bin\Debug\netcoreapp3.1
3、DaprBackEnd項目調整:
a) 修改端口
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.UseUrls("http://0.0.0.0:8230"); });
b) 添加Dapr.AspNetCore包
Install-Package Dapr.AspNetCore
c) 注入Dapr
public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddDapr(); services.AddRazorPages(); }
d) 調用DaprBackEnd 服務接口:
public class IndexModel : PageModel { private readonly DaprClient _daprClient; public IndexModel(DaprClient daprClient) { _daprClient = daprClient ?? throw new ArgumentNullException(nameof(daprClient)); } public async Task OnGet() { //方式一:使用HttpClient //using var httpClient = DaprClient.CreateInvokeHttpClient(); //var result = await httpClient.GetAsync("http://backend/WeatherForecast"); //var resultContent = await result.Content.ReadAsStringAsync(); //方式二:使用DaprClient var forecasts = await _daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>(HttpMethod.Get, "backend", "weatherforecast"); ViewData["WeatherForecastData"] = forecasts; //方式三:Grpc方式調用 //var forecastsGrpc = await _daprClient.InvokeMethodGrpcAsync<IEnumerable<WeatherForecast>>("backend", "weatherforecast"); //ViewData["WeatherForecastData"] = forecasts; } }
e) 通過Dapr CLI啟動DaprFrontEnd,指定sidecar端口為3501,默認為3500,指定app-port是8230,與DaprFrontEnd默認端口保持一致
//命令 dapr run --dapr-http-port 3501 --app-port 8230 --app-id frontend dotnet .\DaprFrontEnd.dll //輸出: Starting Dapr with id frontend. HTTP Port: 3501. gRPC Port: 29840 time="2021-10-24T19:31:25.1254888+08:00" level=info msg="starting Dapr Runtime -- version 1.4.3 -- commit a8ee30180e1183e2a2e4d00c283448af6d73d0d0" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.1284879+08:00" level=info msg="log level set to: info" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.1294861+08:00" level=info msg="metrics server started on :29841/" app_id=frontend instance=Coder scope=dapr.metrics type=log ver=1.4.3 time="2021-10-24T19:31:25.1458074+08:00" level=info msg="standalone mode configured" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.1458074+08:00" level=info msg="app id: frontend" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.1463505+08:00" level=info msg="mTLS is disabled. Skipping certificate request and tls validation" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.1837081+08:00" level=info msg="local service entry announced: frontend -> 192.168.0.109:29845" app_id=frontend instance=Coder scope=dapr.contrib type=log ver=1.4.3 time="2021-10-24T19:31:25.1837081+08:00" level=info msg="Initialized name resolution to mdns" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.184711+08:00" level=info msg="loading components" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2316059+08:00" level=info msg="component loaded. name: pubsub, type: pubsub.redis/v1" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2316059+08:00" level=info msg="waiting for all outstanding components to be processed" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2419333+08:00" level=info msg="component loaded. name: statestore, type: state.redis/v1" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2423401+08:00" level=info msg="all outstanding components processed" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2434172+08:00" level=info msg="enabled gRPC tracing middleware" app_id=frontend instance=Coder scope=dapr.runtime.grpc.api type=log ver=1.4.3 time="2021-10-24T19:31:25.2434172+08:00" level=info msg="enabled gRPC metrics middleware" app_id=frontend instance=Coder scope=dapr.runtime.grpc.api type=log ver=1.4.3 time="2021-10-24T19:31:25.2439954+08:00" level=info msg="API gRPC server is running on port 29840" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2439954+08:00" level=info msg="enabled metrics http middleware" app_id=frontend instance=Coder scope=dapr.runtime.http type=log ver=1.4.3 time="2021-10-24T19:31:25.2445399+08:00" level=info msg="enabled tracing http middleware" app_id=frontend instance=Coder scope=dapr.runtime.http type=log ver=1.4.3 time="2021-10-24T19:31:25.2450858+08:00" level=info msg="http server is running on port 3501" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2450858+08:00" level=info msg="The request body size parameter is: 4" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2456242+08:00" level=info msg="enabled gRPC tracing middleware" app_id=frontend instance=Coder scope=dapr.runtime.grpc.internal type=log ver=1.4.3 time="2021-10-24T19:31:25.2461714+08:00" level=info msg="enabled gRPC metrics middleware" app_id=frontend instance=Coder scope=dapr.runtime.grpc.internal type=log ver=1.4.3 time="2021-10-24T19:31:25.2467123+08:00" level=info msg="internal gRPC server is running on port 29845" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:25.2478066+08:00" level=info msg="application protocol: http. waiting on port 8230. This will block until the app is listening on that port." app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 Updating metadata for app command: dotnet .\DaprFrontEnd.dll You're up and running! Both Dapr and your app logs will appear here. == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Now listening on: http://0.0.0.0:8230 == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Application started. Press Ctrl+C to shut down. == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Hosting environment: Production == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Content root path: F:\coding\DaprMultiContainer\DaprFrontEnd\bin\Debug\netcoreapp3.1 time="2021-10-24T19:31:26.10297+08:00" level=info msg="application discovered on port 8230" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:26.2373056+08:00" level=info msg="application configuration loaded" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:26.2422307+08:00" level=info msg="actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s" app_id=frontend instance=Coder scope=dapr.runtime.actor type=log ver=1.4.3 time="2021-10-24T19:31:26.2677804+08:00" level=info msg="dapr initialized. Status: Running. Init Elapsed 1121.4194ms" app_id=frontend instance=Coder scope=dapr.runtime type=log ver=1.4.3 time="2021-10-24T19:31:26.5035983+08:00" level=info msg="placement tables updated, version: 0" app_id=frontend instance=Coder scope=dapr.runtime.actor.internal.placement type=log ver=1.4.3
查看dapr運行應用情況:
dapr list APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID backend 3511 1030 5000 dotnet .\DaprBack... 32m 2021-10-24 19:00.47 4900 frontend 3501 29840 8230 dotnet .\DaprFron... 1m 2021-10-24 19:31.24 20856
4、dapr cli 調用方式:
dapr invoke --app-id backend --verb "GET" --method weatherforecast
三、Dapr擴展功能:
1、鏈路跟蹤查看:自承載的方式下,Dapr默認啟動了zipkin容器,可以通過以下鏈接查看: http://localhost:9411/zipkin/
2、Dapr Dashboard:
a) 啟動 Dapr Dashboard:
dapr dashboard
b) 查看頁面:http://localhost:8080/
總結:
本篇對構建塊中服務調用進行了解,並在.NET Core 程序中進行了驗證。