Force RMIService to bind with specific IP address

2019-06-11 17:03发布

问题:

I implemented RMIService using Spring RMIServiceExporter running at port 4099. When I start my Jboss , RMI Service is getting bonded to 0.0.0.0:4099 instead of 127.0.0.1:4099. I tried different parameters

-Djava.rmi.server.hostname=127.0.0.1
-Djava.rmi.server.hostname=localhost  
-Djava.rmi.server.hostname=localhost.localdomain along with
-Djava.net.preferIPv4Stack=true parameter. 
Started Jboss with -b 127.0.0.1

Its always getting bonded to 0.0.0.0:4099. I check it using

>netstat -anp | grep 4099

>TCP    0.0.0.0:4099           0.0.0.0:0              LISTENING

Here is my implementation:

    RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
    rmiServiceExporter.setService(this);
    rmiServiceExporter.setServiceName("MyService");
    rmiServiceExporter.setServiceInterface(MyServiceManager.class);
    rmiServiceExporter.setRegistryPort(4099);
    rmiServiceExporter.afterPropertiesSet();

回答1:

  1. java.rmi.server.hostname has nothing to do with setting the bind-address, and I'm not sure it has anything to do with Spring RMI either. It is a feature of the java.rmi package.

  2. The listening port shown in the netstat output is the Registry port, not necessarily the port your server is listening on.

It appears you will have to define your own RMIServerSocketFactory that creates ServerSockets bound as you wish.



回答2:

I assumed that to access RMI from another server, RMI service should be bonded as 127.0.0.1:4099 or 192.168.29.8:4099. I had this issue in the Linux server and it turns out that I have to open up the ports in "iptables".