Search code examples
kubernetesgrpcgrpc-java

How to solve "UNAVAILABLE: Network closed for unknown reason" when a gRPC call with Java?


I am sending a grpc request with Java. Downstream is an istio service (gateway for seldon), which is the kubernetes cluster. Using the grpcurl command can get the result normally:

grpcurl -d '{"model_spec":{"name":"bmv3"},"inputs":{"reshape_1_input":{"dtype": 1, "tensor_shape": {"dim":[{"size": 1},{"size":312}]}, "floatVal" : [ 1 ] }}}' -rpc-header seldon:bmv3 -rpc-header namespace:seldon -insecure -proto ./prediction_service.proto -authority aiplatform-grpc.dev51.cbf.dev.paypalinc.com aiplatform-grpc.dev51.cbf.dev.paypalinc.com:443 tensorflow.serving.PredictionService/Predict

I got this response:

{
  "outputs": {
    "dense_3": {
      "dtype": "DT_FLOAT",
      "tensorShape": {
        "dim": [
          {
            "size": "1"
          },
          {
            "size": "1"
          }
        ]
      },
      "floatVal": [
        0.10923231
      ]
    }
  },
  "modelSpec": {
    "name": "bmv3",
    "version": "1578632718",
    "signatureName": "serving_default"
  }
}

But when I request in Java, I get this error: UNAVAILABLE: Network closed for unknown reason

After searching I got this: Trying to connect to an grpc-server in TLS mode using a PLAINTEXT client.

I'm not sure if this is the problem, how can I fix it?

This is my Java code:

try {

    Channel channel = ManagedChannelBuilder.forAddress("aiplatform-grpc.dev51.cbf.dev.paypalinc.com", 443).usePlaintext().build();

    PredictionServiceGrpc.PredictionServiceBlockingStub preStub = PredictionServiceGrpc.newBlockingStub(channel);

    response = preStub.predict(predictReq);

} catch (StatusRuntimeException e) {
    System.out.println("Call gRPC error, e:" + e.getMessage());
    return;
}

How should I modify to get the correct response in my Java code?


Solution

  • Try SSL mode instead of plaintext mode (with your certificate):

      private static final String ROOT_CERTIFICATE =
          """
              -----BEGIN CERTIFICATE-----
              ...........cert............
              -----END CERTIFICATE-----""";
    
        // Create server certificates.
        final Collection<X509Certificate> certificates;
        try (final InputStream inputStream =
            new ByteArrayInputStream(ROOT_CERTIFICATE.getBytes(StandardCharsets.UTF_8))) {
          //noinspection unchecked
          certificates =
              (Collection<X509Certificate>)
                  CertificateFactory.getInstance("X.509").generateCertificates(inputStream);
        }
        // Create grpc channel.
        final ManagedChannel channel =
            NettyChannelBuilder.forAddress("aiplatform-grpc.dev51.cbf.dev.paypalinc.com", 443)
               .sslContext(GrpcSslContexts.forClient().trustManager(certificates).build())
               .build();
        // create new stub and call your method