Rmi简单实现和总结

Rmi能够简单实现多台主机之间的通讯,好比说一台主机控制另一台主机执行某些任务或者获取某些信息。java

简单实现:ide

客户端会向服务端传输可序列化的对象,服务端也会向客户端返回可序列化的对象。this

Client.java 客户端,获取远程绑定的对象,调用远程对象的方法。spa

package com.peach;
 
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
 
public class Client {
    public static void main(String args[]){
        try {
            Person person = new Person("taozi", 24);
            System.out.println(callRemoteMethod(person));
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
 
    public static Response callRemoteMethod(Person person) throws RemoteException, NotBoundException, MalformedURLException {
        RemoteMethod remoteMethod = (RemoteMethod) Naming.lookup("rmi://localhost:8888/remoteMethodCall");
        return remoteMethod.getRequestMes(person);
    }
}

Person.java 封装客户端向服务端传输的信息。.net

package com.peach;
 
 
import java.io.Serializable;
 
public class Person implements Serializable{
 
    private static final long serialVersionUID = -9222957077010307936L;
    private final String name;
    private final int age;
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public int getAge() {
        return age;
    }
}

RemoteMethod.java 远程调用对象,能够接收客户端的调用请求和参数信息。调试

package com.peach;
 
import java.rmi.Remote;
import java.rmi.RemoteException;
 
public interface RemoteMethod extends Remote {
 
    public Response getRequestMes(Person person) throws RemoteException;
 
}

RemoteMethodImpl.java 远程调用对象实现。code

package com.peach;
 
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
 
public class RemoteMethodImpl extends UnicastRemoteObject implements RemoteMethod {
 
 
    protected RemoteMethodImpl() throws RemoteException {
    }
 
    @Override
    public Response getRequestMes(Person person) {
        String msg = String.format("name:%s   age:%d", person.getName(), person.getAge());
        return new Response(200, "success", msg);
    }
 
}

Response.java 用于封装远程对象方法的返回信息orm

package com.peach;
 
 
import java.io.Serializable;
 
public class Response implements Serializable {
 
    private static final long serialVersionUID = 5346658075279106897L;
    private final int code;
    private final String status;
    private final String message;
 
    public Response(int code, String status, String message) {
        this.code = code;
        this.status = status;
        this.message = message;
    }
 
    public int getCode() {
        return code;
    }
 
    public String getStatus() {
        return status;
    }
 
    public String getMessage() {
        return message;
    }
 
    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", status='" + status + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}

Server.java 服务端 用于创建注册表,绑定远程对象。server

package com.peach;
 
 
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
 
public class Server {
    public static void main(String args[]) {
        try {
            RemoteMethod remoteMethod = new RemoteMethodImpl();
            LocateRegistry.createRegistry(8888);
            Naming.bind("rmi://localhost:8888/remoteMethodCall", remoteMethod);
            System.out.println("INFO:remote object bind success!");
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

执行的时候先执行服务端代码,成功以后再执行客户端代码。可以成功获得response的响应信息。以下:对象

总结:

若是须要在另一个工程中使用客户端的代码,记得使远程对象保持一致,包括包路

若是我启动的是peach包里面的服务,我用peach2包里面的客户端去调用。恭喜,成功的报错了。

因此当在两台主机之间调试的时候,会出如今同一台主机客户端和服务端可以保持通讯。而后将相同的代码拷贝到另一台主机,两台主机之间通讯的时候,就会出错了,坑呀。

相关文章
相关标签/搜索