如何为安全的 go grpc 服务创建不安全的 Java grpc cilent

Err*_*ter 3 java ssl go grpc grpc-java

我正在尝试用 Java 创建 grpc 服务客户端,其中服务器位于 goLang 中并使用 https 进行部署。我试图实现非安全连接[我不想通过证书]

public class testgrpc {
    ManagedChannel channel ;
    ServiceGrpc.ServiceBlockingStub  blockingStub;
    String host = "remotesecuredhost";
    int port ="XXX";

    @Test
    public void testgrpc()
    {
    channel = ManagedChannelBuilder.forAddress(host,port).build();

     blockingStub = ServiceGrpc.newBlockingStub(channel);

    response =  blockingStub.health(Empty.newBuilder().build());

    }

}
Run Code Online (Sandbox Code Playgroud)

上面的代码给出了以下异常

io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
    at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:221)
    at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:202)
    at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:131)
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙处理客户端代码吗

Err*_*ter 7

就我而言,我终于找到了解决方案

  1. Netty 版本在我的 build.gradle 中不同步,因此我收到如下异常
Could not find TLS ALPN provider; no working netty-tcnative,
Conscrypt, or Jetty NPN/ALPN available
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我设置了如下版本

grpc-netty - 1.20.x- ||| netty-handler-4.1.34.Final ||| netty-tcnative-boringssl-static 版本 2.0.22.Final 从这里得到了想法

  1. 对于 tls 连接,这有效
channel  = NettyChannelBuilder
                .forAddress("host",port)
               .sslContext(GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build())
                .build();

Run Code Online (Sandbox Code Playgroud)
  1. 对于非 tls

              channel  = NettyChannelBuilder
            .forAddress("host",port).usePlaintext()
       .build();
Run Code Online (Sandbox Code Playgroud)