java RPC系列之一 rmi

java RPC系列之一    rmijava

1、java RPC簡單的彙總spring

  java的RPC獲得技術,基本包含如下幾個,分別是:RMI(遠程方法調用) 、Caucho的Hessian 和 Burlap 、Spring的基於HTTP的遠程服務、以及使用JAX-RPC和JAX-WS的Web服務。本文主要介紹一下RMI的基本的配置實現,固然,是基於Spring集成的。後面會繼續使用Spring的HTTP的方式實現遠程調用實現。
網絡

2、RMI的基於Spring的配置實現maven

  基本步驟:ide

      1.定義好服務端須要提供的接口方法(客戶端調用的接口)測試

      2.定義好服務端的實現代碼url

      3.使用spring配置服務端,發佈服務到制定端口spa

      4.使用spring配置客戶端的代理bean3d

      5.定義好服務端和客戶端調用的測試代碼。代理

3、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 第三版

相關文章
相關標籤/搜索