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包裏面的客戶端去調用。恭喜,成功的報錯了。

因此當在兩臺主機之間調試的時候,會出如今同一臺主機客戶端和服務端可以保持通訊。而後將相同的代碼拷貝到另一臺主機,兩臺主機之間通訊的時候,就會出錯了,坑呀。

相關文章
相關標籤/搜索