GRPC
一、介绍
gRPC,其实就是RPC框架的一种,前面带了一个g,代表是RPC中的大哥,龙头老大的意思,另外g也有global的意思,意思是全球化比较fashion,是一个高性能、开源和通用的 RPC 框架,基于ProtoBuf(Protocol Buffers) 序列化协议开发,且支持众多开发语言。面向服务端和移动端,基于 HTTP/2 设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。
二、使用:总共有四种模式
三、 相关使用配置
1. 新建一个GRPC的项目
2. 新建一个控制台程序,并且将GRPC项目中的Protos拷贝过来控制台程序中
3. 相关代码:简单模式
using (var channel = GrpcChannel.ForAddress("https://localhost:5001")) { var reply = await client.SayHelloAsync(new HelloRequestMath() { Name = "clay" }); Console.WriteLine("服务端说: " + reply.Message); }
四、各种调用模式总结:
private static async Task TestHello() { using (var channel = GrpcChannel.ForAddress("https://localhost:5001")) { #region MyRegion //var client2 = new Greeter.GreeterClient(channel); //var reply2 = await client2.SayHelloAsync(new HelloRequest { Name = "Clay老师" }); //Console.WriteLine("服务端说: " + reply2.Message); ////client.SayHello(new HelloRequest { Name = "Clay" }); #endregion var client = new CustomMath.CustomMathClient(channel); #region 自定义 //Console.WriteLine("************简单调用************"); //{ // var reply = await client.SayHelloAsync(new HelloRequestMath() { Name = "clay" }); // Console.WriteLine("服务端说: " + reply.Message); //} #region 客户端流 //Console.WriteLine("**************************客户端流*****************************"); //{ // var bathCat = client.SelfIncreaseClient(); // for (int i = 0; i < 10; i++) // { // await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) }); // await Task.Delay(100); // Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}"); // } // Console.WriteLine("**********************************"); // //发送完毕 // await bathCat.RequestStream.CompleteAsync(); // Console.WriteLine("客户端已发送完10个id"); // Console.WriteLine("接收结果:"); // foreach (var item in bathCat.ResponseAsync.Result.Number) // { // Console.WriteLine($"This is {item} Result"); // } // Console.WriteLine("**********************************"); //} #endregion #region 服务端流 //Console.WriteLine("**************************服务端流*****************************"); //{ // IntArrayModel intArrayModel = new IntArrayModel(); // for (int i = 0; i < 15; i++) // { // intArrayModel.Number.Add(i);//Number不能直接赋值, // } // CancellationTokenSource cts = new CancellationTokenSource(); // cts.CancelAfter(TimeSpan.FromSeconds(2.5)); //指定在2.5s后进行取消操作 // var bathCat = client.SelfIncreaseServer(intArrayModel, cancellationToken: cts.Token); // //var bathCat = client.SelfIncreaseServer(intArrayModel);//不带取消 // var bathCatRespTask = Task.Run(async () => // { // await foreach (var resp in bathCat.ResponseStream.ReadAllAsync()) // { // Console.WriteLine(resp.Message); // Console.WriteLine($"This is Response {Thread.CurrentThread.ManagedThreadId}"); // Console.WriteLine("**********************************"); // } // }); // Console.WriteLine("客户端已发送完10个id"); // //开始接收响应 // await bathCatRespTask; //} #endregion #region 双流 Console.WriteLine("**************************这是双流呀*****************************"); { var bathCat = client.SelfIncreaseDouble(); var bathCatRespTask = Task.Run(async () => { await foreach (var resp in bathCat.ResponseStream.ReadAllAsync()) { Console.WriteLine(resp.Message); Console.WriteLine($"This is Response {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine("**********************************"); } }); for (int i = 0; i < 10; i++) { await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) }); await Task.Delay(100); Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine("**********************************"); } //发送完毕 await bathCat.RequestStream.CompleteAsync(); Console.WriteLine("客户端已发送完10个id"); Console.WriteLine("接收结果:"); //开始接收响应 await bathCatRespTask; } } #endregion #endregion #region 取消 //Console.WriteLine("**************************双流+取消*****************************"); //{ // CancellationTokenSource cts = new CancellationTokenSource(); // cts.CancelAfter(TimeSpan.FromSeconds(2.5)); //指定在2.5s后进行取消操作 // var bathCat = client.SelfIncreaseDouble(cancellationToken: cts.Token); // var bathCatRespTask = Task.Run(async () => // { // await foreach (var resp in bathCat.ResponseStream.ReadAllAsync()) // { // Console.WriteLine(resp.Message); // Console.WriteLine($"This is Response {Thread.CurrentThread.ManagedThreadId}"); // Console.WriteLine("**********************************"); // } // }); // for (int i = 0; i < 10; i++) // { // await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) }); // await Task.Delay(100); // Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}"); // Console.WriteLine("**********************************"); // } // //发送完毕 // await bathCat.RequestStream.CompleteAsync(); // Console.WriteLine("客户端已发送完10个id"); // Console.WriteLine("接收结果:"); // //开始接收响应 // await bathCatRespTask; //} #endregion }
谢谢学习!!!