登录
首页 >  Golang >  Go问答

如何为安全的 go grpc 服务创建不安全的 Java grpc cilent

来源:stackoverflow

时间:2024-04-14 23:54:33 152浏览 收藏

你在学习Golang相关的知识吗?本文《如何为安全的 go grpc 服务创建不安全的 Java grpc cilent》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我正在尝试用 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)

有人可以帮忙处理客户端代码吗


解决方案


就我而言,我终于找到了解决方案

  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 got idea from here
  1. 对于 tls 连接,这有效
channel  = nettychannelbuilder
                .foraddress("host",port)
               .sslcontext(grpcsslcontexts.forclient().trustmanager(insecuretrustmanagerfactory.instance).build())
                .build();
  1. 对于非 tls
channel  = nettychannelbuilder
            .foraddress("host",port).useplaintext()
       .build();
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();
        }
    }
}

理论要掌握,实操不能落!以上关于《如何为安全的 go grpc 服务创建不安全的 Java grpc cilent》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>