サーバでは、RMIサーバjarファイルを使用してドッキング・コンテナを実行しています。 私はいくつかの異なる構成を試しましたが、私はちょうどそれを働かせることができません。 私のサーバーサイド:DockerコンテナのJava RMIサーバ
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");
}
}
私のクライアント:
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());
}
}
そして、彼らの両方を共有 "RmiServerIntf"
マイDockerfile:
RUN apt-getの "Ubuntuのイメージの更新" を10FROM ubuntu:latest
RUNエコーが& & apt-getを OpenJDKの-8-JRE \ -yインストール、更新 [1099 COPY RMIServer.jar /home/RMIServer.jar CMDを公開"ジャワ"、 "-jar"、 "/home/RMIServer.jar"]
私は私のコンテナを起動(申し訳ありませんが、それは文句を言わない右のそれをフォーマット): ドッキングウィンドウの実行--name rmitest -d -p 4023:1099 rmitestimage
クライアントが私をスロー:
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)
「LocateRegistry.getRegistry();」の実行が成功しても、ネットワーク操作を実行しないため、レジストリが存在することは証明されません。 'LocateRegistry.createRegistry()'を呼び出すと、その結果を静的変数に格納する必要があります。 – EJP