gRPC - macOS missing ALPN support.

grpc学习官方文档

.net core 版本: 3.1

操作系统: macOS

 

在新建 grpc 工程后,运行时出现 

Unable to bind to https://localhost:5001 on the IPv4 loopback interface: 'HTTP/2 over TLS is not supported on macOS due to missing ALPN support.'.

按照官方给出的解决方案是:

请将 Kestrel 和 gRPC 客户端配置为在不使用 TLS 的情况下使用 HTTP/2。 只应在开发过程中执行此操作。 如果不使用 TLS,将会在不加密的情况下发送 gRPC 消息。

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

 1 public static IHostBuilder CreateHostBuilder(string[] args) =>
 2     Host.CreateDefaultBuilder(args)
 3         .ConfigureWebHostDefaults(webBuilder =>
 4         {
 5             webBuilder.ConfigureKestrel(options =>
 6             {
 7                 // Setup a HTTP/2 endpoint without TLS.
 8                 options.ListenLocalhost(5000, o => o.Protocols = 
 9                     HttpProtocols.Http2);
10             });
11             webBuilder.UseStartup<Startup>();
12         });

如果未使用 TLS 配置 HTTP/2 终结点,则终结点的 ListenOptions 必须设置为 HttpProtocols.Http2 。 无法使用  HttpProtocols.Http1AndHttp2 ,因为需要使用 TLS 来协商 HTTP/2。 如果没有 TLS,与端点的所有连接默认为 HTTP/1.1,并且 gRPC 调用失败。

GRPC 客户端还必须配置为不使用 TLS。必须将  System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport  开关设置为  true  并使用服务器地址中的  http :

1 // This switch must be set before creating the GrpcChannel/HttpClient.
2 AppContext.SetSwitch(
3     "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
4 
5 // The port number(5000) must match the port of the gRPC server.
6 var channel = GrpcChannel.ForAddress("http://localhost:5000");
7 var client = new Greet.GreeterClient(channel);

posted on 2020-01-17 16:08  ChenXinyuan  阅读(666)  评论(0编辑  收藏  举报

导航