zoukankan      html  css  js  c++  java
  • [转载]Hessian构建分布式系统应用

    essian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。
    采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

    本文将分为以下几个内容:
    一、一个简单的Hessian调用例子
    二、Spring+Hessian的例子
    三、使我们的调用变得通用

    第一部分:一个简单的Hessian调用例子
    建立web工程HessianService。
    这里为了简单起见,我将远程接口类、接口实现类都定义在此web工程下。

    个人觉得一个比较好的做法是将web工程分开,此web工程只定义对外的接口并提供服务,而实现类新建一个Java工程存放。
    这样web工程依赖此Java工程,且客户端也依赖此Java工程。

    1) 远程接口类 ServiceRemote.java

    package com.al;
     
     import java.util.Map;
     
     @SuppressWarnings("unchecked")
    public interface ServiceRemote  {
         public Map callService(Map inputMap) ;
     }
    

     2) 实现类 Service.java

    package com.al;
     
     import java.util.HashMap;
     import java.util.Map;
     
     @SuppressWarnings("unchecked")
    public class Service implements ServiceRemote {
     
        public Map callService(Map inputMap) {
            if(inputMap == null) {
                 inputMap = new HashMap();
             }
             // do something
             // 
             inputMap.put("NAME", "Hessian");
             return inputMap;
         }
     }
    

     3) web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
     <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
         <servlet>
             <servlet-name>hessianService</servlet-name>
             <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
             <init-param>
                 <param-name>service-class</param-name>
                 <param-value>com.al.Service</param-value>
             </init-param>
         </servlet>
         <servlet-mapping>
             <servlet-name>hessianService</servlet-name>
             <url-pattern>/hessianService</url-pattern>
         </servlet-mapping>
     </web-app>
    

    经过以上3步,hessian服务部署算是完成了,在tomcat下发布。

    4) 调用方代码

    package com.ai.client;
     
     import com.al.ServiceRemote;
     import com.caucho.hessian.client.HessianProxyFactory;
     
    public class ClientTest {
        public static void main(String[] args) throws Exception {
             String url = "http://localhost:8080/HessianService/hessianService";
             HessianProxyFactory factory = new HessianProxyFactory();
             ServiceRemote rmt = (ServiceRemote) factory.create(ServiceRemote.class, url);
             System.out.println(rmt.callService(null));
         }
     }
    

    执行代码,结果如下:
    {NAME=Hessian}
    说明调用远程代码成功了。

    二、Spring+Hessian的例子
    1) web工程HessianService 的ServiceRemote 和Service类不变。对web.xml进行修改:

    <?xml version="1.0" encoding="UTF-8"?>
     
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation
    ="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
         
    <context-param>
             
    <param-name>contextConfigLocation</param-name>
             
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
         
    </context-param>
         
    <servlet>
             
    <servlet-name>dispatcher</servlet-name>
             
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             
    <load-on-startup>1</load-on-startup>
         
    </servlet>
         
    <servlet-mapping>
             
    <servlet-name>dispatcher</servlet-name>
             
    <url-pattern>/remote/*</url-pattern>
         
    </servlet-mapping>
         
    <welcome-file-list>
             
    <welcome-file>index.jsp</welcome-file>
         
    </welcome-file-list>
     
    </web-app>

     

    这里要注意的是:
    a)dispatcher-servlet.xml这个文件的命名。servlet配置为dispatcher,则此文件定义规则为****-servlet.xml.

    b)另外就是Spring和hessian的版本问题。
    spring版本是2.5.6,需要在此web工程下引入:spring-2.5.6.jar、spring-webmvc-2.5.6.jar、commons-logging-1.1.1.jar。
    hessian的版本是hessian-3.1.6.jar,hessian的低版本如 hessian-3.0.13和此Spring版本不合。(我试验了是配置不成功的。)

     

    2) dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
     
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
         
    <description>hessian server properties</description>
         
    <bean id="serviceImpl" class="com.al.Service" />
         
    <bean name="/service" class="org.springframework.remoting.caucho.HessianServiceExporter">
             
    <property name="service">
                 
    <ref bean="serviceImpl" />
             
    </property>
             
    <property name="serviceInterface">
                 
    <value>com.al.ServiceRemote</value>
             
    </property>
         
    </bean>
     
    </beans>

     

    3) 客户端代码可以保持原样不变,把调用hessian服务的url变换一下即可:
    http://localhost:8080/HessianService/remote/service
    http://IP:port/发布工程名/web.xml中配置的url-pattern/****-servlet.xml bean的ID。

     

    三、使我们的调用变得通用 将在下一篇中介绍。

     

     

  • 相关阅读:
    VSFTP配置参数详解
    C语言---函数
    ios 学习计划
    读书笔记---金融学一<新国富论>
    读书笔记---人生规划一<斯坦福最受欢迎的人生规划课、像卡耐基一样经营人生、九型人格>
    网络基础
    swift中构造方法和Kvc
    swift中的懒加载
    private的用法
    extension
  • 原文地址:https://www.cnblogs.com/licomeback/p/3522790.html
Copyright © 2011-2022 走看看