第一种方式
服务端
import service.DemoService;
import service.impl.DemoServiceImpl;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* @author yjw
* @date 2020/5/18
*/
public class RpcServer {
public static void main(String[] args) {
try {
// 本地主机上的远程对象注册表Register的实例
Registry registry = LocateRegistry.createRegistry(1099);
// 创建一个远程接口对象
DemoService d = new DemoServiceImpl();
// 把远程接口对象绑定到RMI注册服务器上,命名为rmi://127.0.0.1:1099/Demo
registry.bind("rmi://127.0.0.1:1099/Demo", d);
System.out.println("=============== 启动RMI服务成功 ===============");
// 绑定完成后不能直接关闭,需要等待客户端接收数据
while (true) {}
} catch (RemoteException | AlreadyBoundException e) {
e.printStackTrace();
}
}
}
客户端
import service.DemoService;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* @author yjw
* @date 2020/5/18
*/
public class RpcClient {
public static void main(String[] args) {
try {
// 得到指定ip和端口的主机上的远程注册表的Register实例
Registry registry = LocateRegistry.getRegistry("127.0.0.1",1099);
// 从RMI服务器上拿到name的远程接口
DemoService demoService = (DemoService) registry.lookup("rmi://127.0.0.1:1099/Demo");
// 调用远程接口的方法
String xrj = demoService.getMsg("xrj");
System.out.println("==============> "+ xrj + "<==============");
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
}
第二种方式(Naming)
服务端
import service.DemoService;
import service.impl.DemoServiceImpl;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
/**
* @author yjw
* @date 2020/5/18
*/
public class RpcServer {
public static void main(String[] args) {
try {
// 本地主机上的远程对象注册表Register的实例
// 即使没有使用Register绑定远程接口对象也要写这个语句
LocateRegistry.createRegistry(1099);
// 创建一个远程接口对象
DemoService d = new DemoServiceImpl();
// 把远程接口对象绑定到RMI注册服务器上,命名为rmi://127.0.0.1:1099/Demo
Naming.bind("rmi://127.0.0.1:1099/Demo", d);
System.out.println("=============== 启动RMI服务成功 ===============");
// 绑定完成后不能直接关闭,需要等待客户端接收数据
while (true) {}
} catch (RemoteException | AlreadyBoundException | MalformedURLException e) {
e.printStackTrace();
}
}
}
客户端
import service.DemoService;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* @author yjw
* @date 2020/5/18
*/
public class RpcClient {
public static void main(String[] args) {
try {
// 从RMI服务器上拿到name的远程接口
DemoService demoService = (DemoService) Naming.lookup("rmi://127.0.0.1:1099/Demo");
// 调用远程接口的方法
String xrj = demoService.getMsg("xrj");
System.out.println("==============> "+ xrj + "<==============");
} catch (RemoteException | NotBoundException | MalformedURLException e) {
e.printStackTrace();
}
}
}
接口
package service;
import java.rmi.Remote;
/**
* @author yjw
*/
public interface DemoService extends Remote {
/**
* 获取Msg并返回Hello world+msg
* @param msg 字符串
* @return Hello world+msg
*/
String getMsg(String msg);
}
package service.impl;
import service.DemoService;
import java.io.Serializable;
/**
* @author yjw
* @date 2020/5/18
*/
public class DemoServiceImpl implements DemoService, Serializable {
@Override
public String getMsg(String msg) {
return "Hello World!" + msg;
}
}
执行结果
注意:
接口实现类要实现Serializable接口,否则会在解析时报错