gRpc 跨语言调用(NetCore 与 Spring Boot)

NetCore 3.1:

前天才发布的正式版netcore 3.1 https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1/   手痒中...

简介如下:https://docs.microsoft.com/zh-cn/aspnet/core/grpc/?view=aspnetcore-3.1

示例代码如下:https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/tutorials/grpc/grpc-start/sample 也可以自己写。

以上没什么难度。

spring boot :

 很遗憾到现在为止,还没有grpc的官方版本,我这采用 https://yidongnan.github.io/grpc-spring-boot-starter/zh-CN/

https://github.com/yidongnan/grpc-spring-boot-starter 源码下载下来,当然也可以自己写 https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples 中的示例。

 先编译 grpc-lib 之后分别编译 client 和 server 正常运行。

以上没什么难度。

相互调用:

net 中示例使用的是 http2且带着 tls 

spring 中示例使用的是 http2 无 tls 

 

所以只需要修改net的即可!!!

注意 proto 保持一致(proto3),

文件中 option 以上部分不用动,只需要统一 service 定义部分即可。
 1 package greet;
 2 
 3 // The greeting service definition.
 4 service Greeter {
 5   // Sends a greeting
 6   rpc SayHello (HelloRequest) returns (HelloReply);
 7 }
 8 
 9 // The request message containing the user's name.
10 message HelloRequest {
11   string name = 1;
12 }
13 
14 // The response message containing the greetings.
15 message HelloReply {
16   string message = 1;
17 }
 1  public class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             CreateHostBuilder(args).Build().Run();
 6         }
 7 
 8         public static IHostBuilder CreateHostBuilder(string[] args) =>
 9             Host.CreateDefaultBuilder(args)
10                 .ConfigureWebHostDefaults(webBuilder =>
11                 {
12                     webBuilder.ConfigureKestrel(options =>
13                     {
14                         // Setup a HTTP/2 endpoint without TLS.
15                         options.ListenLocalhost(5000, o => o.Protocols =
16                             HttpProtocols.Http2);
17                     });
18                     webBuilder.UseStartup<Startup>();
19                 });
20     }

来自 https://docs.microsoft.com/zh-cn/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1#unable-to-start-aspnet-core-grpc-app-on-macos

Kestrel 必须在Program.cs中配置不包含 TLS 的 HTTP/2 终结点

客户端修改如下:

 1     class Program
 2     {
 3         #region snippet
 4         static async Task Main(string[] args)
 5         {
 6             // The port number(5001) must match the port of the gRPC server.
 7             AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
 8 
 9             var channel = GrpcChannel.ForAddress("http://localhost:5001");
10             var client =  new Greeter.GreeterClient(channel);
11             var reply = await client.SayHelloAsync(
12                               new HelloRequest { Name = "GreeterClient" });
13             Console.WriteLine("Greeting: " + reply.Message);
14             Console.WriteLine("Press any key to exit...");
15             Console.ReadKey();
16         }
17         #endregion
18     }

来自 https://docs.microsoft.com/zh-cn/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1#call-insecure-grpc-services-with-net-core-client

若要将不安全的 gRPC 服务与 .NET Core 客户端一起调用,需要进行其他配置。 GRPC 客户端必须将 System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport 开关设置为 true 并使用服务器地址中的 http

改一下各自的服务端和客户端的端口地址就可以互通有无了,效果如下:

        public override Task<HelloReply> SayHello(
            HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name +" From Net Core."
            });
        }
View Code

疑难问题参考一下基本都能搞定:

https://docs.microsoft.com/zh-cn/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1

https://yidongnan.github.io/grpc-spring-boot-starter/zh-CN/trouble-shooting

https://github.com/grpc/grpc-java/issues

猜你喜欢

转载自www.cnblogs.com/bhfdz/p/11987814.html
今日推荐