Dapr 运用之集成 Asp.Net Core Grpc 调用篇 (2)

创建 DaprClientService.cs 文件,此文件用于终结点路由,内容为

public class DaprClientService : DaprClient.DaprClientBase { private readonly ProductListService _productListService; /// <summary> /// Initializes a new instance of the <see cref="ProductService" /> class. /// </summary> /// <param></param> public DaprClientService(ProductListService productListService) { _productListService = productListService; } public override async Task<Any> OnInvoke(InvokeEnvelope request, ServerCallContext context) { switch (request.Method) { case "GetAllProducts": ProductListRequest productListRequest = ProductListRequest.Parser.ParseFrom(request.Data.Value); ProductList.V1.ProductList productsList = await _productListService.GetAllProducts(productListRequest, context); return Any.Pack(productsList); } return null; } } 说明

使用构造器注入已定义好的 ProductListService

InvokeEnvelope 中的 Method 用于路由数据

使用 ProductListRequest.Parser.ParseFrom 转换请求构造

使用 Any.Pack() 打包需要返回的数据

运行 productService

dapr run --app-id productService --app-port 5001 --protocol grpc dotnet run

小结
至此,ProductService 服务完成。此时 ProductService.Api.csproj Protobuf 内容为

<ItemGroup> <Protobuf Include="Protos\daprclient.proto" GrpcServices="Server" /> <Protobuf Include="Protos\productList.proto" GrpcServices="Server" /> </ItemGroup> 改造 StorageService 服务以完成 Dapr GRPC 服务调用

添加 productList.proto 文件,内容同 ProductService 中的 productList.proto

添加 dapr.proto 文件,此文件也为官方提供,内容为

syntax = "proto3"; package dapr; import "google/protobuf/any.proto"; import "google/protobuf/empty.proto"; import "google/protobuf/duration.proto"; option java_outer_classname = "DaprProtos"; option java_package = "io.dapr"; option csharp_namespace = "Dapr.Client.Grpc"; // Dapr definitions service Dapr { rpc PublishEvent(PublishEventEnvelope) returns (google.protobuf.Empty) {} rpc InvokeService(InvokeServiceEnvelope) returns (InvokeServiceResponseEnvelope) {} rpc InvokeBinding(InvokeBindingEnvelope) returns (google.protobuf.Empty) {} rpc GetState(GetStateEnvelope) returns (GetStateResponseEnvelope) {} rpc SaveState(SaveStateEnvelope) returns (google.protobuf.Empty) {} rpc DeleteState(DeleteStateEnvelope) returns (google.protobuf.Empty) {} } message InvokeServiceResponseEnvelope { google.protobuf.Any data = 1; map<string,string> metadata = 2; } message DeleteStateEnvelope { string key = 1; string etag = 2; StateOptions options = 3; } message SaveStateEnvelope { repeated StateRequest requests = 1; } message GetStateEnvelope { string key = 1; string consistency = 2; } message GetStateResponseEnvelope { google.protobuf.Any data = 1; string etag = 2; } message InvokeBindingEnvelope { string name = 1; google.protobuf.Any data = 2; map<string,string> metadata = 3; } message InvokeServiceEnvelope { string id = 1; string method = 2; google.protobuf.Any data = 3; map<string,string> metadata = 4; } message PublishEventEnvelope { string topic = 1; google.protobuf.Any data = 2; } message State { string key = 1; google.protobuf.Any value = 2; string etag = 3; map<string,string> metadata = 4; StateOptions options = 5; } message StateOptions { string concurrency = 1; string consistency = 2; RetryPolicy retryPolicy = 3; } message RetryPolicy { int32 threshold = 1; string pattern = 2; google.protobuf.Duration interval = 3; } message StateRequest { string key = 1; google.protobuf.Any value = 2; string etag = 3; map<string,string> metadata = 4; StateRequestOptions options = 5; } message StateRequestOptions { string concurrency = 1; string consistency = 2; StateRetryPolicy retryPolicy = 3; } message StateRetryPolicy { int32 threshold = 1; string pattern = 2; google.protobuf.Duration interval = 3; } 说明

此文件提供6个 GRPC 服务,此文介绍的函数为 InvokeService()

请求构造为 InvokeServiceEnvelope

id 请求的服务的 --app-id ,比如 productService

method 请求的方法

data 请求函数的签名

metadata 元数据键值对

修改 StorageController 中的 InitialStorage() 函数为

/// <summary> /// 初始化仓库. /// </summary> /// <returns>是否成功.</returns> [HttpGet("InitialStorage")] public async Task<bool> InitialStorage() { string defaultPort = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT") ?? "5001"; // Set correct switch to make insecure gRPC service calls. This switch must be set before creating the GrpcChannel. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); // Create Client string daprUri = $"http://127.0.0.1:{defaultPort}"; GrpcChannel channel = GrpcChannel.ForAddress(daprUri); var client = new Dapr.Client.Grpc.Dapr.DaprClient(channel); InvokeServiceResponseEnvelope result = await client.InvokeServiceAsync(new InvokeServiceEnvelope { Method = "GetAllProducts", Id = "productService", Data = Any.Pack(new ProductListRequest()) }); ProductList.V1.ProductList productResult = ProductList.V1.ProductList.Parser.ParseFrom(result.Data.Value); var random = new Random(); foreach (Product item in productResult.Results) { _storageContext.Storage.Add(new Storage { ProductID = Guid.Parse(item.ID), Amount = random.Next(1, 1000) }); } return true; }

启动 StorageService

dapr run --app-id storageService --app-port 5003 dotnet run

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgfjs.html