Docker容器中的Java RMI服务器 [英] Java RMI Server in Docker Container

查看:451
本文介绍了Docker容器中的Java RMI服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

我的客户:

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

And they both share "RmiServerIntf"

我的Dockerfile:

My Dockerfile:

FROM ubuntu:latest

运行echo更新ubuntu映像
运行apt-get update& & apt-get install -y \
openjdk-8-jre
EXPOSE 1099
复制RMIServer.jar /home/RMIServer.jar
CMD [ java, -jar , /home/RMIServer.jar]

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)

我用以下命令启动容器:
docker run --name rmitest -d -p 4023:1099 rmitestimage

I start my container with: 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将注册表和远程对象导出到同一端口,则将克服端口问题。您不需要使用套接字工厂。

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

这篇关于Docker容器中的Java RMI服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆