标签:java rmi 远程方法调用 调用远程对象 分布式计算
RMI(Remote Method Invocation)中文名称是远程方法调用,可用于分布式计算。
这里就不去详细介绍RMI了,本Blog主要讲叙RMI实战和有哪些需要注意的地方,如果想要查看详细介绍请查看:百度百科RMI
RMI分为服务端和客户端
LocateRegistry.createRegistry(端口);
Naming.rebind("rmi://IP地址:端口/RMI服务名称",RMI服务对象);//rebind也可以用bind替代
示例:
LocateRegistry.createRegistry(10010);
Naming.rebind("rmi://127.0.0.1:10010/Test", new TestServer());
public interface TestServerInterface extends Remote {
String getString() throws RemoteException;
Student getStudent(String name) throws RemoteException;
}
public class TestServer extends UnicastRemoteObject implements TestServerInterface {
public TestServer() throws RemoteException {
super();
}
@Override
public String getString() throws RemoteException {
return System.currentTimeMillis()+":有客户端调用了此方法";
}
@Override
public Student getStudent(String name) throws RemoteException {
return new Student(name+System.currentTimeMillis());
}
}
RMI服务对象的接口 server= (RMI服务对象的接口) Naming.lookup("rmi://IP地址:端口/RMI服务名称");
返回值 result=server.你要调用的方法(可能需要传入的参数);
示例:
TestServerInterface serverInterface= (TestServerInterface) Naming.lookup("rmi://localhost:10010/Test");
System.out.println(">>>" + serverInterface.getString());
System.out.println(">>>" + serverInterface.getStudent("name1"));
1.可以和服务端定义的接口方法不一致,服务端有的这边可以没有,服务端没有的这边可以有,但调用时会报错
2.无须继承Remote,继承也行不过没意义
3.接口方法无须声明抛出RemoteException,声明抛出也行不过没意义
4.一般来说和服务端保持一致即可
示例:
public interface TestServerInterface {
String getString();
Student getStudent();
}
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString(){
return name;
}
}
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString(){
return name;
}
}
public class Student implements Serializable {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString(){
return name;
}
}
public class Student implements Serializable {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString(){
return name;
}
}
项目图:
package test;
import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class RmiServerMain {
public static void main(String[] args) throws Exception {
LocateRegistry.createRegistry(10010);
Naming.bind("rmi://127.0.0.1:10010/Test", new TestServer());
}
}
interface TestServerInterface extends Remote {
String getString() throws RemoteException;
Student getStudent(String name) throws RemoteException;
}
class TestServer extends UnicastRemoteObject implements TestServerInterface {
private static final long serialVersionUID = 1L;
public TestServer() throws RemoteException {
super();
}
@Override
public String getString() throws RemoteException {
return System.currentTimeMillis() + ":有客户端调用了此方法";
}
@Override
public Student getStudent(String name) throws RemoteException {
return new Student(name + System.currentTimeMillis());
}
}
class Student implements Serializable {
private static final long serialVersionUID = 110L;
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return getClass().getSimpleName() + ":name=" + name;
}
}
package test;
import java.io.Serializable;
import java.rmi.Naming;
public class RmiClientMain {
public static void main(String[] args) throws Exception {
TestServerInterface serverInterface = (TestServerInterface) Naming.lookup("rmi://127.0.0.1:10010/Test");
System.out.println(">>>" + serverInterface.getString());
System.out.println(">>>" + serverInterface.getStudent("StuName"));
}
}
/**
* 客户端的服务接口,不是必须继承Remote,并且方法不是必须申明抛出RemoteException<br>
* 但包名、类名必须要和服务端完全一致。<br>
* 方法可以删掉、可以乱加,但乱加的方法你只要不调用就行,以调用就报错。<br>
* 方法的结构不同改,如返回类型,参数类型。
*
* @author Tang
*
*/
interface TestServerInterface {
String getString();
Student getStudent(String name);
}
class Student implements Serializable {
/**
* serialVersionUID变量必须和服务端保持一致。<br>
* 如果服务端没有定义此变量,客户端这里也必须不要定义此变量<br>
* 如果服务端有定义此变量,客户端这里也必须要定义此变量,并且变量的值也要相同
*/
private static final long serialVersionUID = 110L;
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return getClass().getSimpleName() + ":name=" + name;
}
}
标签:java rmi 远程方法调用 调用远程对象 分布式计算
原文地址:http://blog.csdn.net/u012643122/article/details/46490467