/// <summary> /// Determines whether the specified endpoint is connected to the specified service /// that responds with health status 'Serving' . /// </summary> /// <param name="healthClient"></param> /// <param name="serviceName"></param> /// <returns>StatusCode.OK if the service is healthy.</returns> public static async Task <StatusCode> IsServingAsync( [NotNull] Health.HealthClient healthClient, [NotNull] string serviceName) { StatusCode statusCode = StatusCode.Unknown; try { HealthCheckResponse healthResponse = await healthClient.CheckAsync(new HealthCheckRequest() { Service = serviceName }); statusCode = healthResponse.Status == HealthCheckResponse.Types.ServingStatus.Serving ? StatusCode.OK : StatusCode.ResourceExhausted; } catch (RpcException rpcException) { _msg.Debug($"Error checking health of service {serviceName}", rpcException); statusCode = rpcException.StatusCode; } catch (Exception e) { _msg.Debug($"Error checking health of service {serviceName}", e); return(statusCode); } return(statusCode); }
static async Task Main(string[] args) { AppContext.SetSwitch( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); Channel channel = new Channel("healthchecker:5000", ChannelCredentials.Insecure); Health.HealthClient client = new Health.HealthClient(channel); while (true) { try { var watch = System.Diagnostics.Stopwatch.StartNew(); HealthCheckResponse response = await client.CheckAsync(new HealthCheckRequest() { Service = "healthchecker" }); watch.Stop(); Console.WriteLine("gRPC call to Healthchecker Server took " + watch.ElapsedMilliseconds + "ms"); response.Status.ToString(); } catch (Exception ex) { Console.WriteLine(ex.Message + " " + ex.StackTrace); } System.Threading.Thread.Sleep(1000); } }
private static async Task Main(string[] args) { Channel channel = new Channel("localhost:5000", ChannelCredentials.Insecure); var healthClient = new Health.HealthClient(channel); var healthResponse = await healthClient.CheckAsync(new HealthCheckRequest()); Console.WriteLine($"Server is: {healthResponse.Status}"); healthResponse = await healthClient.CheckAsync(new HealthCheckRequest { Service = CustomerService.Descriptor.Name }); Console.WriteLine($"CustomerService is: {healthResponse.Status}"); var customerClient = new CustomerService.CustomerServiceClient(channel); var customerResponse = await customerClient.GetCustomerByIdAsync( new GetCustomerByIdRequest { Id = 1 }, new CallOptions(new Metadata { { "correlation-id", Guid.NewGuid().ToString() } })).ResponseAsync.ConfigureAwait(false); Console.WriteLine($"Customer: {customerResponse.Customer.Id} retrieved."); customerResponse = await customerClient.GetCustomerById2Async( new GetCustomerByIdRequest { Id = 1 }, new CallOptions(new Metadata { { "correlation-id", Guid.NewGuid().ToString() } })).ResponseAsync.ConfigureAwait(false); Console.WriteLine($"Customer: {customerResponse.Customer.Id} retrieved."); var customerResponse2 = customerClient.DeleteCustomerById(new DeleteCustomerByIdRequest { Id = 1 }); var customerResponse3 = customerClient.ListCustomers(new CustomerSearch { FirstName = "test" }); while (await customerResponse3.ResponseStream.MoveNext(CancellationToken.None)) { var response = customerResponse3.ResponseStream.Current; } await channel.ShutdownAsync().ConfigureAwait(false); Console.ReadKey(); }
private static async Task Main() { using var channel = GrpcChannel.ForAddress("https://localhost:5005"); var healthClient = new Health.HealthClient(channel); var health = await healthClient.CheckAsync(new HealthCheckRequest { Service = "Weather" }); Console.WriteLine($"Health Status: {health.Status}"); Console.WriteLine("Press a key to exit"); Console.ReadKey(); }
private static async Task Main(string[] args) { GrpcChannel channel = GrpcChannel.ForAddress("https://localhost:45679"); var httpsClientTicker = new Ticker.TickerClient(channel); GrpcChannel authChannel = GrpcChannel.ForAddress("https://localhost:56790"); var httpsClient = new Auth.AuthClient(authChannel); var interceptorHttps = channel.Intercept(new HeaderLoggerInterceptor()); var healthChecker = new Health.HealthClient(interceptorHttps); string serviceName = "Ticker"; var result = await healthChecker.CheckAsync(new HealthCheckRequest { Service = serviceName }); Console.WriteLine(result?.Status); /* var reply = await httpsClient.LogInAsync(new LogInRequest { Username = "******", Password = "******" }); * var replyUser = await httpsClient.LogInAsync(new LogInRequest { Username = "******", Password = "******" }); * * var tokenAdmin = * await Authenticate(reply.Role); * * var tokenUser = await Authenticate(replyUser.Role); * * * Console.WriteLine(tokenAdmin); * Console.WriteLine(tokenUser); * * await ClientStreaming(httpsClientTicker, 1, tokenUser); * * * * /*await UpdateTickHandling(httpsClient, * new TickToAdd * { * InstrumentId = 1, * Close = (DecimalValue)5.6234m, * Open = (DecimalValue)5.6225m, * High = (DecimalValue)5.6238m, * Low = (DecimalValue)5.6224m, * Symbol = 2 * }); * await GetTicksForQuota(httpsClient, 1);*/ //await ClientStreaming(httpsClient, 1); Console.WriteLine("Press any key to close app..."); Console.ReadLine(); }
static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Health.HealthClient(channel); Console.WriteLine("Watching health status"); Console.WriteLine("Press any key to exit..."); var response = await client.CheckAsync(new HealthCheckRequest { Service = "T1" }); Console.WriteLine($"check call result Service is StatusCode:{response.Status}"); var cts = new CancellationTokenSource(); var call = client.Watch(new HealthCheckRequest { Service = "T1" }, cancellationToken: cts.Token); var watchTask = Task.Run(async() => { try { await foreach (var message in call.ResponseStream.ReadAllAsync()) { Console.WriteLine($"{DateTime.Now}: Service is {message.Status}"); } } catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled) { Console.WriteLine("Health is cancel."); } }); Console.ReadKey(); Console.WriteLine("watch cancel Finished"); cts.Cancel(); await watchTask; }
public async Task GrpcService_Exec_Test() { var locator = new GrpcLocator(); Console.WriteLine("Testing in {0}", locator.ServiceDomain); GrpcService svc = await locator.Locate("alpha-users", ChannelCredentials.Insecure); Assert.IsNotNull(svc); HealthCheckResponse reply = await svc.Execute(async channel => { var client = new Health.HealthClient(channel); return(await client.CheckAsync(new HealthCheckRequest { Service = "users" })); }, TimeSpan.FromSeconds(2)); Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, reply.Status); }
public static Task <HealthCheckResponse> HealthCheck(HealthCheckRequest request) { return(_client.CheckAsync(request).ResponseAsync); }
static async Task RunAsync(string endpoint, string prefix) { // The port number(5001) must match the port of the gRPC server. var handler = new SocketsHttpHandler(); if (endpoint.StartsWith("https://")) { handler.SslOptions = new SslClientAuthenticationOptions { RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true, }; } using var channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions { HttpHandler = handler, }); // health var healthClient = new Health.HealthClient(channel); var health = await healthClient.CheckAsync(new HealthCheckRequest { Service = "", }); Console.WriteLine($"Health Status: {health.Status}"); // unary var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = $"{prefix} GreeterClient" }); Console.WriteLine("Greeting: " + reply.Message); // duplex var requestHeaders = new Metadata { { "x-host-port", "10-0-0-10" }, }; using var streaming = client.StreamingBothWays(requestHeaders); var readTask = Task.Run(async() => { await foreach (var response in streaming.ResponseStream.ReadAllAsync()) { Console.WriteLine(response.Message); } }); var i = 0; while (i++ < 100) { await streaming.RequestStream.WriteAsync(new HelloRequest { Name = $"{prefix} {i}", }); await Task.Delay(TimeSpan.FromMilliseconds(10)); } await streaming.RequestStream.CompleteAsync(); await readTask; }