无法绑定本地RMI注册表上的多个服务

时间:2016-07-13 19:54:49

标签: java rmi

我正在为学校作业构建这个软件。服务器(调节器)控制商店(分销商)和客户之间的交互。我有三个实体:

  • 调节器。它绑定了两个服务:身份验证(用户控制)和列表(列出经销商及其提议)到注册表。
  • 分销商。部署用户界面,允许注册和删除商品,并将服务(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.");

0 个答案:

没有答案