码迷,mamicode.com
首页 > 编程语言 > 详细

Java——RMI

时间:2016-11-18 23:22:59      阅读:564      评论:0      收藏:0      [点我收藏+]

标签:异常   object   err   gravity   操作   balance   lock   user   远程   


      之前分布式系统调用用的是比较老的EJB,当时还是作为服务调用方,去调用别的系统的服务。最近发现新公司里面,用的是RMI,查了下发现EJB的底层实现就是RMI,也算是熟悉了。。。


 一,使用JDK 中的RMI实现服务发布和引用


服务端接口:

/**
 * Created by LiuHuiChao on 2016/11/18.
 */
public interface UserInfoService extends Remote{
    /**
     * 定义远程接口,必须继承Remote接口,
     * 其中所有需要远程调用的方法都必须抛出RemoteException异常
     */
    String getUserName(int id) throws RemoteException;
}


服务端接口实现:

/**
 * Created by LiuHuiChao on 2016/11/18.
 */
public class UserInfoServiceImpl extends UnicastRemoteObject implements UserInfoService{

    /**
     * 接口的实现类同时要实现Serializable接口,这里继承UnicastRemoteObject也是间接实现Serializable接口,
     * 同时,因为构造方法需要抛出RemoteException,所以不能缺省使用隐含的无参构造方法,而应该自己显式定义构造方法。
     * @throws RemoteException
     */
    protected UserInfoServiceImpl() throws RemoteException {
    }

    @Override
    public String getUserName(int id) throws RemoteException {
        return "漠漠水田飞白鹭";
    }
}


单元测试服务启动与客户端调用:


**
 * Created by LiuHuiChao on 2016/11/18.
 */
public class JdkRmiServiceClientTest {

    /**
     * 使用Java 原生JDK实现RMI调用:服务端
     * @throws RemoteException
     * @throws AlreadyBoundException
     * @throws MalformedURLException
     */
    @Test
    public void startServer() throws RemoteException, AlreadyBoundException, MalformedURLException, InterruptedException {

        Object lock = new Object();
        synchronized (lock) {

            UserInfoService userInfoService=new UserInfoServiceImpl();//创建一个远程对象
            //生成远程对象注册表Registry的实例,并指定端口为8888(默认端口是1099)
            LocateRegistry.createRegistry(8888);
            //把远程对象注册到RMI服务器上,名称为UserInfoService
            ////绑定的URL标准格式为:rmi://host:port/name(协议名可以省略)
            Naming.bind("rmi://127.0.0.1:8888/UserInfoService",userInfoService);

            lock.wait();
        }
    }

    @Test
    public  void startClient() throws RemoteException, NotBoundException, MalformedURLException {
        // 在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法
        UserInfoService userInfoService=(UserInfoService)Naming.lookup("rmi://127.0.0.1:8888/UserInfoService");
        System.out.println(userInfoService.getUserName(1));
    }
}


 在使用中,一般是服务端把接口打包成Jar,之后客户端在pom里面配置依赖这个jar,就能使用服务端接口了。


二,Spring集成RMI


  为了方便服务发布与调用,可以使用Spring进行集成,我们只需编写普通POJO类就能实现;


服务端接口:

/**
 * Created by LiuHuiChao on 2016/11/18.
 */
public interface AccountService {
    int queryBalance(String mobileNo);
    String shoopingPayment(String mobileNo, byte protocol);
}

服务端实现类:

/**
 * Created by LiuHuiChao on 2016/11/18.
 */
public class MobileAccountServiceImpl implements AccountService  {

    public int queryBalance(String mobileNo) {
        if (mobileNo != null)
            return 100;
        return 0;
    }

    public String shoopingPayment(String mobileNo, byte protocol) {
       return mobileNo;
    }
}


服务端的Spring配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="serviceName" value="MobileAccountService"/><!--服务名称-->
		<property name="service" ref="accountService"/> <!--配置注入的实现类-->
		<property name="serviceInterface"
				  value="test.Rmi.AccountService" />
		<property name="registryPort" value="8089" /> <!--调用的端口-->
		<property name="servicePort" value="8088" />
	</bean>

	<bean id="accountService" class="test.Rmi.MobileAccountServiceImpl" />

</beans>


在测试代码中启动服务端:


/**
 * Created by LiuHuiChao on 2016/11/18.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext-test-service-rmi.xml"})
public class StartServerTest {

    @Test
    public void startServerTest() throws InterruptedException {
        Object lock = new Object();
        synchronized (lock) {
            lock.wait();
        }
    }
}


客户端Spring的配置:


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://127.0.0.1:8089/MobileAccountService" /> <!--调用的RMI服务的地址-->
		<property name="serviceInterface"
				  value="test.Rmi.AccountService" /> <!--调用的服务的接口-->
		<property name="refreshStubOnConnectFailure" value="true"/> <!--解决重启 rmi 的服务器后会出现拒绝连接或找不到服务对象的错误-->
		<!--
			 stub查询的另一个问题是,目标RMI服务器和RMI注册项在查询时要为可用的。如果客户端在服务器启动之前,
			 尝试查询和缓存该服务stub,那么客户端的启动将会失败(即使还不需要该服务)。
			 为了能够惰性查询服务stub,设定RmiProxyFactoryBean的lookupStubOnStarup标志为false。
			 然后在第一次访问 时查询该stub,也就是说,当代理上的第一个方法被调用的时候去主动查询stub,
			 同时被缓存。这也有一个缺点,就是直到第一次调用,否则无法确认目标 服务是否实际存在。
		-->
		<property name="lookupStubOnStartup" value="false"/>
	</bean>


</beans>


客户端调用代码:


/**
 * Created by LiuHuiChao on 2016/11/18.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext-test-client-rmi.xml"})
public class StartClientTest {

    @Resource
    AccountService accountService;

    @Test
    public void testRmiAtClient(){
        String result = accountService.shoopingPayment("123333456357654", (byte) 5);
        System.out.println(result);
    }

}



三,RMI原理


技术分享



     RMI应用程序通常包括两个独立的程序:服务器程序和客户机程序。典型的服务器应用程序将创建多个远程对象,使这些远程对象能够被引用,然后等待客户机调用这些远程对象的方法。而典型的客户机程序则从服务器中得到一个或多个远程对象的引用,然后调用远程对象的方法。RMI为服务器和客户机进行通信和信息传递提供了一种机制。


     在与远程对象的通信过程中,RMI使用标准机制:stub和skeleton。远程对象的stub担当远程对象的客户本地代表或代理人角色。调用程序将调用本地stub的方法,而本地stub将负责执行对远程对象的方法调用。在RMI中,远程对象的stub与该远程对象所实现的远程接口集相同。调用stub的方法时将执行下列操作:

       (1) 初始化与包含远程对象的远程虚拟机的连接;

       (2) 对远程虚拟机的参数进行编组(写入并传输);

       (3) 等待方法调用结果;

       (4) 解编(读取)返回值或返回的异常;

       (5) 将值返回给调用程序。


    为了向调用程序展示比较简单的调用机制,stub将参数的序列化和网络级通信等细节隐藏了起来。在远程虚拟机中,每个远程对象都可以有相应的skeleton(在JDK1.2环境中无需使用skeleton)。Skeleton负责将调用分配给实际的远程对象实现。它在接收方法调用时执行下列操作:(1) 解编(读取)远程方法的参数;(2) 调用实际远程对象实现上的方法;(3) 将结果(返回值或异常)编组(写入并传输)给调用程序。stub和skeleton由rmic编译器生成。

     方法调用从客户对象经占位程序(Stub)、远程引用层(RemoteReference Layer)和传输层(Transport Layer)向下,传递给主机,然后再次经传输层,向上穿过远程调用层和骨干网(Skeleton),到达服务器对象。 占位程序扮演着远程服务器对象的代理的角色,使该对象可被客户激活。远程引用层处理语义、管理单一或多重对象的通信,决定调用是应发往一个服务器还是多个。传输层管理实际的连接,并且追追踪可以接受方法调用的远程对象。服务器端的骨干网完成对服务器对象实际的方法调用,并获取返回值。返回值向下经远程引用层、服务器端的传输层传递回客户端,再向上经传输层和远程调用层返回。最后,占位程序获得返回值。


四,RMI与RPC



 1,语言差异:RMI只支持Java系统间调用,而RPC支持异构系统调用;

 2,如何被调用的:

        在RMI中,远程接口使每个远程方法都具有方法签名。如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新方法就不能被RMI客户方所调用。


        在RPC中,当一个请求到达RPC服务器时,这个请求就包含了一个参数集和一个文本值,通常形成“classname.methodname”的形式。这就向RPC服务器表明,被请求的方法在为“classname”的类中,名叫“methodname”。然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入。这里的参数类型是与RPC请求中的类型是匹配的。一旦匹配成功,这个方法就被调用了,其结果被编码后返回客户方。


3,传递信息直接的差异


     在RMI中,远程接口使每个远程方法都具有方法签名。如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新方法就不能被RMI客户方所调用。

 

     在RPC中,当一个请求到达RPC服务器时,这个请求就包含了一个参数集和一个文本值,通常形成“classname.methodname”的形式。这就向RPC服务器表明,被请求的方法在为“classname”的类中,名叫“methodname”。然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入。这里的参数类型是与RPC请求中的类型是匹配的。一旦匹配成功,这个方法就被调用了,其结果被编码后返回客户方。




     




Java——RMI

标签:异常   object   err   gravity   操作   balance   lock   user   远程   

原文地址:http://blog.csdn.net/lhc1105/article/details/53223721

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!