• .Net Core框架下实现Grpc客户端和服务端


    一、Grpc服务端

    1、新建.Net core框架下Grpc服务

    2、修改launchsettings.json

    {
      "profiles": {
        "GrpcServer": {
          "commandName": "Project",
          "dotnetRunMessages": "true",
          "launchBrowser": false,
          "applicationUrl": "http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }

    3、修改greet.proto文件

    syntax = "proto3";
    
    option csharp_namespace = "MyGrpcServer";
    
    package MyGrpc;
    
    // The greeting service definition.
    service TestGrpc {
      // Sends a greeting
      rpc TestSay (TestRequest) returns (TestReply);
    }
    
    // The request message containing the user's name.
    message TestRequest {
      string name = 1;
    }
    
    // The response message containing the greetings.
    message TestReply {
      string message = 1;
    }

    4、修改GreeterService.cs

    public class GreeterService : TestGrpc.TestGrpcBase
        {
            private readonly ILogger<GreeterService> _logger;
            public GreeterService(ILogger<GreeterService> logger)
            {
                _logger = logger;
            }
    
            public override Task<TestReply> TestSay(TestRequest request, ServerCallContext context)
            {
                Console.WriteLine($"接收到{request.Name}的消息");
                return Task.FromResult(new TestReply
                {
                    Message = "Hello " + request.Name
                });
            }
        }

    5、修改appsettings.json

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "Urls": "http://localhost:5000",
      "Kestrel": {
        "EndpointDefaults": {
          "Protocols": "Http2"
        }
      }
    }

    二、Grpc客户端

    1、新建.Net Core 控制台应用程序

     2、NuGet包下载安装

    Grpc.Net.Client
    Google.ProtoBuf
    Grpc.Tools

    3、新建Protos文件夹,将服务端的greet.proto复制到该文件夹下,修改greet.proto

    syntax = "proto3";
    
    option csharp_namespace = "MyGrpcClient";
    
    package MyGrpc;
    
    // The greeting service definition.
    service TestGrpc {
      // Sends a greeting
      rpc TestSay (TestRequest) returns (TestReply);
    }
    
    // The request message containing the user's name.
    message TestRequest {
      string name = 1;
    }
    
    // The response message containing the greetings.
    message TestReply {
      string message = 1;
    }

    4、修改GrpcClient.csproj文件,添加一下代码

    <ItemGroup>
            <Protobuf Include="Protosgreet.proto" GrpcServices="Client" />
        </ItemGroup>

    5、修改Program.cs

    class Program
        {
            static async Task Main(string[] args)
            {
                using var channel = GrpcChannel.ForAddress("http://localhost:5000");
                var client = new TestGrpc.TestGrpcClient(channel);
                var ret = await client.TestSayAsync(new TestRequest { Name = "MyTestClient" });
                Console.WriteLine("MyTestGrpc: " + ret.Message);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
  • 相关阅读:
    构建TensorFlow数据流图
    Python小练习:复制操作
    Python小练习:列表的相关操作
    【Jave】接入极光推送 ------- 封装极光推送工具类
    jenkins邮件-使用变量定制化html邮件报告
    十六进制的颜色转变为rgb,设置透明度,通用方法
    一. Go微服务--隔离设计
    7.23 学习笔记
    7.22 学习笔记
    8.28正睿CSP七连测day1
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15157339.html
Copyright © 2020-2023  润新知