doujia2090
2019-04-29 12:11
浏览 437
已采纳

如何为安全的go grpc服务创建不安全的Java grpc客户端

I am trying to create grpc service client in Java where server is in goLang and deployed with the https . where I am trying to achieve a non-secured connection [I don't want to pass the certificate ]

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

    }

}

the above code gives following exception

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)

can someone help with Client code

图片转代码服务由CSDN问答提供 功能建议

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

 公共类testgrpc {
 ManagedChannel channel; 
 ServiceGrpc.ServiceBlockingStub  BlockingStub; 
字符串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()); 
 
} 
 
} 
 <  / code>  
 
 

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

  io.grpc.StatusRuntimeException:不可用:io异常
在io.grpc  .stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:221)
在io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:202)
在io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:131  )
   
 
 

有人可以帮助客户代码

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanlou7910 2019-05-15 05:23
    已采纳

    I have figured out the solution finally , in my case

    1. Netty versions were not in sync in my build.gradle , because of that I was receiving the exception as below
    Could not find TLS ALPN provider; no working netty-tcnative,
    Conscrypt, or Jetty NPN/ALPN available
    

    to resolve this I have setup the versions as followed

    grpc-netty - 1.20.x- ||| netty-handler-4.1.34.Final ||| netty-tcnative-boringssl-static version 2.0.22.Final got idea from here

    1. for tls connection this works
    channel  = NettyChannelBuilder
                    .forAddress("host",port)
                   .sslContext(GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build())
                    .build();
    
    
    1. for non tls
    
                  channel  = NettyChannelBuilder
                .forAddress("host",port).usePlaintext()
           .build();
    
    打赏 评论
  • dongziche8030 2019-04-30 08:18
    
    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();
            }
        }
    }
    
    打赏 评论
  • dongtan5555 2019-05-12 12:21

    could be an error within your code. my code works well, clone it.

    https://github.com/kubesure/publisher

    打赏 评论

相关推荐 更多相似问题