RMI——hello world程序

来源:互联网 发布:雷洋之死 知乎 编辑:程序博客网 时间:2024/05/16 03:30

RMI——hello world程序

RMI作为分布式的基础之一,主要用来解决的问题是两个jvm进程间通讯的问题。RMI可以用作提供统一的服务接口和实现,方面各个系统间统一。

好,下面上货。
RMI分为服务端和客户端。下面我们先看一下服务端。
一、服务端。
1、服务端的目录结构截图:


2、MyRemote接口

package com.xueyou.demo.rmiserver.service;import com.xueyou.demo.rmiserver.dao.Student;import java.rmi.Remote;import java.rmi.RemoteException;public interface MyRemote extends Remote {    public String sayHello() throws RemoteException;    public Student sayStudent() throws RemoteException;}

3、MyRemoteImpl类
package com.xueyou.demo.rmiserver.serviceImpl;import com.xueyou.demo.rmiserver.dao.Student;import com.xueyou.demo.rmiserver.service.MyRemote;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {    public String sayHello() throws RemoteException {        return "hello world RMI";    }    public MyRemoteImpl() throws RemoteException {    }    public Student sayStudent() throws RemoteException {        return new Student("xiaoming", 10);    }}

4、Student实体类。这里需要注意的是实体类需要实现serializeble接口。
package com.xueyou.demo.rmiserver.dao;import java.io.Serializable;public class Student implements Serializable {    private String name;    private int age;    public Student() {    }    public Student(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

5、启动服务类
package com.xueyou.demo.rmiserver;import com.xueyou.demo.rmiserver.service.MyRemote;import com.xueyou.demo.rmiserver.serviceImpl.MyRemoteImpl;import java.net.MalformedURLException;import java.rmi.AlreadyBoundException;import java.rmi.Naming;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;public class App {    public static void main(String[] args) {        try {            MyRemote service = new MyRemoteImpl();            LocateRegistry.createRegistry(9191);            try {                Naming.bind("rmi://192.168.0.113:9191/RemoteHello", service);            } catch (AlreadyBoundException e) {                e.printStackTrace();            }        } catch (RemoteException e) {            e.printStackTrace();        } catch (MalformedURLException e) {            e.printStackTrace();        }        System.out.println("success publish");    }}

二、客户端
首先看一下客户端的目录结构。需要注意的是,客户端的目录结构中,要调用的接口的包名和结构需要和服务端一致。这里一定要注意了,因为如果借口的包路径不一致的话,会出现ClassNotFoundException。
如图:


1、其他文件和服务端一致,这里只介绍如何调用RMI
package com.xueyou.demo.rmiclient;import com.xueyou.demo.rmiserver.dao.Student;import com.xueyou.demo.rmiserver.service.MyRemote;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;public class App {    public static void main(String[] args) {        System.out.println("begin use RMI");        try {            MyRemote service = (MyRemote) Naming.lookup("rmi://192.168.0.113:9191/RemoteHello");            String s = service.sayHello();            Student student = service.sayStudent();            System.out.println(s);            System.out.println(student.getName());            System.out.println(student.getAge());        } catch (NotBoundException e) {            e.printStackTrace();        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (RemoteException e) {            e.printStackTrace();        }    }}


运行截图: