无法通过SSL从客户端恢复代理(HAProxy)连接到gRPC服务器

时间:2018-03-15 07:44:30

标签: java ssl haproxy grpc grpc-java

在我使用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

1 个答案:

答案 0 :(得分:1)

创建频道时删除.usePlaintext(false)将解决问题。