Grpc-java: How to connect to unix socket?

Created on 10 Mar 2016  路  2Comments  路  Source: grpc/grpc-java

I'm using this code snippet to connect to gRPC server though a unix socket:

ManagedChannel channel = ManagedChannelBuilder.forTarget("unix:/tmp/imageresizer.socket")
        .usePlaintext(true)
        .build();

but I'm getting this exception: java.lang.IllegalArgumentException: cannot find a NameResolver for unix:/tmp/imageresizer.socket

Most helpful comment

Got it! Thanks!
I've ended up with (for future generations):

ManagedChannel channel = NettyChannelBuilder.forAddress(new DomainSocketAddress("/tmp/imageresizer.socket"))
        .eventLoopGroup(new EpollEventLoopGroup())
        .channelType(EpollDomainSocketChannel.class)
        .usePlaintext(true)
        .build();

and in pom.xml

<dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-transport-native-epoll</artifactId>
      <version>4.1.0.CR1</version>
      <classifier>${os.detected.classifier}</classifier>
</dependency>

there should also be os-maven-plugin configured

All 2 comments

grpc-java doesn't provide native unix domain socket support. However, you can provide a io.netty.channel.unix.DomainSocketAddress to NettyChannelBuilder.forAddress() and also override the eventLoopGroup() to be an io.netty.channel.epoll.EpollEventLoopGroup and the channelType() to io.netty.channel.epoll.EpollSocketChannel.

If you have trouble, you can try reference our benchmark, but it may be a bit hard to follow.

Got it! Thanks!
I've ended up with (for future generations):

ManagedChannel channel = NettyChannelBuilder.forAddress(new DomainSocketAddress("/tmp/imageresizer.socket"))
        .eventLoopGroup(new EpollEventLoopGroup())
        .channelType(EpollDomainSocketChannel.class)
        .usePlaintext(true)
        .build();

and in pom.xml

<dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-transport-native-epoll</artifactId>
      <version>4.1.0.CR1</version>
      <classifier>${os.detected.classifier}</classifier>
</dependency>

there should also be os-maven-plugin configured

Was this page helpful?
0 / 5 - 0 ratings