spring+hessian

本文详细介绍了如何将Hessian与Spring框架结合使用,提供了一种高效、简便的数据传输方式,适用于对象间远程过程调用(RPC)。通过配置服务器端和客户端,实现了对象直接传递,避免了JSON转换,显著提高了传输速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 {
	
	@Override
	public 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.xml
			classpath:/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的方式来进行讲解。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值