在我使用 gRPC Java 的项目中,我使用 OpenSSL 通过还原代理 (HAProxy) 在客户端和服务器之间建立安全连接。
由于客户端和服务器通过还原代理进行交互,因此我只为 HAProxy 配置 SSL,因此无需对服务器执行任何操作。所以:
- 客户端<>代理:TLS/SSL
- 代理<>服务器:原始数据
我设法成功运行服务器并在 HAProxy 上配置了 SSL,我用 https://www.digicert.com/help/检查了 SSL 配置,一切正常。
此处的文档提到安全通道的客户端代码如下:
ManagedChannel channel = ManagedChannelBuilder.forAddress("myservice.example.com", 443).build();
GreeterGrpc.GreeterStub stub = GreeterGrpc.newStub(channel);
这是我的代码:
channel = ManagedChannelBuilder
.forAddress(domain, port)
.usePlaintext(false)
.build();
interceptChannel = ClientInterceptors.intercept(channel, new ClientHeaderInterceptor());
asyncStub = GatewayServiceGrpc.newStub(channel);
blockingStub = GatewayServiceGrpc.newBlockingStub(interceptChannel);
但问题是我无法连接到服务器,没有响应,它根本不会抛出任何超出或错误。
但是,如果我从客户端将usePlaintext从false更改为true(禁用 TLS),并且代理仍然具有 SSL,那么我会收到以下异常:
io.grpc.StatusRuntimeException: UNAVAILABLE: Network closed for unknown reason
at io.grpc.Status.asRuntimeException(Status.java:526)
at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:385)
at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:41)
at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:339)
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:443)
at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:525)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:446)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:557)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:107)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
这是客户端的依赖项:
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.6.Final</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
我使用的是gRPC 版本 1.7.0。
创建频道时删除.usePlaintext(false)
将解决问题。