天天看點

【RPC架構Burlap】Spring內建Burlap

Burlap和Hessian同屬于codehaus的RPC調用架構,但是Burlap已經幾年不更新,是以Spring在4.0裡已經将Burlap的支援置為Deprecated,是以在選擇RPC架構時,不應該考慮Burlap了。

這篇文章還是記錄下Burlap的用法吧,主要是複制粘貼了Hessian與Spring內建一文,【RPC架構Hessian四】Hessian與Spring內建

Burlap和Hessian的共同點:

  • 基于HTTP協定
  • 簡單易上手,不需要定義複雜的接口描述檔案,如WSDL,IDL
  • 在RPC架構中,性能優越
  • Spring對它們一緻的內建,使得在Burlap和Hessian之間切換很容易,隻要修改Spring的暴露接口(Exporter)和遠端服務代理工廠類,就可以實作切換

Burlap和Hessian的不同點:

  • Burlap基于XML,Hessian基于二進制,這使得Burlap傳輸資料的可讀性優于Hessian
  • Hessian的性能稍微優于Burlap

在【RPC架構Hessian二】Hessian 對象序列化和反序列化一文中介紹了基于Hessian的RPC服務的實作步驟,在那裡使用Hessian提供的API完成基于Hessian的RPC服務開發和用戶端調用,本文使用Spring對Hessian的內建來實作Hessian的RPC調用。

定義模型、接口和伺服器端代碼

|---Model

        |--ComplexModel

        |--Person

        |--Point

|---Interface

        |--IComplexModelService

|---Service Implementation

        |--ComplexModelService

定義pom.xml

 添加對Spring、Hessian和Spring Remoting的支援

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hessian.project</artifactId>
        <groupId>com.tom</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>learn.hessian.spring.integration</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-remoting</artifactId>
            <version>2.0.8</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!--jetty plugin to manage embedded jetty-->
            <!--No goal  is specified-->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.7</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8668</port>
                            <maxIdleTime>30000</maxIdleTime>
                        </connector>
                    </connectors>
                    <webAppSourceDirectory>${project.basedir}/web
                    </webAppSourceDirectory>
                    <contextPath>/web</contextPath>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>      

定義web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-hessian-server.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
    </servlet-mapping>
</web-app>      

定義applicationContext-hessian-server.xml

<?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-2.0.xsd">
    <!-- 服務暴露 -->
    <!--BurlapServiceExporter代替HessianServiceExporter-->
    <bean name="/complexModelService" class="org.springframework.remoting.caucho.BurlapServiceExporter">
        <!-- 服務類 -->
        <property name="service">
            <bean id="complexModelService" class="com.tom.hessian.server.ComplexModelService"/>
        </property>
        <property name="serviceInterface">
            <value>
                com.tom.hessian.common.IComplexModelService
            </value>
        </property>
    </bean>

</beans>      

定義applicationContext-hessian-client.xml

<?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-2.0.xsd">
    <!--遠端對象代理工廠-->
    <!--BurlapProxyFactoryBean代替HessianProxyFactoryBean-->
    <bean name="complexModelService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
        <!--BurlapProxyFactoryBean沒有connectTimeout屬性-->
        <property name="connectTimeout" value="60000"/>
        <property name="serviceUrl" value="http://localhost:8668/web/hessian/complexModelService"/>
        <property name="serviceInterface" value="com.tom.hessian.common.IComplexModelService"/>
    </bean>

</beans>      

定義用戶端測試代碼

package com.tom.hessian.client;

import com.tom.hessian.common.ComplexModel;
import com.tom.hessian.common.IComplexModelService;
import com.tom.hessian.common.Person;
import com.tom.hessian.common.Point;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ComplextModelServiceSpringTest {

    public static void main(String[] args) throws Exception {
        ComplexModel<Point> model = new ComplexModel<Point>();
        model.setId(1);
        Person person = new Person();
        person.setName("Tom");
        person.setAge(86);
        person.setBirthDay(new Date());
        person.setSensitiveInformation("This should be private over the wire");
        model.setPerson(person);

        List<Point> points = new ArrayList<Point>();
        Point point = new Point();
        point.setX(3);
        point.setY(4);
        points.add(point);

        point = new Point();
        point.setX(100);
        point.setY(100);
        points.add(point);

        model.setPoints(points);


        //遠端方法調用
        ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext-hessian-client.xml");
        IComplexModelService service = cxt.getBean("complexModelService", IComplexModelService.class);
        service.save(model);
        model = service.read(model.getId());
        List<Point> points1 = model.getPoints();
        for(Point elem : points1) {
            System.out.println(elem.getX() + "\t" + elem.getY());
        }

    }

}
           

運作

啟動Jetty Server,然後運作上面的用戶端代碼,可以正常執行