JAVA RMI远程调用实现

JAVA RMI远程调用实现

#创建RMI Server

1.创建Java实体类,实现序列化

package com.hp.beans;

import java.io.Serializable;

public class user implements Serializable {
    private Integer uid;
    private String uanem;
    private String upassword;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUanem() {
        return uanem;
    }

    public void setUanem(String uanem) {
        this.uanem = uanem;
    }

    public String getUpassword() {
        return upassword;
    }

    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }

    @Override
    public String toString() {
        return "user{" +
                "uid=" + uid +
                ", uanem='" + uanem + '\'' +
                ", upassword='" + upassword + '\'' +
                '}';
    }
}

2.创建调用接口,集成Remote类,接口方法需要抛出remote异常

package com.hp.interfaces;

import com.hp.beans.user;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface userInterface extends Remote {
    public String getUserName() throws RemoteException;
    public user getAdminAccount() throws RemoteException;

}

3.创建接口实现类,实现接口方法

package com.hp.userimpl;

import com.hp.beans.user;
import com.hp.interfaces.userInterface;

import java.rmi.RemoteException;

public class userImpls implements userInterface {
    @Override
    public String getUserName() throws RemoteException {
        return "你好";
    }

    @Override
    public user getAdminAccount() throws RemoteException {

        user user=new user();
        user.setUid(1);
        user.setUanem("tom");
        user.setUpassword("123456");
        return user;
    }
}

4.main程序入口

import com.hp.interfaces.userInterface;
import com.hp.userimpl.userImpls;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Main {
    public static void main(String[] args) throws IOException {
        userImpls u = new userImpls();
        userInterface ui= (userInterface) UnicastRemoteObject.exportObject(u,0);
        Registry lo = LocateRegistry.createRegistry(2003);
        lo.rebind("a",ui);
        System.out.println("server is ready!");
    }
}

#创建RMI Client

 

1.创建RMI Server 的实体类和接口的jar包

  将实体类与接口类导出为一个jar包,加入到client项目中

2.main程序入口调用server

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Main {
    public static void main(String[] args) throws RemoteException, NotBoundException {
        Registry re = LocateRegistry.getRegistry("localhost", 2003);
        com.hp.interfaces.userInterface userInterface = (com.hp.interfaces.userInterface) re.lookup("a");
        System.out.println(userInterface.getAdminAccount());
    }
}

#测试

user{uid=1, uanem='tom', upassword='123456'}

Process finished with exit code 0

 

 

 

没有更多推荐了,返回首页