我正在尝试在Java在服务器位于goLang并使用https部署的地方创建grpc服务客户端。我试图建立不安全的连接的地方[我不想通过证书]
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)
有人可以提供客户代码帮助
答案 0 :(得分:1)
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();
}
}
}
答案 1 :(得分:1)
可能是您的代码中的错误。我的代码运行良好,请克隆它。
答案 2 :(得分:1)
对于我来说,我终于找到了解决方案
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。最终 ||| netty-tcnative-boringssl-static版本2.0.22.Final got idea from here
channel = NettyChannelBuilder
.forAddress("host",port)
.sslContext(GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build())
.build();
channel = NettyChannelBuilder
.forAddress("host",port).usePlaintext()
.build();