netcore grpc

news/2024/10/19 17:10:27

netcore grpc

一、solution

  1. 创建空解决方案
     > dotnet new sln -n Apricot.Grpc
    

二、Grpc.Server

  1. 创建Apricot.Grpc类库项目
     > dotnet new classlib -n Apricot.Grpc# 解决方案添加类库项目> dotnet sln add Apricot.Grpc/Apricot.Grpc.csproj
    
  2. 安装依赖
     > dotnet add package Grpc.AspNetCore --version 2.66.0> dotnet add package protobuf-net --version 3.2.30
    
  3. 创建Protos文件夹
    • 添加Garner文件夹,包含增、删、改、查 等操作
    • 添加 garner.proto 文件 [主文件]
      syntax = "proto3";
      option csharp_namespace = "Apricot.Grpc";package garner;// google protos
      import "google/protobuf/empty.proto";
      import "google/protobuf/Any.proto";// garner protos
      import "Protos/Garner/create.proto";
      import "Protos/Garner/update.proto";
      import "Protos/Garner/get.proto";
      import "Protos/Garner/list.proto";// params protos
      import "Protos/Params/id.proto";
      import "Protos/Params/query.proto";// results protos
      import "Protos/Results/result.proto";// services
      service Garner{rpc CreateAsync(CreateGarnerRequest) returns(RcpResult);rpc UpdateAsync(UpdateGarnerRequest) returns(RcpResult);rpc RemoveAsync(IdParam) returns(RcpResult);rpc GetAsync(IdParam) returns(GetGarnerResponse);rpc GetListAsync(QueryParam) returns(GetGarnerListResponse);
      }
    • 添加 create.proto 文件
      syntax = "proto3";option csharp_namespace = "Apricot.Grpc";package garner;// google empty.proto
      import "google/protobuf/empty.proto";// create request
      message CreateGarnerRequest{string name = 2;string address = 3;
      }
      
    • 添加 update.proto 文件
      syntax = "proto3";option csharp_namespace = "Apricot.Grpc";package garner;// google empty.proto
      import "google/protobuf/empty.proto";// update request
      message UpdateGarnerRequest{int64 id = 1;string name = 2;string address = 3;}
      
    • 添加 get.proto 文件
      syntax = "proto3";option csharp_namespace = "Apricot.Grpc";package garner;// google empty.proto
      import "google/protobuf/empty.proto";// garner response
      message GetGarnerResponse{int32 code = 1;string message = 2;bool success = 3;oneof garner{GetGarnerData data =4;}
      }// garner data
      message GetGarnerData{int64 id = 1;string name = 2;string address = 3;}
    • 添加 list.proto 文件
      syntax = "proto3";option csharp_namespace = "Apricot.Grpc";package garner;// google empty.proto
      import "google/protobuf/empty.proto";// garner list response
      message GetGarnerListResponse{int32 code = 1;string message = 2;bool success = 3;int32 total = 4;repeated GetGarnerListData rows = 5;
      }// garner list data
      message GetGarnerListData{int64 id = 1;string name = 2;string address = 3;}
      
  4. 项目文件添加 .proto 配置
    <ItemGroup><Protobuf Include="Protos\Results\result.proto" /><Protobuf Include="Protos\Params\query.proto" /><Protobuf Include="Protos\Params\id.proto" /><Protobuf Include="Protos\Garner\get.proto" /><Protobuf Include="Protos\Garner\list.proto" /><Protobuf Include="Protos\Garner\update.proto" /><Protobuf Include="Protos\Garner\create.proto" /><Protobuf Include="Protos\Garner\garner.proto" GrpcServices="Server" /></ItemGroup>
    
  5. 编译项目
     > dotnet build
    
  6. 查看生成类
     > dir obj\Debug\net8.0\Protos
    
  7. 创建 grpc service
    • 创建 GarnerGrpcService
    • 继承 Garner.GarnerBase
    • 重写方法
    • 整体代码
       public class GarnerGrpcService : Garner.GarnerBase{public override Task<RcpResult> CreateAsync(CreateGarnerRequest request, ServerCallContext context){return Task.FromResult(new RcpResult{Code = StatusCodes.Status200OK,Success = true,});}public override Task<RcpResult> UpdateAsync(UpdateGarnerRequest request, ServerCallContext context){return Task.FromResult(new RcpResult{Code = StatusCodes.Status200OK,Success = true,});}public override Task<RcpResult> RemoveAsync(IdParam request, ServerCallContext context){return Task.FromResult(new RcpResult{Code = StatusCodes.Status200OK,Success = true,});}public override Task<GetGarnerResponse> GetAsync(IdParam request, ServerCallContext context){return Task.FromResult(new GetGarnerResponse{Code = StatusCodes.Status200OK,Success = true,Data = new GetGarnerData{Id = Random.Shared.NextInt64(),Address = "127.0.0.1",Name = "garner"}});}public override Task<GetGarnerListResponse> GetListAsync(QueryParam request, ServerCallContext context){var response = new GetGarnerListResponse{Code = StatusCodes.Status200OK,Success = true,Total = 10,};response.Rows.AddRange(new[]{new GetGarnerListData{Id = Random.Shared.NextInt64(),Address = "127.0.0.1",Name = "garner"},new GetGarnerListData{Id = Random.Shared.NextInt64(),Address = "127.0.0.1",Name = "apricot"}});return Task.FromResult(response);}
      }
      

三、Grpc.WebApi

  1. 创建Apricot.Grpc.WebApi启动项目
     > dotnet new web -n Apricot.Grpc.WebApi# 解决方案添加启动项目> dotnet sln add Apricot.Grpc.WebApi/Apricot.Grpc.WebApi.csproj
    
  2. 添加项目引用
     > dotnet add reference ../Apricot.Grpc/Apricot.Grpc.csproj
    
  3. 注入容器、管道
     // add grpcbuilder.Services.AddGrpc();// map grpcapp.MapGrpcService<GarnerGrpcService>();
    
  4. 协议配置
    {"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*",// setting  http2 protocol"Kestrel": {"EndpointDefaults": {"Protocols": "Http2"}}
    }
    
    • 支持 Http1/Http2 方法
    builder.WebHost.ConfigureKestrel(options =>{// http2options.ListenAnyIP(5132, listenOption =>{listenOption.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;});// http1options.ListenAnyIP(5133, listenOption =>{listenOption.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1;});});
    
  5. 启动项目

四、apifox 联调 grpc

  1. 个人团队
  2. 新建项目
    • 类型 grpc 项目
  3. 添加 .proto
    • .proto 文件

      • 选择 garner.proto 主文件
    • 依赖关系目录

      • 选择 Protos 文件夹
    • 错误 & 处理

      • 未找到 google protobuf

      • google protobuf 拷贝 Protos 目录

        • 目录:%USERPROFILE%.nuget\packages\grpc.tools\2.66.0\build\native\include\google
      • 未找到 create.proto

      • garner.protoimport 去掉 Protos/ (仅限导入)

    • 导入成功

  4. 接口测试 create、list
    • create
    • list

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/73502.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

NOIP 计划 R15

NOIP 计划 R15 \(\def\EZ{\textcolor{#51af44}{\text{EZ}}}\EZ\) 表示简单,10分钟内就能想到。 \(\def\HD{\textcolor{#3173b3}{\text{HD}}}\HD\) 表示中等,能独立想出 \(\def\IN{\textcolor{#be2d23}{\text{IN}}}\IN\) 表示困难,独立思考能想到 \(50\%\) 以上 \(\def\AT{\t…

星际战甲 - 武器配卡

题记部分 一、主武器二、副武器三、近战武器 3.1、弧光蓄电重锤 — 业精于勤荒于嬉,行成于思毁于随 —

web端ant-design-vue-Anchor锚点组件使用小节(1)

web端ant-design-vue-Anchor锚点组件使用小节。项目开发中如果要实现前端页面平滑滚动到指定的位置,Anchor组件是一个好的选择,灵活且平滑,能满足常见的项目需求。最近开发中幸运的用到这个组件,从此对她爱不释手。下面就把开发中遇到的一些问题及源码整理出来,供以后查看…

高等数学 6.2 定积分在几何学上的应用

目录一、平面图形的面积1.直角坐标情形2.极坐标情形二、体积1.旋转体体积2.平行截面面积为已知的立体的体积三、平面曲线的弧长 一、平面图形的面积 1.直角坐标情形 我们已经知道,由曲线 \(y = f(x) (f(x) \geqslant 0)\) 及直线 \(x = a, x = b (a < b)\) 与 \(x\) 轴所围…

Ubuntu 16.04 编译安装Python 2.7.18

安装python 2.7.18(注)使用apt install python安装的版本是2.7.10,该版本对部分项目存在兼容性问题,因此需要手动编译安装 安装python编译环境sudo apt install python-dev pkg-config libreadline-dev libc6-dev libncursesw5-dev build-essential gdb pkg-config libbz2-…

STM32F1,LVGL简易DEMO移植

简介 尝试过在ESP32上移植LVGL之后,再在STM32上面LVGL,确认下是不是可以用 虽然STM32F103ZE的ROM及RAM都没有ESP32丰富,便对应于LVGL的最低配置要求,应该也可以正常运行的。不过也只能移植简单的 按键显示,像复杂一些DEMO,在STM32F1不用了,资源不够,导致编译不通过。 L…

于国庆回高中母校省锡中有感(搬运自qq空间)

于2024.10.6下午在回京高铁上浅记一下昨日回省锡中的一些事(虽然到宿舍才发) 有些东西还是一如既往,比如乐群湖边乱窜的白鹅,某人的摩托车;但有些已悄然发生了变化,诚信超市5元大瓶装饮料被取缔,为防止糖尿病小卖部含糖饮品被禁售(太了吧),教室外走廊上多出的公共饮水…

数据采集与融合技术实践作业一

作业1:大学排名数据爬取 作业代码和图片主要代码import urllib.request from bs4 import BeautifulSoup import re # 导入正则表达式模块# 指定要爬取的URL url = http://www.shanghairanking.cn/rankings/bcur/2020# 发送请求获取网页内容 response = urllib.request.urlope…