其实我是棵树 发表于 2024-3-1 16:53:40

.NET Core(.NET6)中gRPC使用

目录

[*]一、简介
[*]二、创建gRPC服务端

[*]1.创建gRPC项目
[*]2.编写自己的服务

[*]三、创建gRPC客户端

[*]1.创建客户端项目
[*]2.grPC服务https的调用
[*]3.gRPC内网http调用
[*]4.IOC注入的方式调用gRPC

[*]四、webapi中加入gRPC
 
回到顶部一、简介

简单解析一下gRPC,gRPC 是一个由Google开源的,跨语言的,高性能的远程过程调用(RPC)框架。
特点:

[*]跨语言
[*]内容protobuf格式(比json体积小),网络传输快
[*]使用HTTP/2进行传输
适合高性能轻量的微服务,一般对外的接口用restful api,内部服务的调用用gRPC。gRPC是一个分布式服务框架,和以前的WebService,WCF类似。
回到顶部二、创建gRPC服务端

1.创建gRPC项目

新建一个gRPC模板的项目

 
 
 
 
 
 特别的地方就这里4点。
1.基于http2来通信。
2.proto协议文件,greet.proto是项目默认给我们的一个hello world的示例。它会根据协议自动生成需要的类。

 
 
3.服务类,Greeter.GreeterBase来自于2中的proto文件自动生成的类,生成的类在\obj\Debug\net6.0\Protos 目录下。

 
 自动生成的类:

 
 4.Program.cs添加了gRPC服务,和把GreeterService注册到管道中。
2.编写自己的服务

怎么样写自己的调用服务呢?
 1.创建proto文件
参照上面的示例创建自己的Proto文件

 
 
 
 
 代码:
syntax = "proto3";

option csharp_namespace = "GrpcDemo.Service";

package order;

// 订单服务定义
service Order {
// 创建订单
rpc CreateOrder (CreateRequest) returns (CreateResult);
//查询订单
rpc QueryOrder (QueryRequest) returns (QueryResult);
}

//创建订单请求参数
message CreateRequest {
string orderNo = 1;
string orderName=2;
double price=3;
}

//创建订单返回结果
message CreateResult {
bool result = 1;
string message=2;
}

//查询订单请求参数
message QueryRequest{
    string id=1;
}
//查询订单返回结果
message QueryResult{
    string id=1;
    string orderNo=2;
    string orderName=3;
    double price=4;

生成一下就能看到对应的类已经生成了。

 
 2.实现定义的服务
 创建OrderService.cs
public class OrderService:Order.OrderBase
    {
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private readonly ILogger<GreeterService> _logger;
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public OrderService(ILogger<GreeterService> logger)
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _logger = logger;
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <summary>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// 创建订单
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// </summary>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="request"></param>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="context"></param>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <returns></returns>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public override Task<CreateResult> CreateOrder(CreateRequest request, ServerCallContext context)
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //报存数据库 todo

<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new CreateResult
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Result=true,
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Message="订单创建成功"
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <summary>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// 查询订单
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// </summary>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="request"></param>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="context"></param>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <returns></returns>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public override Task<QueryResult> QueryOrder(QueryRequest request, ServerCallContext context)
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //查询数据库 //todo

<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new QueryResult
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Id = request.Id,
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱",
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1288
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
    }继承的Order.OrderBase 是上面的proto文件生成的,然后实现了proto里面定义的两个服务。
然后在program.cs里面把服务注入管道。

 
 到这里服务端就完成了,就可以启动服务端了。

 
 浏览器访问不了,要通过gRPC客户端才能访问,下面就建一个gRPC客户端。
回到顶部三、创建gRPC客户端

1.创建客户端项目

1.1、这里创建一个控制台程序。
1.2、然后添加Nuget包
Google.Protobuf
Grpc.Net.Client
Grpc.Tools<br>Grpc.Net.clientFactory1.3、把服务端的proto文件拷贝过来

 
 1.4、编辑项目文件,把proto里面的内容替换掉,默认是服务端的配置

 
 
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />生成的时候,客户端也生成了对应proto的类

 
2.grPC服务https的调用

因为服务端提供了http和https的端口,这里先调用https的
创建GrpcRequestTest.cs类
///   /// gRPC请求测试    ///   public class GrpcRequestTest    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public void CreateOrder()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //常规使用,https<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    string url = "https://localhost:7246";<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    using(var channel=GrpcChannel.ForAddress(url))<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var client = new Order.OrderClient(channel);<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var reply = client.CreateOrder(new CreateRequest()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱22款",<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1688<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />});<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    }<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }结果:

3.gRPC内网http调用

      public void CreateOrder()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />   //使用http<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    string url = "http://localhost:5246";<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    using(var channel=GrpcChannel.ForAddress(url))<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var client = new Order.OrderClient(channel);<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var reply = client.CreateOrder(new CreateRequest()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱22款",<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1688<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />});<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    }<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}比起https,前面多了一行。结果:

 
4.IOC注入的方式调用gRPC

4.1、program.cs里面注入gRPCClient
// See https://aka.ms/new-console-template for more information
using GrpcDemo.Client;
using GrpcDemo.Service;
using Microsoft.Extensions.DependencyInjection;

Console.WriteLine("Hello, World!");

IServiceCollection services = new ServiceCollection();
services.AddTransient<GrpcRequestTest>();

#region gRPC Client注册
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
services.AddGrpcClient<Order.OrderClient>(options =>
{
    options.Address = new Uri("http://localhost:5246");
}).ConfigureChannel(grpcOptions =>
{
    //可以完成各种配置,比如token
});
#endregion

//构建容器
IServiceProvider serviceProvider = services.BuildServiceProvider();
//解析grpc请求测试
var grpcRequestTest = serviceProvider.GetService<GrpcRequestTest>();
//执行
grpcRequestTest.CreateOrder();grpcRequestTest里代码:
///   /// gRPC请求测试    ///   public class GrpcRequestTest    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private Order.OrderClient _orderClient;<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public GrpcRequestTest(Order.OrderClient orderClient)<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _orderClient = orderClient;<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public void CreateOrder()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    var reply = _orderClient.CreateOrder(new CreateRequest()<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderName = "冰箱22款",<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Price = 1688<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }结果:

 
回到顶部四、webapi中加入gRPC

通常我们的服务有对外提供对外接口,又要对内提供gRPC服务,那怎么做呢,下面在webapi中加入gRPC服务
1.创建asp.net core mvc项目
2.安装nuget包
Grpc.AspNetCore3.添加protebuf文件
把上面的proto文件复制过来

 
 
4.添加Service

 
 
public class OrderService : Order.OrderBase    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private readonly ILogger _logger;<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public OrderService(ILogger logger)<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _logger = logger;<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> /// 创建订单<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> public override Task CreateOrder(CreateRequest request, ServerCallContext context)<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //报存数据库 todo<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new CreateResult<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Result = true,<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Message = "订单创建成功"<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> /// 查询订单<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> public override Task QueryOrder(QueryRequest request, ServerCallContext context)<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //查询数据库 //todo<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new QueryResult<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderInfo = new OrderInfo<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Id = request.Id,<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyyMMddHHmmss"),<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱",<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1288<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" /><Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
      <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }5.注册gRPC服务
在Program.cs文件中

 
到这里,就把gRPC加入到webapi里面了。 
6.验证
启动asp.net core mvc程序
 

 
 
浏览器能访问,证明 restful api是没问题的。
然后再用上面的客户端访问一下gRPC的服务。

 
 
 结果:
 
 这样就成功对外提供api接口和对内提供gRPC服务了。
 
源码地址:https://github.com/weixiaolong325/GrpcDemo.Service
 

来源:https://www.cnblogs.com/lijiasnong/p/18047005
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: .NET Core(.NET6)中gRPC使用