gRPC搭建使用方式
gRpc 官网 链接
新建服务端项目
在服务端内先编写一个 .proto 文件
greet.proto
- syntax = "proto3";
- service Greeter {
- rpc SayHello (HelloRequest) returns (HelloReply);
- }
- message HelloRequest {
- string name = ;
- }
- message HelloReply {
- string message = ;
- }
服务端生成 .proto 代码为 C# NutGet Install-Package Grpc.Tools
找到tools 从里面选择自己电脑的版本
把 protoc.exe 、 grpc_csharp_plugin.exe 复制出来和 .proto 文件放在一起
编写一个 .cmd 文件和 .proto 文件放在一起,插入生成代码的指令。(注意 greet.proto 这是 .proto 文件名)
- protoc -I . --csharp_out . --grpc_out . --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe greet.proto
点击cmd 生成 会获得两个C#文件。
新建类GreeterService
并在使用客户端之前启动(启动服务端之前,要先排除与.Net 无关的.proto文件,不然直接运行会报错的)
- public class GreeterService : Greeter.GreeterBase
- {
- private readonly ILogger<GreeterService> _logger;
- public GreeterService(ILogger<GreeterService> logger)
- {
- _logger = logger;
- }
- public override Task<HelloReply> SayHello(HelloRequest request,
- ServerCallContext context)
- {
- _logger.LogInformation("Saying hello to {Name}", request.Name);
- return Task.FromResult(new HelloReply
- {
- Message = "Hello " + request.Name
- });
- }
- }
- class Program
- {
- const int Port = ;
- static void Main(string[] args)
- {
- Server server = new Server
- {
- Services = { Greeter.BindService(new GreeterService()) },
- Ports = { new ServerPort("127.0.0.1", , ServerCredentials.Insecure) }
- };
- server.Start();
- Console.WriteLine("127.0.0.1");
- Console.WriteLine("GrpcService server listening on port " + Port);
- Console.WriteLine("任意键退出...");
- Console.ReadKey();
- }
- }
新建控制台客户端项目
客户端调用服务端
- namespace gRPCClient
- {
- class Program
- {
- static void Main(string[] args)
- {
- // The port number(5001) must match the port of the gRPC server.
- Channel channel = new Channel("127.0.0.1", , ChannelCredentials.Insecure);
- var client = new Greeter.GreeterClient(channel).WithHost("127.0.0.1");
- try
- {
- var reply = client.SayHelloAsync(new HelloRequest { Name = "Zhang San" });
- Console.WriteLine("来自" + reply.ResponseAsync.Result);
- channel.ShutdownAsync().Wait();
- Console.WriteLine("Greeting: " + reply.ResponseAsync.Result);
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
gRPC搭建使用方式的更多相关文章
- Linux系统——搭建FTP方式的本地定制化Yum仓库
(1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe ...
- 跟我一起学Go系列:Go gRPC 安全认证方式-Token和自定义认证
Go gRPC 系列: 跟我一起学Go系列:gRPC安全认证机制-SSL/TLS认证 跟我一起学 Go 系列:gRPC 拦截器使用 跟我一起学 Go 系列:gRPC 入门必备 接上一篇继续讲 gRPC ...
- TensorFlow搭建模型方式总结
引言 TensorFlow提供了多种API,使得入门者和专家可以根据自己的需求选择不同的API搭建模型. 基于Keras Sequential API搭建模型 Sequential适用于线性堆叠的方式 ...
- 使用gRPC搭建Server端与Client端
gRPC简介 gRPC是一种RPC框架技术,采用Protocal Buffers(协议缓存) 作为其接口定义的语言(就是Proto来写接口)和基础的消息交换格式. 在gRPC中,客户端应用程序可以直接 ...
- CAS环境搭建-证书方式(https连接)
一.教程前言 1 教程目的:从头到尾细细道来单点登录服务器及客户端应用的每个步骤 2 单点登录(SSO):请看<CAS简介> 3 本教程使用的SSO服务器是Yelu大学研发的CAS(Cen ...
- springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目
在上一篇依赖方式多模块的基础上对项目进行改造.主要改造user-service项目,service要配置mapper.mybatis及数据库相关的东西,后面的接口消费方user就不再需要了 注意:以下 ...
- .NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
.NET Core love gRPC 千呼万唤的 .NET Core 3.0 终于在 9 月份正式发布,在它的众多新特性中,除了性能得到了大大提高,比较受关注的应该是 ASP.NET Core 3. ...
- 【网络安全】window 快速搭建 ftp 及 多种访问方式
在局域网里面使用ftp传输文件比使用qq等软件传输速度快很多,但是搭建ftp很多时候需要下载相应的支持软件,其实不必下载相关的软件,因为window自带ftp功能. 演示操作系统:windows10 ...
- GRpc添加客户端的四种方式
随着微服务的发展,相信越来越多的.net人员也开始接触GRpc这门技术,大家生成GRpc客户端的方式也各不相同,今天给大家介绍一下依据Proto文件生成Rpc客户端的四种方式 前提:需要安装4个Nug ...
随机推荐
- java8 stream按对象多个属性对集合进行分组,并进行组装数据
如图,数据库查出来的数据: 需求是按menu_id和menu_name分组,stream实现最简单, stream里面只有按一个属性分组的,但是可以利用string简单变换一下: List<Js ...
- 跟Evan学Sprign编程思想 | Spring注解编程模式【译】
Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...
- FMPEG结构体分析:AVStream
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...
- C++ traits技法的一点理解
为了更好的理解traits技法.我们一步一步的深入.先从实际写代码的过程中我们遇到诸如下面伪码说起. template< typename T,typename B> void (T a, ...
- AWS的边缘计算平台GreenGrass和IoT
AWS的边缘计算平台GreenGrass和IoT 为什么需要有边缘计算? 如今公有云和私有云平台提供的服务已经连接上了绝大多数的桌面设备和移动设备.但是更多的设备比如,车辆,工程机械,医疗设备,无人机 ...
- PHP反序列化中过滤函数使用不当导致的对象注入
1.漏洞产生的原因 #### 正常的反序列化语句是这样的 $a='a:2:{s:8:"username";s:7:"dimpl3s";s:8:"pa ...
- vmware14 unlock开启macos选项
之前搜索了很多资料,用了很多Unlock都失败了,最后重新卸载vmware重新安装后,关闭应用竟然可以了 工具在微信公众号菜菜电脑已保存到百度网盘
- Codeforces 977D Divide by three, multiply by two(拓扑排序)
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, ...
- Why Oracle VIP can not be switched to original node ?
Oracle RAC is an share everything database architecture. The article is how to check out why virtual ...
- ionic2的返回按钮的编辑问题
ionic2 返回按钮 首先可以在 app.module.ts 文件中配置. @NgModule 中的 imports 属性的 IonicModule.forRoot 第二个参数,如下: IonicM ...