gRPC由于需要用工具生成代码实现,可开发性不是很高,在扩展这方面不是很友好
最近研究了下,进行了扩展,不需要额外的工具生成,直接使用默认Grpc.Tools生成的代理类即可
相关源码在文章底部
客户端目标:
查看gRPC生成的代码,可以看到Client实例化有有两个构造方法,以测试为例
/// Creates a new client for Greeter /// The channel to use to make remote calls. public GreeterClient(grpc::ChannelBase channel) : base(channel) { } /// Creates a new client for Greeter that uses a custom CallInvoker. /// The callInvoker to use to make remote calls. public GreeterClient(grpc::CallInvoker callInvoker) : base(callInvoker) { }
1.可传入一个ChannelBase实例化
2.可传入一个CallInvoker实例化
Channel可实现为
Channel CreateChannel(string address) {var channelOptions = new List() {new ChannelOption(ChannelOptions.MaxReceiveMessageLength, int.MaxValue),new ChannelOption(ChannelOptions.MaxSendMessageLength, int.MaxValue), };var channel = new Channel(address, ChannelCredentials.Insecure, channelOptions);return channel; }
在这里,可以从consul地址按服务名获取真实的服务地址,生成Channel
CallInvoker为一个抽象类,若要对方法执行过程干预,则需要重写这个方法,大致实现为
public class GRpcCallInvoker : CallInvoker {public readonly Channel Channel;public GRpcCallInvoker(Channel channel) { Channel = GrpcPreconditions.CheckNotNull(channel); }public override TResponse BlockingUnaryCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.BlockingUnaryCall(CreateCall(method, host, options), request); }public override AsyncUnaryCallAsyncUnaryCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.AsyncUnaryCall(CreateCall(method, host, options), request); }public override AsyncServerStreamingCallAsyncServerStreamingCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.AsyncServerStreamingCall(CreateCall(method, host, options), request); }public override AsyncClientStreamingCallAsyncClientStreamingCall(Methodmethod, string host, CallOptions options) {return Calls.AsyncClientStreamingCall(CreateCall(method, host, options)); }public override AsyncDuplexStreamingCallAsyncDuplexStreamingCall(Methodmethod, string host, CallOptions options) {return Calls.AsyncDuplexStreamingCall(CreateCall(method, host, options)); }protected virtual CallInvocationDetailsCreateCall(Methodmethod, string host, CallOptions options)where TRequest : class where TResponse : class{return new CallInvocationDetails(Channel, method, host, options); } }
这里可以传入上面创建的Channel,在CreateCall方法里,则可以对调用方法进行控制
完整实现为
public class GRpcCallInvoker : CallInvoker { GrpcClientOptions _options; IGrpcConnect _grpcConnect;public GRpcCallInvoker(IGrpcConnect grpcConnect) { _options = grpcConnect.GetOptions(); _grpcConnect = grpcConnect; }public override TResponse BlockingUnaryCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.BlockingUnaryCall(CreateCall(method, host, options), request); }public override AsyncUnaryCallAsyncUnaryCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.AsyncUnaryCall(CreateCall(method, host, options), request); }public override AsyncServerStreamingCallAsyncServerStreamingCall(Methodmethod, string host, CallOptions options, TRequest request) {return Calls.AsyncServerStreamingCall(CreateCall(method, host, options), request); }public override AsyncClientStreamingCallAsyncClientStreamingCall(Methodmethod, string host, CallOptions options) {return Calls.AsyncClientStreamingCall(CreateCall(method, host, options)); }public override AsyncDuplexStreamingCallAsyncDuplexStreamingCall(Methodmethod, string host, CallOptions options) {return Calls.AsyncDuplexStreamingCall(CreateCall(method, host, options)); }protected virtual CallInvocationDetailsCreateCall(Methodmethod, string host, CallOptions options)where TRequest : classwhere TResponse : class{var methodName = $"{method.ServiceName}.{method.Name}";var key = methodName.Substring(methodName.IndexOf(".") + 1).ToLower();var a = _options.MethodPolicies.TryGetValue(key, out PollyAttribute methodPollyAttr);if (!a) { _options.MethodPolicies.TryGetValue("", out methodPollyAttr); } CallOptions options2;//重写headerif (options.Headers != null) { options2 = options; }else{ options2 = new CallOptions(_grpcConnect.GetMetadata(), options.Deadline, options.CancellationToken); }var pollyData = PollyExtension.Invoke(methodPollyAttr, () =>{var callRes = new CallInvocationDetails(_grpcConnect.GetChannel(), method, host, options2);return new PollyExtension.PollyData<CallInvocationDetails>() { Data = callRes }; }, $"{methodName}");var response = pollyData.Data;if (!string.IsNullOrEmpty(pollyData.Error)) {throw new Exception(pollyData.Error); }return response;//return new CallInvocationDetails(Channel.Invoke(), method, host, options2); } }
其中传入了PollyAttribute,由PollyExtension.Invoke来完成Polly策略的实现,具体代码可在源码里找到
从上面代码可以看到,CallInvoker里可以传入了IGrpcConnect,由方法IGrpcConnect.GetChannel()获取Channel
Client实例化
.net FrameWork实现为
public T GetClient() {var a = instanceCache.TryGetValue(typeof(T), out object instance);if (!a) {var grpcCallInvoker = new GRpcCallInvoker(this); instance = System.Activator.CreateInstance(typeof(T), grpcCallInvoker); instanceCache.TryAdd(typeof(T), instance); }return (T)instance; }
core则简单点,直接注入实现
var client = provider.GetService();
服务端注册
和其它服务注册一样,填入正确的服务地址和名称就行了,但是在Check里得改改,gRPC的健康检查参数是不同的,并且在consul客户端里没有这个参数,得自已写
以下代码是我封装过的,可查看源码
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints =>{ endpoints.MapGrpcService(); endpoints.MapGrpcService(); endpoints.MapGet("/", async context =>{await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); }); });//注册服务var consulClient = new CRL.Core.ConsulClient.Consul("http://localhost:8500");var info = new CRL.Core.ConsulClient.ServiceRegistrationInfo { Address = "127.0.0.1", Name = "grpcServer", ID = "grpcServer1", Port = 50001, Tags = new[] { "v1" }, Check = new CRL.Core.ConsulClient.CheckRegistrationInfo() { GRPC = "127.0.0.1:50001", Interval = "10s", GRPCUseTLS = false, DeregisterCriticalServiceAfter = "90m"} }; consulClient.DeregisterService(info.ID);var a = consulClient.RegisterService(info); }
客户端完整封装代码为
core扩展方法,设置GrpcClientOptions来配置consul地址和Polly策略,直接注入了Client类型
同时添加了统一header传递,使整个服务都能用一个头发送请求,不用再在方法后面跟参数
public static class GrpcExtensions {public static void AddGrpcExtend(this IServiceCollection services, ActionsetupAction, params Assembly[] assemblies) { services.Configure(setupAction); services.AddSingleton(); services.AddScoped();foreach (var assembyle in assemblies) {var types = assembyle.GetTypes();foreach (var type in types) {if(typeof(ClientBase).IsAssignableFrom(type)) { services.AddSingleton(type); } } } } }
class Program {static IServiceProvider provider;static Program() {var builder = new ConfigurationBuilder();var configuration = builder.Build();var services = new ServiceCollection(); services.AddSingleton(configuration); services.AddOptions(); services.AddGrpcExtend(op =>{ op.Host = "127.0.0.1"; op.Port = 50001; op.UseConsulDiscover("http://localhost:8500", "grpcServer");//使用consul服务发现op.AddPolicy("Greeter.SayHello", new CRL.Core.Remoting.PollyAttribute() { RetryCount = 3 });//定义方法polly策略 }, System.Reflection.Assembly.GetExecutingAssembly()); provider = services.BuildServiceProvider(); }static void Main(string[] args) { //设置允许不安全的HTTP2支持AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);var grpcConnect = provider.GetService();//认证//https://www.cnblogs.com/stulzq/p/11897628.htmlvar token = "";var headers = new Metadata { { "Authorization", $"Bearer {token}" } }; grpcConnect.SetMetadata(headers); label1:var client = provider.GetService();var reply = client.SayHello(new HelloRequest { Name = "test" }); Console.WriteLine("Greeter 服务返回数据: " + reply.Message); Console.ReadLine();goto label1; } }
运行服务端,结果为
可以看到服务注册成功,状态检查也成功
运行客户端
客户端正确调用并返回了结果
项目源码:
https://github.com/CRL2020/CRL.NetStandard/tree/master/Grpc
除了gRPC实现了服务发现和Polly策略,本框架对API代理,动态API,RPC也一起实现了
API代理测试
https://github.com/CRL2020/CRL.NetStandard/tree/master/DynamicWebApi/ApiProxyTest
动态API测试
https://github.com/CRL2020/CRL.NetStandard/tree/master/DynamicWebApi/DynamicWebApiClient
RCP测试
https://github.com/CRL2020/CRL.NetStandard/tree/master/RPC/RPCClient
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4