java中的rmi
rmi 是 java中的一种rpc实现 ,提供了一种机制来实现分布式应用的通信和调用远程方法的能力.
rmi的独特优势
- rmi是java 特定的远程调用机制,它利用java的强类型和面向对象的特性,支持参数和返回值的序列化
- rmi提供了对象的远程引用,使得客户端可以像本地对象一样调用远程对象的方法,而不需要编写底层的通信代码
- rmi提供了透明的远程对象传输,客户不需要关系网络通信细节,就可以调用远程方法
- rmi支持动态类加载,允许在运行时动态下载和加载远程对象的类定义
测试案例
rmi 服务器端案例
package cn.demo.rmi.server;
import cn.demo.rmi.pojo.TestBean;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteInterface extends Remote {
public String sayHello() throws RemoteException;
String test2(String msg) throws RemoteException;
String test3(TestBean testBean) throws RemoteException;
}
package cn.demo.rmi.server;
import cn.demo.rmi.pojo.TestBean;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
public class RemoteService extends UnicastRemoteObject implements RemoteInterface {
public RemoteService() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
System.err.println("====>sayHello invoke time"+new Date());
return "hello from remote object!";
}
@Override
public String test2(String msg) throws RemoteException {
System.err.println("====>test2 invoke time"+new Date());
return "rec: "+msg;
}
@Override
public String test3(TestBean testBean) throws RemoteException {
System.err.println("====>test3 invoke time"+new Date());
return testBean.toString();
}
}
package cn.demo.rmi.server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* rmi 是 java中的一种rpc实现 ,提供了一种机制来实现分布式应用的通信和调用远程方法的能力
*
* rmi的独特优势
* --rmi是java 特定的远程调用机制,它利用java的强类型和面向对象的特性,支持参数和返回值的序列化
* --rmi提供了对象的远程引用,使得客户端可以像本地对象一样调用远程对象的方法,而不需要编写底层的通信代码
* --rmi提供了透明的远程对象传输,客户不需要关系网络通信细节,就可以调用远程方法
* --rmi支持动态类加载,允许在运行时动态下载和加载远程对象的类定义
*/
public class Server {
public static void main(String[] args) {
try {
//创建rmi注册表实例
Registry registry = LocateRegistry.createRegistry(1099);
//注册远程对象
RemoteService remoteService = new RemoteService();
registry.rebind("RemoteService", remoteService);
System.out.println("server is running...");
}catch (Exception e){
e.printStackTrace();
}
}
}
传输的java对象
package cn.demo.rmi.pojo;
import java.io.Serializable;
public class TestBean implements Serializable {
private String name;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return "TestBean{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
rmi客户端代码案例
package cn.demo.rmi.cli;
import cn.demo.rmi.pojo.TestBean;
import cn.demo.rmi.server.RemoteInterface;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
//查找rmi注册表中的远程对象引用
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
RemoteInterface remoteObject = (RemoteInterface) registry.lookup("RemoteService");
//调用远程方法
String s = remoteObject.sayHello();
System.out.println(s);
String r21r21 = remoteObject.test2("r21r21");
System.out.println(r21r21);
TestBean t = new TestBean();
t.setId(123L);
t.setName("r23rde");
String s1 = remoteObject.test3(t);
System.out.println(s1);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
}