如何在Scala/java中通过本地套接字而不是inet创建GRPC服务 [英] How to create a GRPC service over a local socket rather then inet in scala/java

查看:127
本文介绍了如何在Scala/java中通过本地套接字而不是inet创建GRPC服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 GRPC 服务只能由本地计算机上的应用程序访问.

My GRPC service is only accessible to applications on the local machine.

我认为,如果客户端通过Unix域套接字而不是localhost:port进行连接,它将执行得更快.

I assume it would perform faster if the clients would connect over Unix domain socket, rather then localhost:port

我很想了解在这种情况下如何创建grpc服务,它应该能够在CentOS和Mac上运行

I'm tyring to understand how to create the grpc service in this case, it should be able to run on CentOS as well as on Mac

当前正在创建服务器,如下所示:

Currently creating the server like this:

val server: Server = ServerBuilder.forPort(port).addService(GreeterGrpc.bindService(new GrpcServer, ec)).build().start()

我也尝试过这样的配置:

I also tried configuration like this:

val server: Server = io.grpc.netty.NettyServerBuilder.forPort(port).addService(ProtoReflectionService.newInstance()).addService(GreeterGrpc.bindService(new GrpcServer, ec)).build().start()

但无法弄清楚如何绑定到本地套接字而不是本地主机

but couldn't figure out how to bind to local socket rather then localhost

推荐答案

localhost:port 应该基本上与Unix域套接字(UDS)一样快.UDS的主要用例是,在应用Unix文件权限的情况下,可以更好地控制权限和安全性.

localhost:port should be basically as fast as a Unix Domain Socket (UDS). The main use case of UDS is for better control of permissions and security, as Unix file permissions apply.

Java不支持UDS.因此,要使用UDS获取grpc-java,必须使用JNI组件,例如netty-transport-epoll或netty-transport-kqueue.

Java does not support UDS. So to get grpc-java using UDS you must use a JNI component like netty-transport-epoll or netty-transport-kqueue.

grpc-java不提供对UDS的现成支持,但您可以自己将各个部分组合在一起:

grpc-java does not provide out-of-the-box support for UDS, but you can combine the pieces together yourself:

// You are responsible for shutting down 'elg' and 'boss'. You
// may want to provide a ThreadFactory to use daemon threads.
EventLoopGroup elg = new EpollEventLoopGroup();

ManagedChannel chan = NettyChannelBuilder
    .forAddress(new DomainSocketAddress("filename"))
    .eventLoopGroup(elg)
    .channelType(EpollDomainSocketChannel.class)
    .build();

EventLoopGroup boss = new EpollEventLoopGroup(1);
Server serv = NettyServerBuilder
    .forAddress(new DomainSocketAddress("filename"))
    .bossEventLoopGroup(boss)
    .workerEventLoopGroup(elg)
    .channelType(EpollServerDomainSocketChannel.class)
    .build();

老板"事件循环组的唯一工作是 accept()新连接.如果新连接的速率较低,则可以将 elg 用于 bossEventLoopGroup().

The "boss" event loop group's only job is to accept() new connections. If the rate of new connections is low, you can use elg for bossEventLoopGroup().

您应该在 Channel s和 Server s之间尽可能多地共享 elg .

You should share elg as much as possible across Channels and Servers.

Epoll在OS X上不可用,因此您需要改用kqueue.对于kqueue,请使用 KQueueEventLoop KQueueDomainSocketChannel KQueueServerDomainSocketChannel 而不是其epoll副本.

Epoll isn't available on OS X, so you would need to use kqueue instead. For kqueue use KQueueEventLoop, KQueueDomainSocketChannel, and KQueueServerDomainSocketChannel instead of their epoll counterparts.

这篇关于如何在Scala/java中通过本地套接字而不是inet创建GRPC服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆