发布Hessian服务作为服务内部基础服务

时间:2023-03-09 21:34:15
发布Hessian服务作为服务内部基础服务
摘要:Hessian经常作为服务内部RPC工具来使用,速度快效率高。重构代码的核心思想就是把共用的代码段提出来,使代码结构优化;架构设计类似,把基本的共用的服务提出来,使架构优化。下面讲述一下我在具体应用中使用Spring3.2.4在Tomcat7.0.47下发布Hessian4.0.37服务的过程。
关键词:Spring, Hessian, JAVA, 架构设计, Hibernate, Tomcat

Hessian经常作为服务内部RPC远程过程调用工具来使用,速度快效率高,我们的项目也不例外的使用了它。把一些基础的服务,被别的服务使用的服务,专门提出来,使用Hessian发布一个单独的服务,供别的服务使用。我发现这里的逻辑,设计理念与《重构》这本书里的思想如出一辙,重构代码的核心思想就是把共用的代码段提出来,采用优良的设计模式,来把代码结构优化。架构设计也是,把基本的共用的服务提出来,把架构优化。提出的服务当然要求性能很高,处理正确,运行稳定,Hessian是一个好的选择。
当然Netty和Mina也是常用的Java NIO框架,它们的性能同样优良,我们也可以利用它来发布服务,不过实现起来不如Hessian来的方便。因此,在工具的选择上,我选择了Hessian支持我们的系统架构。
Hessian的好处有很多,它可以支持C#客户端调用我们使用JAVA编写的服务,并且性能超群,传输速率很高。发布Hessian服务时,它也支持集成Spring,下面就讲述一下我在具体应用中使用Spring3.2.4在Tomcat7.0.47下发布Hessian4.0.37服务的过程。
一、将project发布成Hessian服务
首先在WEB-INF目录下配置web.xml文件,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>  
        <servlet-name>remoting</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
        <servlet-name>remoting</servlet-name>  
        <url-pattern>/</url-pattern>  
  </servlet-mapping> 

</web-app>

由于使用了Spring框架,还需要Spring相关的配置,这里在WEB-INF目录下添加remoting-servlet.xml文件。
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    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/context  
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                        http://www.springframework.org/schema/tx  
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
                        http://cxf.apache.org/jaxws   
                        http://cxf.apache.org/schemas/jaxws.xsd">            
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:configLocation="/WEB-INF/hibernate.cfg.xml">
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />    
    <context:component-scan base-package="com.wang.anqi.model" />
    <context:component-scan base-package="com.wang.anqi.dao.impl" />      
    <!-- 发布bean -->      
    <bean id="roleBLLImpl" class="com.wang.anqi.dataBll.impl.RoleBLLImpl" />       
    <bean name="/role-svc"  
            class="org.springframework.remoting.caucho.HessianServiceExporter"> 
            <property name="service" ref="roleBLLImpl" />  
            <property name="serviceInterface" value="com.wang.anqi.dataBll.RoleBLL" />  
    </bean>    
    <!-- 发布bean --> 
</beans>  
可以看到remoting-servlet.xml文件中引用了hibernate.cfg.xml文件,自然也要将hibernate.cfg.xml添加到WEB-INF目录下来,大家可以配合我的另一篇博客 关于项目中的DAL数据接入层架构设计 理解这里的Spring配置。添加相关的JAR包,将project打包成WAR包放在Tomcat的webapps目录下,启动Tomcat。
能配合Eclipse调试是最好不过了,你需要在Eclipse/Window/Preferences下的Server/Runtime Environments配置上你的Apache Tomcat v7.0,主要是把下载的apache-tomcat-7.0.47路径设好。然后去Window/Show View把Servers界面调出来,添加Tomcat v7.0 Server at localhost。File/New建立Dynamic Web Project,新建的Dynamic Web Project如果要使用其它的project里的代码,则右键它,打开Properties,在Deplyment Assembly中Add其它的project。右键它,点击Debug As/Debug on Server,就可以看到它在Tomcat v7.0 Server at localhost中运行了。可能会报错,记住把所有引用的jar包丢到apache-tomcat-7.0.47路径lib下,注意jar版本号不一致要去除。
启动成功后,在浏览器地址栏中输入:http://localhost/***HessianService/role-svc,如果在浏览器中出现的是 HTTP Status 405 - HessianServiceExporter only supports POST requests 而不是404便说明发布成功。
 
二、调用Hessian服务
那么其它的使用此基础服务的服务便可以以“客户"的形式来使用Hessian服务了,同样,在JAVA中调用Hessian服务也可以使用Spring,下面是spring.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" 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/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="roleBLLImpl"  
        class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
        <property name="serviceUrl">  
        <value>http://localhost/***HessianServcie/role-svc</value>  
        </property>  
        <property name="serviceInterface">  
            <value>com.wang.anqi.dataBll.RoleBLL</value>
        </property>

</bean> 
</beans> 

我们自己来管理这Spring上下文,RoleBLL的实例可以通过下面的代码:RoleBLL m_roleBLL = (RoleBLL) SpringServiceSupport.getContext().getBean("roleBLLImpl"); 来获取到。如此一来,就可以其它编写其它服务啦,真是带劲。
补充说一句,《重构》这本书里处处使用着测试驱动开发的思想,当然这对于充分保证代码逻辑的正确性是非常有好处的;类似的,把共用服务提取出来,作为基础服务,与不提出来代码、架构都变化许多,这时我们也需要有充分的保证,业务逻辑的正确性,毕竟是通过网络来RPC远程过程调用的,对象的网络传输还是有一定的风险的,不过从目前的使用来看,JAVA之间的远程过程调用一点问题都没有,我们也对每个接口写了网络调用的测试,开发简单,正确性高,性能优良!
Hessian的服务发布及使用倒是很简单,不简单的是这种系统架构设计的思想。《重构》是一本好书,值得我再读很多遍。
现期的任务真是很多啊,责任又很重大。
后台服务架构设计完了,JAVA服务编码、服务发布不要紧,要紧的是还要开发部分很久没写的C#前端,更要紧的是尽快开发完毕(我真是个厉害的角色发布Hessian服务作为服务内部基础服务),最要紧的是我来负责管理这个项目的开发进度,并且鼓舞大家开发的士气发布Hessian服务作为服务内部基础服务,有什么疑难杂症要不断的解决处理,还要带俩实习生。
做了不止两个PM的活,真是既当爹又当妈又当小孩,疼并快乐着,且码且珍惜啊!
加油,王安琪!发布Hessian服务作为服务内部基础服务

来自王安琪http://www.cnblogs.com/wgp13x/