7

I have a gRPC service which I would just like to bind just to the localhost address. However, I don't see a way to do that in Java. Instead it will bind to all addresses.

Here is how I create my service now:

server = ServerBuilder.forPort(config.port())
                      .addService(new GRPCServiceImpl(serviceParams))
                      .build()
                      .start();
LOG.info("Server started, listening on " + config.port());

There doesn't seem to be an API exposed on ServerBuilder or Server which allows specifying the address or network interface. Whereas the C++ API has AddListentingPort.

So is there a way in Java to restrict which IP or interface the service listens on?

Dusty Campbell
  • 3,146
  • 31
  • 34

1 Answers1

15

You can do this by picking a more specific ServerBuilder, such as the NettyServerBuilder:

NettyServerBuilder.forAddress(new InetSocketAddress("localhost", config.port()))
    .addService(new GRPCServiceImpl(serviceParams))
    .build()
    .start();
Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37