簡體   English   中英

如何為安全的 go grpc 服務創建不安全的 Java grpc cilent

[英]How to create Insecure Java grpc cilent for secure go grpc service

我正在嘗試在 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)

有人可以幫忙處理客戶端代碼嗎


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();
        }
    }
}

可能是您代碼中的錯誤。 我的代碼運行良好,請克隆它。

https://github.com/kubesure/publisher

我終於找到了解決方案

  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-static版本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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM