gRpc 官网  链接

新建服务端项目

在服务端内先编写一个 .proto 文件

greet.proto

  1. syntax = "proto3";
  2.  
  3. service Greeter {
  4. rpc SayHello (HelloRequest) returns (HelloReply);
  5. }
  6.  
  7. message HelloRequest {
  8. string name = ;
  9. }
  10.  
  11. message HelloReply {
  12. string message = ;
  13. }

服务端生成 .proto 代码为 C#   NutGet Install-Package Grpc.Tools

 

找到tools 从里面选择自己电脑的版本

把 protoc.exe 、 grpc_csharp_plugin.exe 复制出来和 .proto 文件放在一起

编写一个 .cmd 文件和 .proto 文件放在一起,插入生成代码的指令。(注意 greet.proto 这是 .proto 文件名)

  1. protoc -I . --csharp_out . --grpc_out . --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe greet.proto   

点击cmd 生成 会获得两个C#文件。

新建类GreeterService

并在使用客户端之前启动(启动服务端之前,要先排除与.Net 无关的.proto文件,不然直接运行会报错的)

  1. public class GreeterService : Greeter.GreeterBase
  2. {
  3. private readonly ILogger<GreeterService> _logger;
  4.  
  5. public GreeterService(ILogger<GreeterService> logger)
  6. {
  7. _logger = logger;
  8. }
  9.  
  10. public override Task<HelloReply> SayHello(HelloRequest request,
  11. ServerCallContext context)
  12. {
  13. _logger.LogInformation("Saying hello to {Name}", request.Name);
  14. return Task.FromResult(new HelloReply
  15. {
  16. Message = "Hello " + request.Name
  17. });
  18. }  
  19. }
  20. class Program
  21. {
  22.  
  23. const int Port = ;
  24.  
  25. static void Main(string[] args)
  26. {
  27. Server server = new Server
  28.    {
  29. Services = { Greeter.BindService(new GreeterService()) },
  30. Ports = { new ServerPort("127.0.0.1", , ServerCredentials.Insecure) }
  31. };
  32. server.Start();
  33. Console.WriteLine("127.0.0.1");
  34. Console.WriteLine("GrpcService server listening on port " + Port);
  35. Console.WriteLine("任意键退出...");
  36. Console.ReadKey();
  37. }
  38. }

新建控制台客户端项目

客户端调用服务端

  1. namespace gRPCClient
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7.  
  8. // The port number(5001) must match the port of the gRPC server.
  9. Channel channel = new Channel("127.0.0.1", , ChannelCredentials.Insecure);
  10. var client = new Greeter.GreeterClient(channel).WithHost("127.0.0.1");
  11.  
  12. try
  13. {
  14. var reply = client.SayHelloAsync(new HelloRequest { Name = "Zhang San" });
  15. Console.WriteLine("来自" + reply.ResponseAsync.Result);
  16.  
  17. channel.ShutdownAsync().Wait();
  18. Console.WriteLine("Greeting: " + reply.ResponseAsync.Result);
  19. Console.WriteLine("Press any key to exit...");
  20. Console.ReadKey();
  21. }
  22. catch (Exception ex)
  23. {
  24.  
  25. throw ex;
  26. }
  27.  
  28. }
  29. }
  30. }

gRPC搭建使用方式的更多相关文章

  1. Linux系统——搭建FTP方式的本地定制化Yum仓库

    (1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe ...

  2. 跟我一起学Go系列:Go gRPC 安全认证方式-Token和自定义认证

    Go gRPC 系列: 跟我一起学Go系列:gRPC安全认证机制-SSL/TLS认证 跟我一起学 Go 系列:gRPC 拦截器使用 跟我一起学 Go 系列:gRPC 入门必备 接上一篇继续讲 gRPC ...

  3. TensorFlow搭建模型方式总结

    引言 TensorFlow提供了多种API,使得入门者和专家可以根据自己的需求选择不同的API搭建模型. 基于Keras Sequential API搭建模型 Sequential适用于线性堆叠的方式 ...

  4. 使用gRPC搭建Server端与Client端

    gRPC简介 gRPC是一种RPC框架技术,采用Protocal Buffers(协议缓存) 作为其接口定义的语言(就是Proto来写接口)和基础的消息交换格式. 在gRPC中,客户端应用程序可以直接 ...

  5. CAS环境搭建-证书方式(https连接)

    一.教程前言 1 教程目的:从头到尾细细道来单点登录服务器及客户端应用的每个步骤 2 单点登录(SSO):请看<CAS简介> 3 本教程使用的SSO服务器是Yelu大学研发的CAS(Cen ...

  6. springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

    在上一篇依赖方式多模块的基础上对项目进行改造.主要改造user-service项目,service要配置mapper.mybatis及数据库相关的东西,后面的接口消费方user就不再需要了 注意:以下 ...

  7. .NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端

    .NET Core love gRPC 千呼万唤的 .NET Core 3.0 终于在 9 月份正式发布,在它的众多新特性中,除了性能得到了大大提高,比较受关注的应该是 ASP.NET Core 3. ...

  8. 【网络安全】window 快速搭建 ftp 及 多种访问方式

    在局域网里面使用ftp传输文件比使用qq等软件传输速度快很多,但是搭建ftp很多时候需要下载相应的支持软件,其实不必下载相关的软件,因为window自带ftp功能. 演示操作系统:windows10 ...

  9. GRpc添加客户端的四种方式

    随着微服务的发展,相信越来越多的.net人员也开始接触GRpc这门技术,大家生成GRpc客户端的方式也各不相同,今天给大家介绍一下依据Proto文件生成Rpc客户端的四种方式 前提:需要安装4个Nug ...

随机推荐

  1. java8 stream按对象多个属性对集合进行分组,并进行组装数据

    如图,数据库查出来的数据: 需求是按menu_id和menu_name分组,stream实现最简单, stream里面只有按一个属性分组的,但是可以利用string简单变换一下: List<Js ...

  2. 跟Evan学Sprign编程思想 | Spring注解编程模式【译】

    Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...

  3. FMPEG结构体分析:AVStream

    注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...

  4. C++ traits技法的一点理解

    为了更好的理解traits技法.我们一步一步的深入.先从实际写代码的过程中我们遇到诸如下面伪码说起. template< typename T,typename B> void (T a, ...

  5. AWS的边缘计算平台GreenGrass和IoT

    AWS的边缘计算平台GreenGrass和IoT 为什么需要有边缘计算? 如今公有云和私有云平台提供的服务已经连接上了绝大多数的桌面设备和移动设备.但是更多的设备比如,车辆,工程机械,医疗设备,无人机 ...

  6. PHP反序列化中过滤函数使用不当导致的对象注入

    1.漏洞产生的原因 ####  正常的反序列化语句是这样的 $a='a:2:{s:8:"username";s:7:"dimpl3s";s:8:"pa ...

  7. vmware14 unlock开启macos选项

    之前搜索了很多资料,用了很多Unlock都失败了,最后重新卸载vmware重新安装后,关闭应用竟然可以了 工具在微信公众号菜菜电脑已保存到百度网盘

  8. 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, ...

  9. 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 ...

  10. ionic2的返回按钮的编辑问题

    ionic2 返回按钮 首先可以在 app.module.ts 文件中配置. @NgModule 中的 imports 属性的 IonicModule.forRoot 第二个参数,如下: IonicM ...