public static IApplicationBuilder UseGrpcRegisterService(this IApplicationBuilder app, IConfiguration configuration) { RpcServiceDiscoveryOptions serviceDiscoveryOption = new RpcServiceDiscoveryOptions(); configuration.GetSection("ServiceDiscovery").Bind(serviceDiscoveryOption); app.UseGrpcRegisterService(serviceDiscoveryOption); return(app); }
public static IApplicationBuilder UseGrpcRegisterService(this IApplicationBuilder app, RpcServiceDiscoveryOptions serviceDiscoveryOption) { var applicationLifetime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>() ?? throw new ArgumentException("Missing Dependency", nameof(IApplicationLifetime)); var serviceDiscovery = app.ApplicationServices.GetRequiredService <IServiceDiscovery>() ?? throw new ArgumentException("Missing Dependency", nameof(IServiceDiscovery)); if (string.IsNullOrEmpty(serviceDiscoveryOption.ServiceName)) { throw new ArgumentException("service name must be configure", nameof(serviceDiscoveryOption.ServiceName)); } IEnumerable <Uri> addresses = null; if (serviceDiscoveryOption.Endpoints != null && serviceDiscoveryOption.Endpoints.Length > 0) { addresses = serviceDiscoveryOption.Endpoints.Select(p => new Uri(p)); } else { var features = app.Properties["server.Features"] as FeatureCollection; addresses = features.Get <IServerAddressesFeature>().Addresses.Select(p => new Uri(p)).ToArray(); } // 以默认第一个地址开启rpc服务 var grpcServer = InitializeGrpcServer(addresses.FirstOrDefault()); foreach (var address in addresses) { UriBuilder myUri = new UriBuilder(address.Scheme, address.Host, address.Port); var serviceID = GetRpcServiceId(serviceDiscoveryOption.ServiceName, myUri.Uri); Uri healthCheck = null; if (!string.IsNullOrEmpty(serviceDiscoveryOption.HealthCheckTemplate)) { healthCheck = new Uri(myUri.Uri, serviceDiscoveryOption.HealthCheckTemplate); } var registryInformation = serviceDiscovery.RegisterServiceAsync(serviceDiscoveryOption.ServiceName, serviceDiscoveryOption.Version, myUri.Uri, healthCheckUri: healthCheck, tags: new[] { $"urlprefix-/{serviceDiscoveryOption.ServiceName}" }).Result; applicationLifetime.ApplicationStopping.Register(() => { try { grpcServer.ShutdownAsync().Wait(); } catch (Exception ex) { Console.WriteLine($"grpcServer had shutown {ex}"); } serviceDiscovery.DeregisterServiceAsync(registryInformation.Id); }); } return(app); }
public static IApplicationBuilder UseGrpcRegisterService(this IApplicationBuilder app, RpcServiceDiscoveryOptions serviceDiscoveryOption) { var applicationLifetime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>() ?? throw new ArgumentException("Missing Dependency", nameof(IApplicationLifetime)); var serviceDiscovery = app.ApplicationServices.GetRequiredService <IServiceDiscovery>() ?? throw new ArgumentException("Missing Dependency", nameof(IServiceDiscovery)); if (string.IsNullOrEmpty(serviceDiscoveryOption.ServiceName)) { throw new ArgumentException("service name must be configure", nameof(serviceDiscoveryOption.ServiceName)); } if (serviceDiscoveryOption.RpcEndpoint == null) { throw new ArgumentException("RpcEndpoint must be configure", nameof(serviceDiscoveryOption.RpcEndpoint)); } // 开启rpc服务 var grpcServer = InitializeGrpcServer(serviceDiscoveryOption.RpcEndpoint); Uri healthCheck = null; if (!string.IsNullOrEmpty(serviceDiscoveryOption.HealthCheckTemplate)) { healthCheck = new Uri(serviceDiscoveryOption.HealthCheckTemplate); } Uri address = null; if (!string.IsNullOrWhiteSpace(serviceDiscoveryOption.Endpoint)) { address = new Uri(serviceDiscoveryOption.Endpoint); } else { var features = app.Properties["server.Features"] as FeatureCollection; address = features.Get <IServerAddressesFeature>()?.Addresses?.Select(p => new Uri(p))?.FirstOrDefault(); } if (address != null) { var registryInformation = serviceDiscovery.RegisterServiceAsync(serviceDiscoveryOption.ServiceName, serviceDiscoveryOption.Version, address, healthCheckUri: healthCheck, tags: new[] { "GRPC", $"urlprefix-/{serviceDiscoveryOption.ServiceName}" }).Result; applicationLifetime.ApplicationStopping.Register(() => { try { grpcServer.ShutdownAsync().Wait(); } catch (Exception ex) { Console.WriteLine($"grpcServer had shutown {ex}"); } serviceDiscovery.DeregisterServiceAsync(registryInformation.Id); }); } return(app); }