Docker 容器中的 Java RMI 服务器

标签 java docker rmi dockerfile

在服务器中,我使用 RMI 服务器 jar 文件运行 docker 容器。 我尝试了几种不同的配置,但我就是无法让它工作。 我的服务器端:

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:

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"]

我启动我的容器:

docker run --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)

最佳答案

如果您从同一 JVM 的同一端口导出注册表和远程对象,您将克服端口问题。您不需要使用套接字工厂。

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);

关于Docker 容器中的 Java RMI 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43759967/

相关文章:

docker - 如何修复 : yum install with error in docker container?

java - 客户端的 RMI 可激活错误

java - Android 和运行 JVM 的服务器之间的动态代码加载

java - 使用 Java 和 JAI 对图像进行圆角处理

java - 如何将系统时区转换为用户时区

docker - 在构建系统中创建Docker容器的惯用策略是什么

Spring 3.0 RmiProxyFactoryBean : how to change serviceUrl at runtime?

java - 按继承层次结构解析

C# 与 Java?

mysql - NodeJS + Mysql 与 Docker Compose 2