简体   繁体   English

在Java中将gRPC服务绑定到localhost

[英]Bind gRPC service to localhost in Java

I have a gRPC service which I would just like to bind just to the localhost address. 我有一个gRPC服务,我想将其仅绑定到本地主机地址。 However, I don't see a way to do that in Java. 但是,我没有用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. ServerBuilderServer上似乎没有公开允许指定地址或网络接口的API。 Whereas the C++ API has AddListentingPort . 而C ++ API具有AddListentingPort

So is there a way in Java to restrict which IP or interface the service listens on? 那么,Java中是否有一种方法可以限制服务侦听的IP或接口?

You can do this by picking a more specific ServerBuilder, such as the NettyServerBuilder: 您可以通过选择一个更特定的ServerBuilder(例如NettyServerBuilder)来实现:

NettyServerBuilder.forAddress(new InetSocketAddress("localhost", config.port()))
    .addService(new GRPCServiceImpl(serviceParams))
    .build()
    .start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM