上一篇对Dapr进行了了解,并搭建了Dapr环境。接下来就对Dapr的各个构建块类型的了解、应用实际案例。
在许多具有多个需要相互通信的服务的环境中,都会面临着很多问题。 如:
维护其他服务的地址。如何安全地调用服务。在发生短暂的 暂时性错误 时如何处理重试。分布式应用程序调用链路追踪。服务调用构建块通过使用 Dapr 挎斗作为服务的 反向代理 来解决这些难题。
服务调用就是应用程序可以使用 gRPC 或 HTTP 这样的标准协议来发现并可靠地与其他应用程序通信。
Dapr的服务调用如何工作的总览图,如下:
由于调用经过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();
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.13、DaprBackEnd项目调整:
a) 修改端口
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
webBuilder.UseUrls("http://0.0.0.0:8230");
}); b) 添加Dapr.AspNetCore包
Install-Package Dapr.AspNetCorec) 注入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>(HttpMethod.Get, "backend", "weatherforecast");
ViewData["WeatherForecastData"] = forecasts;
//方式三:Grpc方式调用
//var forecastsGrpc = await _daprClient.InvokeMethodGrpcAsync>("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 208564、dapr cli 调用方式:
dapr invoke --app-id backend --verb "GET" --method weatherforecast1、链路跟踪查看:自承载的方式下,Dapr默认启动了zipkin容器,可以通过以下链接查看: http://localhost:9411/zipkin/
2、Dapr Dashboard:
a) 启动 Dapr Dashboard:
dapr dashboardb) 查看页面:http://localhost:8080/
本篇对构建块中服务调用进行了解,并在.NET Core 程序中进行了验证。
yua原文地址:https://www.cnblogs.com/cwsheng/p/15452074.html
版权声明:我们致力于保护作者版权,注重分享,被刊用文章【pr代理(Dapr)】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;
工作时间:8:00-18:00
客服电话
电子邮件
beimuxi@protonmail.com
扫码二维码
获取最新动态
