gRPC(gRPC Remote Procedure Call)是由Google开发的高性能、跨语言的远程过程调用框架。它基于HTTP/2协议进行通信,支持多种编程语言,包括C++, C#, Java, Python等,使不同语言的应用程序可以通过远程调用相互通信。
Grpc类似于一种协议,遵循网络通讯,以.proto文件为协议模板进行客户端与服务端的交互开发,不限制客户端和服务端的代码语言风格.也可以在服务端与客户端使用不同的语言开发
// Protocol Buffers 语法版本 proto3 版本
syntax = "proto3";
// 定义了消息类型和服务的包名,类似于命名空间,用于避免命名冲突。
package Calculator;
// 定义了一个 gRPC 服务。在大括号中,您可以列出服务方法的定义。
service CalculatorService
{
// 定义了一个服务方法。rpc 表示定义一个远程过程调用(RPC)方法。MyMethod 是方法的名称,MyRequest 是输入参数类型,MyResponse 是输出参数类型。
rpc MyMethod (MyRequest) returns (MyResponse);
// 服务器流式方法
rpc ServerStreamingMethod(Request1) returns (stream Response1);
// 客户端流式方法
rpc ClientStreamingMethod(stream Request2) returns (Response2);
// 双向流式方法
rpc BidirectionalStreamingMethod(stream Request3) returns (stream Response3);
}
message MyRequest
{
repeated int32 num = 10;
}
message MyResponse
{
repeated string strs = 10;
}
message Request1
{
string Message = 1;
}
message Response1
{
string Message = 1;
}
message Request2
{
string Message = 1;
}
message Response2
{
string Message = 1;
}
message Request3
{
string Message = 1;
}
message Response3
{
string Message = 1;
}
syntax = "proto3";
package Calculator;
import "Protos/Calculator.proto";
service FirstService {
rpc MyMethod (MyRequest1) returns (MyResponse);
}
message MyRequest1
{
repeated int32 num = 10;
}
多个.proto文件间是可以调用的
.proto文件生成的文件位置 添加到客户端和服务端
using Calculator;
using Grpc.Core;
class CalculatorServiceImpl : CalculatorService.CalculatorServiceBase
{
/// <summary>
/// 发布响应
/// summary>
/// <param name="request">param>
/// <param name="context">param>
/// <returns>returns>
public override async Task<MyResponse> MyMethod(MyRequest request, ServerCallContext context)
{
MyResponse myResponse = new MyResponse();
foreach (int i in request.Num)
{
myResponse.Strs.Add(i.ToString());
}
return myResponse;
}
/// <summary>
/// 服务器流(Server Streaming)
/// summary>
/// <param name="request">param>
/// <param name="responseStream">param>
/// <param name="context">param>
/// <returns>returns>
public override async Task ServerStreamingMethod(Request1 request, IServerStreamWriter<Response1> responseStream, ServerCallContext context)
{
for (int i = 0; i < 10; i++)
{
Response1 response = new Response1 { Message = request.Message + $"Message {i}" };
await responseStream.WriteAsync(response);
await Task.Delay(500); // 模拟每秒发送一次数据
}
}
/// <summary>
/// 客户端流(Client Streaming)
/// summary>
/// <param name="requestStream">param>
/// <param name="context">param>
/// <returns>returns>
public override async Task<Response2> ClientStreamingMethod(IAsyncStreamReader<Request2> requestStream, ServerCallContext context)
{
string str = "";
while (requestStream.MoveNext().Result)
{
str += requestStream.Current.Message;
Console.WriteLine(requestStream.Current.Message);
}
return new Response2 { Message = str };
}
/// <summary>
/// 双向流(Bidirectional Streaming)
/// summary>
/// <param name="requestStream">param>
/// <param name="responseStream">param>
/// <param name="context">param>
/// <returns>returns>
public override async Task BidirectionalStreamingMethod(IAsyncStreamReader<Request3> requestStream, IServerStreamWriter<Response3> responseStream, ServerCallContext context)
{
while (requestStream.MoveNext().Result)
{
// 处理客户端发送的请求
Console.WriteLine(requestStream.Current.Message);
Response3 response = new Response3 { Message = requestStream.Current.Message + "abc" };
await responseStream.WriteAsync(response); // 发送响应
await Task.Delay(500);
}
}
}
class Program
{
static void Main(string[] args)
{
const int Port = 50051;
Server server = new Server
{
Services = { CalculatorService.BindService(new CalculatorServiceImpl()) },
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine("Calculator server listening on port " + Port);
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();
server.ShutdownAsync().Wait();
}
}
6.客户端代码
using Calculator;
using Grpc.Core;
class Program
{
const string ServerAddress = "localhost";
const int Port = 50051;
static void Main(string[] args)
{
MyMethod();
ServerStreamingMethod();
SelfIncreaseClient();
BidirectionalStreamingMethod();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
/// <summary>
/// 发布响应
/// summary>
private static void MyMethod()
{
Channel channel = new Channel($"{ServerAddress}:{Port}", ChannelCredentials.Insecure);
var client = new CalculatorService.CalculatorServiceClient(channel);
MyRequest myRequest = new MyRequest { Num = { 1, 2, 3, 4 } };
MyResponse myResponse = client.MyMethod(myRequest);
Console.WriteLine($"Result: {myResponse.Strs}");
channel.ShutdownAsync().Wait();
}
/// <summary>
/// 服务器流(Server Streaming)
/// summary>
private static void ServerStreamingMethod()
{
Channel channel = new Channel($"{ServerAddress}:{Port}", ChannelCredentials.Insecure);
var client = new CalculatorService.CalculatorServiceClient(channel);
Request1 request1 = new Request1 { Message = "ceshi" };
AsyncServerStreamingCall<Response1> response1s = client.ServerStreamingMethod(request1);
while (response1s.ResponseStream.MoveNext().Result)
{
Console.WriteLine(response1s.ResponseStream.Current.Message);
}
channel.ShutdownAsync().Wait();
}
/// <summary>
/// 客户端流(Client Streaming)
/// summary>
private static async void SelfIncreaseClient()
{
Channel channel = new Channel($"{ServerAddress}:{Port}", ChannelCredentials.Insecure);
var client = new CalculatorService.CalculatorServiceClient(channel);
var call = client.SelfIncreaseClient();
for (int i = 0; i < 10; i++)
{
await call.RequestStream.WriteAsync(new Request2() { Message = $"第{i}个" });
await Task.Delay(500);
}
await call.RequestStream.CompleteAsync();
Console.WriteLine($"Result: {call.ResponseAsync.Result.Message}");
channel.ShutdownAsync().Wait();
}
/// <summary>
/// 双向流(Bidirectional Streaming)
/// summary>
private static async void BidirectionalStreamingMethod()
{
Channel channel = new Channel($"{ServerAddress}:{Port}", ChannelCredentials.Insecure);
var client = new CalculatorService.CalculatorServiceClient(channel);
var call = client.BidirectionalStreamingMethod();
for (int i = 1; i <= 5; i++)
{
// 发送请求
await call.RequestStream.WriteAsync(new Request3 { Message = i.ToString() });
await Task.Delay(500);
}
await call.RequestStream.CompleteAsync();
while (call.ResponseStream.MoveNext().Result)
{
// 处理服务器发送的响应
Console.WriteLine(call.ResponseStream.Current.Message);
}
channel.ShutdownAsync().Wait();
}
}
2023/11/7