spring+hessian

来源:互联网 发布:软件配置管理工具 编辑:程序博客网 时间:2024/05/01 23:22

Spring 集成hessian

hessian作为一个基于rpc的接口服务,使用起来特别方便。在之前都是使用webservice或者http+json进行数据传输。但是webservice传输过程都是字符串传输,速度上面不大理想,而且需要进行一系列的xml之类的封装,用起来稍显麻烦。后来改用http+json传输。速度快,使用也方便,但是在使用过程中,需要自己编写传输过程,同时对于数据在发送方和接收方都需要进行json与对象的转换。

      hessian基于http进行传输,速度很快,据说速度比webservice快了十倍左右,同时支持对象传递,不再需要进行对象的转换处理了。

      在经过一番了解后。果断使用spring+hessian系统之间的交互。本文例子是基于maven的环境进行相关配置。

  服务器端编写

     pom.xml配置

    <!-- 添加hessian支持 --> <dependency><groupId>com.caucho</groupId><artifactId>hessian</artifactId><version>4.0.38</version> </dependency>

     web.xml 配置

        在web.xml添加如下代码

    <!-- hessian拦截 --><servlet><servlet-name>hessian</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>hessian</servlet-name><url-pattern>/hessian/*</url-pattern></servlet-mapping> 

   hessian-servlet.xml 配置

      主要进行hessianService服务的配置,也就是我们要提供出来的接口定义。 个人还是比较喜欢这种把服务接口定义配置在xml中,方便查看。hessian-servlet可以直接扔在web.xml同级目录下。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <!-- 接口的具体实现类 -->      <bean id="impl" class="com.example.hessian.IsayImpl" />          <!-- 使用Spring的HessianServie做代理 -->      <bean name="/helloHessian"    class="org.springframework.remoting.caucho.HessianServiceExporter">          <!-- service引用具体的实现实体Bean-->          <property name="service" ref="impl" />          <property name="serviceInterface" value="com.example.hessian.Isay" />      </bean>            <!-- 可以配置多个HessianServiceExporter代理Bean -->  </beans>


package com.example.hessian;public interface Isay {//业务方法public Hello sayHello(String age,String name);}


package com.example.hessian;public class IsayImpl implements Isay {@Overridepublic Hello sayHello(String age,String name) {Hello hl=new Hello();hl.setAge(age);hl.setName(name);return hl;}}


package com.example.hessian;import java.io.Serializable;public class Hello implements Serializable{/** *  */private static final long serialVersionUID = 1L;private String age;private String name;public Hello() {super();}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
javabean Hello为进行传递的对象,要实现序列化。  以上配置完成,启动容器即可。


纯hessin客户端编写

package com.example.hessian.client;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;import com.example.hessian.Hello;import com.example.hessian.Isay;public class HessianClient {public static void main(String[] args) throws MalformedURLException {//客户端调用  example为项目名称  hessian为服务器端web.xml配置的拦截servlet名称,helloHessian为hessian-servlet里面配置的接口服务名称 String url = "http://localhost:8080/example/hessian/helloHessian";HessianProxyFactory factory = new HessianProxyFactory();Isay api = (Isay) factory.create(Isay.class, url);//业务调用Hello hello=api.sayHello("25","jacky");//输出返回结果System.out.println(hello.getAge());System.out.println(hello.getName());}}


hessian跟spring的集成过程中,非常简单,对于已有的业务类讲,服务器端基本不用什么编码,只需要通过hessian-servlet把服务暴露出来即可。同时对象可以直接进行传递,不需要进行json的转换。


Spring 集成客户端编写

添加hessian-client.xml ,配置调用的服务地址和服务接口

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>         <!-- hessian 客户端调用 -->        <bean id="hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">       <property name="serviceUrl">           <value>           http://localhost:8080/SpringMvcSp/hessian/helloHessian           </value>       </property>       <property name="serviceInterface">           <value>com.example.hessian.Isay</value>       </property>    </bean></beans>

在web.xml中,hessian-client.xml添加到web.xml中

 <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/application.xmlclasspath:/hessian-client.xml</param-value></context-param>

调用代码
package com.example.hessian.client;import java.net.MalformedURLException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.example.hessian.Hello;import com.example.hessian.Isay;import com.example.hessian.token.TokenUtil;public class SpringHessianClient {public static void main(String[] args) throws MalformedURLException {ApplicationContext contex = new ClassPathXmlApplicationContext(                  "hessian-client.xml");            // 获得客户端的Hessian代理工厂bean          Isay api = (Isay) contex.getBean("hessianService");    Hello  hl=api.sayHello("25", "jacky");  //输出返回结果System.out.println(hl.getAge());System.out.println(hl.getName());}}


不过在实际运用中,对于接口的调用可能会存在一些安全的问题,如何对hessian进行安全的保障,防止恶意的调用,在下一篇中将采用token的方式来进行讲解。





0 0