如何通过本地套接字而不是在 Scala/java 中的 inet 创建 GRPC 服务

Avn*_*arr 1 scala unix-socket netty grpc

我的GRPC服务只能被本地机器上的应用程序访问。

我认为如果客户端通过 Unix 域套接字而不是 localhost:port 连接,它的执行速度会更快

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

目前正在创建这样的服务器:

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

我也尝试过这样的配置:

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

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

Eri*_*son 6

localhost:port应该基本上和 Unix 域套接字 (UDS) 一样快。UDS 的主要用例是更好地控制权限和安全性,因为 Unix 文件权限适用。

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

grpc-java 不为 UDS 提供开箱即用的支持,但您可以自己将这些部分组合在一起:

// 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();
Run Code Online (Sandbox Code Playgroud)

“老板”事件循环组的唯一工作是建立accept()新的连接。如果新连接的速率较低,您可以使用elgfor bossEventLoopGroup()

您应该elgChannels 和Servers之间尽可能多地共享。

Epoll 在 OS X 上不可用,因此您需要改用 kqueue。对于 kqueue 使用KQueueEventLoop, KQueueDomainSocketChannel, andKQueueServerDomainSocketChannel而不是它们的 epoll 对应物。