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



我正在尝试在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());
    }
}

上述代码给出以下异常

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)

有人可以帮助客户端代码

我最终发现了解决方案,在我的情况下

  1. Netty版本在我的build.gradle中不同步,因此我收到的例外
Could not find TLS ALPN provider; no working netty-tcnative,
Conscrypt, or Jetty NPN/ALPN available

要解决这个问题,我已经设置了按照

的设置版本

grpc-netty-1.20.x- |||Netty Handler-4.1.34.Final |||netty-tcnative-boringssl静态版本2.0.22.-final 从这里有想法

  1. 对于TLS连接,这项工作
channel  = NettyChannelBuilder
                .forAddress("host",port)
               .sslContext(GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build())
                .build();
  1. 对于非TLS

              channel  = NettyChannelBuilder
            .forAddress("host",port).usePlaintext()
       .build();

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.kubesure.publish.PublisherGrpc;
import io.kubesure.publish.PublisherProtos.Ack;
import io.kubesure.publish.PublisherProtos.Message;
import io.kubesure.publish.PublisherProtos.Message.Builder;
public class AppClient {
    private static final Logger logger = Logger.getLogger(AppClient.class.getName());
    private final ManagedChannel channel;
    private final PublisherGrpc.PublisherBlockingStub blockingStub;
    public AppClient(String host, int port) {
        this(ManagedChannelBuilder.forAddress(host, port)
                // Channels are secure by default (via SSL/TLS). For the example we disable TLS
                // to avoid
                // needing certificates.
                .usePlaintext().build());
    }
    AppClient(ManagedChannel channel) {
        this.channel = channel;
        blockingStub = PublisherGrpc.newBlockingStub(channel);
    }
    public Ack publish(String payload) {
        logger.info("Payload sent to publisher ");
        Builder builder = Message.newBuilder();
        builder.setPayload(payload);
        builder.setVersion("v1");
        builder.setType("Policy");
        builder.setDestination("policyissued");
        Message message = builder.build();
        try {
            Ack ack = blockingStub.publish(message);
            logger.info("is published: " + ack.getOk());
            return ack;
        } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return Ack.newBuilder().setOk(false).build(); 
        }
    }
    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }
    public static void main(String[] args) throws Exception {
        AppClient client = new AppClient("localhost", 50051);
        try {
            /* Access a service running on the local machine on port 50051 */
            String payload = "supplies to mars";
            if (args.length > 0) {
                payload = args[0]; /* Use the arg as the name to greet if provided */
            }
            client.publish(payload);
        } finally {
            client.shutdown();
        }
    }
}

最新更新