Java RmI原生的RPC。
PS: 继承Remote接口的接口方法必须手动抛出异常RemoteException,否则server都端启动会报错。
1. API端:
1.1 IHello.java
package com.xh.rmi.api;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IHello extends Remote {
String hello(String username) throws RemoteException;
UserDto setAge(UserDto userDto) throws RemoteException;
}
1.2 UserDto.java
package com.xh.rmi.api;
import java.io.Serializable;
public class UserDto implements Serializable {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserDto{" +
"username='" + username + '\'' +
", age=" + age +
'}';
}
}
2. Server端:
2.1 IHelloImpl.java
package com.xh.rmi.server;
import com.xh.rmi.api.IHello;
import com.xh.rmi.api.UserDto;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class IHelloImpl extends UnicastRemoteObject implements IHello {
public IHelloImpl() throws RemoteException {
super();
}
@Override
public String hello(String username) {
System.out.println("Connected Successfully!");
return String.format("你好, %s", username);
}
@Override
public UserDto setAge(UserDto userDto) throws RemoteException {
userDto.setAge(26);
return userDto;
}
}
2.2 ServerMain.java
package com.xh.rmi.server;
import com.xh.rmi.api.IHello;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class ServerMain {
public static void main(String[] args) throws RemoteException
, AlreadyBoundException, MalformedURLException {
IHello hello = new IHelloImpl();
LocateRegistry.createRegistry(8888);
System.setProperty("java.rmi.server.hostname", "127.0.0.1");
Naming.bind("rmi://localhost:8888/RHello", hello);
System.out.println("远程Hello对象绑定成功!");
}
}
3. Client端: ClientMain.java
package com.xh.rmi.client;
import com.xh.rmi.api.IHello;
import com.xh.rmi.api.UserDto;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class ClientMain {
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
IHello hello = (IHello) Naming.lookup("rmi://127.0.0.1:8888/RHello");
System.out.println(hello.hello("张三"));
UserDto userDto = new UserDto();
userDto.setUsername("黄雅");
UserDto dto = hello.setAge(userDto);
System.out.println(dto.toString());
}
}
4. 运行结果:
4.1 server
Connected to the target VM, address: '127.0.0.1:50548', transport: 'socket'
远程Hello对象绑定成功!
Connected Successfully!
Disconnected from the target VM, address: '127.0.0.1:50548', transport: 'socket'
4.2 client
Connected to the target VM, address: '127.0.0.1:50621', transport: 'socket'
你好, 张三
UserDto{username='黄雅', age=26}
Disconnected from the target VM, address: '127.0.0.1:50621', transport: 'socket'
5. 源码地址
https://github.com/xieyucan/rmi-demo