简介

grpc是由google公司开发的一个高性能、开源和通用的RPC框架,采用HTTP/2通信。

1.gRPC的传输使用http/2支持双向流。

2.支持多语言,例如java、go、php、net、node等多种语言.

3.gRPC支持多平台

4.性能好,效率高

1.HTTP/2

HTTP/2 提供了连接多路复用、双向流、服务器推送、请求优先级、首部压缩等机制。可以节省带宽、降低TCP链接次数、节省CPU,帮助移动设备延长电池寿命等。gRPC 的协议设计上使用了HTTP2 现有的语义,请求和响应的数据使用HTTP Body 发送,其他的控制信息则用Header 表示。

使用

以下演示基于netcore2.2进行创建运行

  运行前准备

因为netcore不会生成依赖包,所以需要先新建一个net framework的类库程序把需要用到的文件下载来

1.新建net framework程序并且安装grpc.tools 

2.安装成功之后项目文件夹里会生成一个packages文件,在这里找到我们所需要的工具(x86还64请根据自己电脑自行选择)

1.环境准备

  •   GrpcClient   命令行程序
  •   GrpcLibray  类库程序
  •   GrpcServer 命令行程序

 

2.安装项目依赖

GrpcClient和GrpcLibray和GrpcServer 都需要安装通用的插件

install-package Grpc
install-package Goole.Protobuf

 3.在GrpcLibray程序当中把前面我们用netframework添加的exe文件添加到该项目当中

4.添加命令行用来生成供其他程序调用的代码(hello.cmd)

protoc -I . --csharp_out ./server --grpc_out ./server --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe hello.proto

5.编写接口服务(hello.proto)

syntax = "proto3";
package GrpcLibrary;
service HelloService{ rpc GetSum(GetMsgNumRequest) returns (GetMsgSumReply){}; rpc test(GetLotID)returns(GetLotAll){};
}
message GetMsgNumRequest { int32 Num1 = ; int32 Num2 = ;}
message GetMsgSumReply { int32 Sum = ;} message GetLotID{int32 Num1=;string s=;} message GetLotAll{
enum Lists{
mes=;
wip=;
eap=;
dcs=;
}
Lists t=; repeated UserList user=;
} message UserList{
string userName=;
string pwd=;
}

6.在GrpcServer当中引用GrpcLibrary,并且重写定义的方法

using Grpc.Core;
using GrpcLibrary;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; namespace GrpcServer
{
class Program
{
public class GrpcImpl : HelloService.HelloServiceBase
{ public override async Task<GetMsgSumReply> GetSum(GetMsgNumRequest request, ServerCallContext context)
{
var result = new GetMsgSumReply(); result.Sum = request.Num1 + request.Num2; return result;
}
public override async Task<GetLotAll> test(GetLotID request, ServerCallContext context)
{
var result = new GetLotAll();
string s = "这里一共有:";
if (request.Num1 == )
{
s += "1个字符";
}
s += request.S; UserList userList = new UserList();
userList.UserName = "张三";
result.User.Add(userList); List<UserList> userLists = new List<UserList>();
result.User.AddRange(userLists);
result.T = GetLotAll.Types.Lists.Wip;
return result;
} }
private static Server _server;
static void Main(string[] args)
{
_server = new Server {
Services = {HelloService.BindService(new GrpcImpl()) },
Ports = { new ServerPort("localhost",,ServerCredentials.Insecure)}
};
_server.Start();
Console.WriteLine("grpc ServerListening On Port 8088");
Console.WriteLine("任意键退出...");
Console.ReadKey(); _server?.ShutdownAsync().Wait(); }
}
}

7.grpcClient客户端也需要引用GrpcLibrary

using Grpc.Core;
using GrpcLibrary;
using System;
using System.Collections.Generic;
using System.Text; namespace GrpcClient
{
public static class HelloClient
{
private static Channel _channel;
private static HelloService.HelloServiceClient _client; static HelloClient()
{
_channel = new Channel("localhost:8088",ChannelCredentials.Insecure);
_client = new HelloService.HelloServiceClient(_channel);
}
public static GetMsgSumReply getSum(int num1, int num2)
{
return _client.GetSum(new GetMsgNumRequest { Num1=num1,Num2=num2});
}
public static GetLotAll GetLotAll(int num1, string s)
{
return _client.test(new GetLotID { Num1 = num1, S = s });
}
}
}

 8.客户端调用

using GrpcLibrary;
using System; namespace GrpcClient
{
class Program
{
static void Main(string[] args)
{ GetMsgSumReply helloMsg = HelloClient.getSum(,) ; GetLotAll getLotAll = HelloClient.GetLotAll(, "我是老哈哈哈哈");
Console.WriteLine("grpc Client Call GetSum():" + helloMsg.Sum);
string user = string.Empty;
foreach (var item in getLotAll.User)
{
user += item;
}
Console.WriteLine("grpc Client Call GetLotAll():" + user);
Console.WriteLine("任意键退出...");
Console.ReadKey();
}
}
}

运行如下图所示,先运行服务器端,在运行客户端

源码地址:https://github.com/zhengyazhao/grpc.git

netcore中使用grpc的更多相关文章

  1. .NetCore中的日志(2)集成第三方日志工具

    .NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...

  2. .NetCore中的日志(1)日志组件解析

    .NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...

  3. C#中使用gRPC

    C#中使用gRPC 我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目 ...

  4. 初识google多语言通信框架gRPC系列(三)C#中使用gRPC

    我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目提供借鉴.如果对gR ...

  5. 初识google多语言通信框架gRPC系列(四)C++中使用gRPC

    我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目提供借鉴.如果对gR ...

  6. AutoMapper在asp.netcore中的使用

    # AutoMapper在asp.netcore中的使用  automapper 是.net 项目中针对模型之间转换映射的一个很好用的工具,不仅提高了开发的效率还使代码更加简洁,当然也是开源的,htt ...

  7. netcore中的缓存介绍

    Cache(缓存)是优化web应用的常用方法,缓存存放在服务端的内存中,被所有用户共享.由于Cache存放在服务器的内存中,所以用户获取缓存资源的速度远比从服务器硬盘中获取快,但是从资源占有的角度考虑 ...

  8. 在netcore中如何注入同一个接口的多个实现

    netcore中自带了Ioc框架,这也影响了我们的编码习惯,以前都是静态类或者直接new对象,现在有了Ioc框架的支持,我们也不必守旧,应当使用起来,接受这种对象管理方式.使用过java的同仁,都习惯 ...

  9. .NetCore中EFCore的使用整理(二)-关联表查询

    EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core  1.1 1.当前的版本,还不支持延迟加载(Lazy ...

随机推荐

  1. 原生js获得八种方式,事件操作

    08.17自我总结 关于js 一.原生js获得八种方式 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTa ...

  2. Go-接口(作用类似python类中的多态)

    一.定义接口 type Person interface { Run() //只要有run方法的都算 Person结构体 } //还有定义方法 type Person2 interface { Spe ...

  3. python 使用tesseract进行图片识别

    from PIL import Image import pytesseract text = pytesseract.image_to_string(Image.open(r'E:\guo\2432 ...

  4. JS中的slice()和splice()的区别以及记忆方式

    总结 splice()会改变原来的数组,返回的是被改变的内容,比如说通过splice删掉了某一项,那么返回的是删掉的这一项,当然还是会以数组的形式返回. slice不会对原数组进行改变,会返回一个新的 ...

  5. OC-AVAudioPlayer的使用小记

    - (void)viewDidLoad { [super viewDidLoad]; //设置音乐的后台播放,注意background mode中需要勾选上 AVAudioSession *sessi ...

  6. Python完全平方数

    python解题源代码如下: import math """ 简述:一个整数,它加上100和加上268后都是一个完全平方数 提问:请问该数是多少? Python解题思路分 ...

  7. RDIFramework.NET敏捷开发框架 ━ 工作流程组件介绍

    RDIFramework.NET,基于.NET的快速信息化系统敏捷开发.整合框架,给用户和开发者最佳的.Net框架部署方案. 1.RDIFramework.NET敏捷开发框架介绍 RDIFramewo ...

  8. MSSQL 删除重复数据

    --删除重复数据,无标识列情况 if object_id(N'test',N'U') is not null drop table test go create table test( id INT, ...

  9. mysql 安装(Linux、Ubuntu)

    1.检查系统是否已经安装过mysql rpm -qa | grep mysql 若出现类似于以上的结果则表明系统已经安装过mysql,执行以下命令卸载 rpm -e --nodeps mysql-co ...

  10. GO 使用静态链接库编译 生成可执行文件 使用第三方 .a 文件,无源码构造

    go build 和 go install 都需要使用源码来进行编译.但是有时候我们只有.a或者.so文件.并不能获取到第三方库的源码,这时我们需要静态链接库编译的技巧: 上图是实验前的文件分布. 使 ...