• java RPC系列之一 rmi


    java RPC系列之一    rmi

    一、java RPC简单的汇总

      java的RPC得到技术,基本包含以下几个,分别是:RMI(远程方法调用) 、Caucho的Hessian 和 Burlap 、Spring的基于HTTP的远程服务、以及使用JAX-RPC和JAX-WS的Web服务。本文主要介绍一下RMI的基本的配置实现,当然,是基于Spring集成的。后面会继续使用Spring的HTTP的方式实现远程调用实现。

    二、RMI的基于Spring的配置实现

      基本步骤:

          1.定义好服务端需要提供的接口方法(客户端调用的接口)

          2.定义好服务端的实现代码

          3.使用spring配置服务端,发布服务到制定端口

          4.使用spring配置客户端的代理bean

          5.定义好服务端和客户端调用的测试代码。

    三、RMI的基于Spring的配置实现的代码如下:

          1.定义好IRmiService 接口类,服务端和客户端都需要的接口类。

    package com.lilin.maven.service.rmi;
    
    /**
     * @author lilin
     * 
     */
    public interface IRmiService {
        /**
         * 
         * @param name
         * @return
         */
        String sayHello(String name);
    }

      2.定义好服务端的实际实现代码RmiService。

    package com.lilin.maven.service.rmi;
    
    /**
     * 
     * @author lilin
     * 
     */
    public class RmiService implements IRmiService {
    
        @Override
        public String sayHello(String name) {
            return "Hello " + name;
        }
    
    }

      3.使用spring的配置rmi服务,配置rmi-server.xml文件,发布rmi服务到制定的1199端口。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="rmiService" class="com.lilin.maven.service.rmi.RmiService" />
        <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
            <property name="serviceName" value="rmiService" />
            <property name="service" ref="rmiService" />
            <property name="serviceInterface" value="com.lilin.maven.service.rmi.IRmiService" />
            <property name="registryPort" value="1199" />
        </bean>
    </beans>

      4.使用spring配置客户端的代理bean,配置rmi-client.xml,把远程的服务方法配置为bean,可以方便注入。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="rmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
            <property name="serviceUrl" value="rmi://localhost:1199/greetService" />
            <property name="serviceInterface" value="com.lilin.maven.service.rmi.IRmiService" />
        </bean>
    </beans>

          5.定义好服务端和客户端调用的测试代码,首先启动服务端,发布服务,然后启动测试端代码调用远程服务。

         服务端:启动server后,可以看到如下信息:

    信息: Looking for RMI registry at port '1199'
    2016-3-27 23:48:38 org.springframework.remoting.rmi.RmiServiceExporter prepare
    信息: Binding service 'rmiService' to RMI registry: RegistryImpl_Stub[UnicastRef [liveRef: [endpoint:[192.168.1.105:1199](remote),objID:[0:0:0, 0]]]]

    则表示当前的发布服务成功!

    package com.lilin.maven.service.rmi;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author lilin
     * 
     */
    public class Server {
        public static void main(String[] args) {
            new ClassPathXmlApplicationContext("/config/spring/rmi-server.xml");
        }
    }

           客户端:获取client配置的bean,直接调用远程服务方法,可以得到正确结果如下:

    package com.lilin.maven.service.rmi;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author lilin
     * 
     */
    public class Client {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "config/spring/rmi-client.xml");
            IRmiService service = context.getBean("rmiService", IRmiService.class);
            System.out.println(service.sayHello("李林"));
        }
    
    }

    测试结果如下:

    信息: Loading XML bean definitions from class path resource [config/spring/rmi-client.xml]
    2016-3-27 23:48:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e13d52: defining beans [rmiService]; root of factory hierarchy
    Hello 李林

    到此简单的rmi远程服务调用,基于spring的配置就全部结束了,值得注意的是:

    rmi使用场景:不考虑网络限制(防火墙),访问/发布基于java的服务。参见的是spring in action 第三版

     

     

  • 相关阅读:
    一个小厂算法工程师的2021个人年终总结
    优达学城 UdaCity 纳米学位
    Eclipse 常用可视化开发插件
    Android创建文件夹和文件
    Windows Mobile 播放声音文件
    C++实现顺序栈类
    c++实现的图类
    常见的字符串操作
    常见的链表操作
    取余数法实现哈希表(包括开放定址法和链地址法解决冲突)
  • 原文地址:https://www.cnblogs.com/lilin0719/p/5327526.html
Copyright © 2020-2023  润新知