我正在为学校作业构建这个软件。服务器(调节器)控制商店(分销商)和客户之间的交互。我有三个实体:
ServiceSales
)绑定到注册表。监管机构将这两项服务都绑定得很好,其他实体也可以与之互动。当我试图绑定ServiceSales
时,我得到一个错误,好像注册表没有启动。
这是课程Regulator
中的相关代码:
Registry registry = LocateRegistry.getRegistry();
Utils.setCodeBase(ServiceSalesInterface.class);
ServiceSalesInterface sales = new ServiceSalesImpl(servicio);
ServicioVentaInterface sales_remote =
(ServiceSalesInterface) UnicastRemoteObject.exportObject(sales, 8888);
registry.rebind(sales.name(), sales_remote);
sales.name()
返回服务名称。
这是Utils
来源:
public class Utils {
public static final String CODEBASE = "java.rmi.server.codebase";
public static void setCodeBase(Class<?> c){
String path = c.getProtectionDomain().getCodeSource()
.getLocation().toString();
String cbase = System.getProperty(CODEBASE);
if (path != null && !path.isEmpty()){
path = path + " " + cbase;
}
System.setProperty(CODEBASE, path);
}
}
我尝试过使用不同的端口,但似乎并不是导致错误的原因。这是我尝试将ServiceSales
绑定到注册表(registry.rebind(sales.name(), sales_remote);
)时弹出的错误消息:
java.rmi.ConnectException:Connection拒绝主机:127.0.1.1;嵌套异常是: java.net.ConnectException:连接被拒绝 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.newCall(UnicastRef.java:342) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
如果需要,这是Regulator
类中的相关代码(工作正常)。
System.setProperty("java.rmi.server.hostname","127.0.0.1");
registry = LocateRegistry.createRegistry(8888);
Utils.setCodeBase(ServiceGoods.class);
ServiceGoodsInterface goods = new ServiceGoodsImpl();
ServiceGoodsInterface goods_remote =
(ServiceGoodsInterface)UnicastRemoteObject.exportObject(goods, 8888);
registry.rebind("ServiceGoods", goods_remote);
Utils.setCodeBase(ServiceAuth.class);
ServiceAuthInterface auth = new ServiceAuthImpl();
ServiceAuthInterface auth_remote =
(ServiceAuthInterface)UnicastRemoteObject.exportObject(auth, 8888);
registry.rebind("ServiceAuth", auth_remote);
System.out.println("Server is running.");