最近,Java 课程设计要求实现一个ATM 系统,我做的架构设计是 RMI 的 C/S。
在部署的时候出了点问题。。。。
我的 Server 是部署在 Linux (Ubuntu 7.04)上的,而 Client 是部署在 Windows XP 上,在做测试的时候 Client 总是报异常。
这里先给出 Server 的主程序代码:
package cn.edu.ynu.sei.atm.server;
import cn.edu.ynu.sei.atm.loginProcess.AvailableAccount;
import cn.edu.ynu.sei.atm.loginProcess.Verification;
import cn.edu.ynu.sei.atm.transactions.Transaction;
import cn.edu.ynu.sei.atm.account.CurrentAccount;
import cn.edu.ynu.sei.atm.account.DepositAccount;
import cn.edu.ynu.sei.atm.account.CreditAccount;
import java.rmi.Naming;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
/**
* ATM服务端<br>
* 该类提供了ATM系统的基本服务对象
* @author 88250
*/
public class ATMServer
{
/**
* 服务端程序入口点
* @param args 命令行参数,这里为<code>null</code>
*/
public static void main(String[] args)
{
try
{
Verification verification= new Verification();
AvailableAccount availableAccountInfo = new AvailableAccount();
Transaction transaction = new Transaction();
CurrentAccount currentAccount = new CurrentAccount();
DepositAccount depositAccount = new DepositAccount();
CreditAccount creditAccount = new CreditAccount();
System.setProperty("java.rmi.server.hostname", "192.168.110.87");
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://192.168.110.87:1099/CurrentAccount", currentAccount);
System.out.println("CurrentAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/DepositAccount", depositAccount);
System.out.println("DepositAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/CreditAccount", creditAccount);
System.out.println("CreditAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/Transaction", transaction);
System.out.println("Transaction ready!");
Naming.bind("rmi://192.168.110.87:1099/Verification", verification);
System.out.println("Verification ready!");
Naming.bind("rmi://192.168.110.87:1099/AvailableAccount", availableAccountInfo);
System.out.println("AvailableAccount ready!");
System.out.print("ATM services ready....");
}
catch (RemoteException re)
{
re.printStackTrace();
}
catch (AlreadyBoundException abe)
{
abe.printStackTrace();
}
catch (IOException ioE)
{
ioE.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import cn.edu.ynu.sei.atm.loginProcess.AvailableAccount;
import cn.edu.ynu.sei.atm.loginProcess.Verification;
import cn.edu.ynu.sei.atm.transactions.Transaction;
import cn.edu.ynu.sei.atm.account.CurrentAccount;
import cn.edu.ynu.sei.atm.account.DepositAccount;
import cn.edu.ynu.sei.atm.account.CreditAccount;
import java.rmi.Naming;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
/**
* ATM服务端<br>
* 该类提供了ATM系统的基本服务对象
* @author 88250
*/
public class ATMServer
{
/**
* 服务端程序入口点
* @param args 命令行参数,这里为<code>null</code>
*/
public static void main(String[] args)
{
try
{
Verification verification= new Verification();
AvailableAccount availableAccountInfo = new AvailableAccount();
Transaction transaction = new Transaction();
CurrentAccount currentAccount = new CurrentAccount();
DepositAccount depositAccount = new DepositAccount();
CreditAccount creditAccount = new CreditAccount();
System.setProperty("java.rmi.server.hostname", "192.168.110.87");
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://192.168.110.87:1099/CurrentAccount", currentAccount);
System.out.println("CurrentAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/DepositAccount", depositAccount);
System.out.println("DepositAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/CreditAccount", creditAccount);
System.out.println("CreditAccount ready!");
Naming.bind("rmi://192.168.110.87:1099/Transaction", transaction);
System.out.println("Transaction ready!");
Naming.bind("rmi://192.168.110.87:1099/Verification", verification);
System.out.println("Verification ready!");
Naming.bind("rmi://192.168.110.87:1099/AvailableAccount", availableAccountInfo);
System.out.println("AvailableAccount ready!");
System.out.print("ATM services ready....");
}
catch (RemoteException re)
{
re.printStackTrace();
}
catch (AlreadyBoundException abe)
{
abe.printStackTrace();
}
catch (IOException ioE)
{
ioE.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
看上去没什么问题,可是Client总说是服务被拒绝127.0.0.1。
后来发现是Server端的网络设置有问题。。。。
在Linux下,RMI的部署总是首先被映射在localhose,即127.0.0.1。
下面是解决方法:
编辑hosts文件。
sudo gedit /etc/hosts
192.168.110.87 daniel-laptop #即 IP hostname
P.S. 听说在Windows下部署Server不会出现上述问题。。。。