Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 1.16 KB

RMI.md

File metadata and controls

60 lines (43 loc) · 1.16 KB

rmi

  • 接口继承java.rmi.Remote
  • 实现类继承java.rmi.server.UnicastRemoteObject

简单demo

public interface HelloService extends Remote {

    /**
     * @return hello
     */
    String hello(String msg) throws RemoteException;

}
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {

    public HelloServiceImpl() throws RemoteException {
        super();
    }

    public String hello(String msg) throws RemoteException {
        return "msg:" + msg;
    }
}
public class Server {

    public static void main(String[] args) throws Exception {
        HelloService service = new HelloServiceImpl();
        // 注册中心?
        Registry registry = LocateRegistry.createRegistry(1099);
        Naming.rebind("rmi://192.168.1.215/hello", service);
        System.out.println("服务启动成功");

    }

}
public class Client {

    public static void main(String[] args) throws Exception {
        HelloService helloService = (HelloService) Naming.lookup("rmi://localhost/hello");
        String msg = helloService.hello("msg");
        System.out.println(msg);
    }

}