问题
In a server i am running my docker container with an RMI-server jar file. I have tried several different configurations but i just cant get it working. My Serverside:
public class Main extends UnicastRemoteObject implements RmiServerIntf {
public static final String MESSAGE = "Hello World from docker in RMI";
public Main() throws RemoteException {
super(0); // required to avoid the 'rmic' step, see below
}
public String getMessage() {
return MESSAGE;
}
public static void main(String args[]) throws Exception {
System.out.println("RMI server started");
System.setProperty("java.rmi.server.hostname", "<host-ip-address>");
try { //special exception handler for registry creation
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created.");
} catch (RemoteException e) {
LocateRegistry.getRegistry();
System.out.println("java RMI registry already exists.");
}
//Instantiate RmiServer
Main obj = new Main();
// Bind this object instance to the name "RmiServer"
Naming.rebind("RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
}
My Client:
public class Main {
public Main() {
}
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
RmiServerIntf obj = (RmiServerIntf) Naming.lookup("rmi://<my-host-address>:4023/RmiServer");
System.out.println(obj.getMessage());
}
}
And they both share "RmiServerIntf"
My Dockerfile:
FROM ubuntu:latest
RUN echo "Updating ubuntu image" RUN apt-get update && apt-get install -y \ openjdk-8-jre EXPOSE 1099 COPY RMIServer.jar /home/RMIServer.jar CMD ["java", "-jar", "/home/RMIServer.jar"]
(Sorry, it wont format it right)
I start my container with: docker run --name rmitest -d -p 4023:1099 rmitestimage
The client throws me :
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: <my-host-address>; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:130)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
at com.sun.proxy.$Proxy0.getMessage(Unknown Source)
at com.company.Main.main(Main.java:19)
回答1:
If you export the Registry and your remote object on the same port from the same JVM you will overcome your port problem. You don't need to use a socket factory.
static Registry registry;
// ...
registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
// ...
public Main() throws RemoteException
{
super(Registry.REGISTRY_PORT);
// ...
}
// ....
Main main = new Main();
registry.rebind("RmiServer", main);
回答2:
It have come to my knowledge that rmi only uses the rmiregistry-port for initializing the connection, and the actual data-transfer is happening on random ports.
Since docker only allows connections to ports you have explicit linked to the host-machine, the initialization of the RMI-server side is happening, but the actual data-transfer from method-invokation is "blocked". This problem should be possible to overcome with the help of a custom RMI socket factory. Will return with answer if I succeed.
来源:https://stackoverflow.com/questions/43759967/java-rmi-server-in-docker-container