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";

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

     blockingStub = ServiceGrpc.newBlockingStub(channel);

    response =;



the above code gives following exception

io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
    at io.grpc.stub.ClientCalls.toStatusRuntimeException(
    at io.grpc.stub.ClientCalls.getUnchecked(
    at io.grpc.stub.ClientCalls.blockingUnaryCall(

can someone help with Client code

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

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

 公共类testgrpc {
 ManagedChannel channel; 
 ServiceGrpc.ServiceBlockingStub  BlockingStub; 
字符串host =“ remotesecuredhost”; 
 int port =“ XXX”; 
 public void testgrpc()
 channel = ManagedChannelBuilder.forAddress(host,port).build  (); 
blockingStub = ServiceGrpc.newBlockingStub(channel); 
 response =。build()); 
 <  / code>  


在io.grpc  .stub.ClientCalls.toStatusRuntimeException(
在io.grpc.stub.ClientCalls.blockingUnaryCall(  )


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

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
    1. for non tls
                  channel  = NettyChannelBuilder
    打赏 评论
  • 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.
        AppClient(ManagedChannel channel) {
   = channel;
            blockingStub = PublisherGrpc.newBlockingStub(channel);
        public Ack publish(String payload) {
  "Payload sent to publisher ");
            Builder builder = Message.newBuilder();
            Message message =;
            try {
                Ack ack = blockingStub.publish(message);
      "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 */
            } finally {
    打赏 评论
  • dongtan5555 2019-05-12 12:21

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

    打赏 评论

相关推荐 更多相似问题