Java 无法与远程服务器上的RMI注册表建立连接
目前,我正在尝试在远程机器上使用RMI启动java服务器。服务器似乎启动了,但我无法在客户机上调用它的方法。 在客户端上,我得到一个异常,如下所示:Java 无法与远程服务器上的RMI注册表建立连接,java,azure,client-server,rmi,firewall,Java,Azure,Client Server,Rmi,Firewall,目前,我正在尝试在远程机器上使用RMI启动java服务器。服务器似乎启动了,但我无法在客户机上调用它的方法。 在客户端上,我得到一个异常,如下所示: Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.0.0.6; nested exception is: java.net.ConnectException: Operation timed out (Connectio
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.0.0.6; nested exception is:
java.net.ConnectException: Operation timed out (Connection timed out)
这是我的RMI服务器构造函数:
private RmiServer(String address, int port) throws RemoteException, SQLException, MalformedURLException {
System.out.println("This address = " + address + ", Port = " + port);
Registry registry;
try {
registry = LocateRegistry.createRegistry(PORT);
} catch (RemoteException e)
{
registry = LocateRegistry.getRegistry(PORT);
}
System.setProperty("java.rmi.server.hostname", address);
try {
registry.bind(REGISTRY_NAME, this);
Naming.bind(address, this);
}
catch (AlreadyBoundException e)
{
registry.rebind(REGISTRY_NAME, this);
Naming.rebind(address, this);
}
...
}
这是我的客户代码:
static public void main(String[] args) throws IOException {
try {
Registry registry = LocateRegistry.getRegistry(SERVER_ADDRESS, SERVER_PORT);
rmiServer = (ReceiveMessageInterface) registry.lookup(REGISTRY_NAME);
} catch (RemoteException | NotBoundException e) {
System.err.println(e.getMessage());
isServerOK = false;
}
Scanner reader = new Scanner(new InputStreamReader(System.in));
boolean isAuthenticated = false;
int pin;
String cardNumber;
SessionInfo session = null;
if (isServerOK) {
while (true) {
if (!isAuthenticated) {
System.out.println("Welcome Customer to HDFC Bank ATM : \nPlease enter your card number: ");
cardNumber = reader.next();
System.out.println("Please enter your PIN: ");
pin = reader.nextInt();
String cardHolderName = rmiServer.verifyPIN(cardNumber, pin);
...
}
...
}
...
}
...
}
我使用以下字符串启动服务器:
java -cp flyway-core-6.0.8.jar:mssql-jdbc-7.4.1.jre8.jar:mysql-connector-java-8.0.18.jar:. -Djava.security.policy=java.security.AllPermission kmalfa.RmiServer
我的服务器在linux虚拟机上运行。我使用Microsoft Azure服务。我关闭了客户端和虚拟机上的防火墙。出于测试目的,我允许Azure设置中的所有传入连接,但这没有帮助。
可能是什么问题?已解决
我不得不打电话
System.setProperty("java.rmi.server.hostname", address);
不是在RmiServer构造函数中,而是在调用它之前,在main的开头,唯一可能的解决方案是设置我的代码库,但我不确定如何正确设置它。。。也许,有人能给我详细解释一下吗?不,不是。您应该删除安全管理器。你不需要它。这个问题是RMI常见问题解答中的A.1项。@user207421我试图使用RMI常见问题解答A.1项中的解决方案,但没有帮助我。我仍然得到异常。如果您仍然得到相同的异常,并且显示了相同的IP地址,则表明您没有正确执行该操作。@user207421您能解释一下我需要如何绑定注册表吗?我更新了问题中的代码。我是否正确设置了注册表?我需要如何在服务器上使用Naming.bind,在客户端上使用Naming.lookup?