(二)spring 远程服务之Hessian

Hessian
Hessian 和 RMI 一样,可以让你调用远程服务像调用本地对象一样简单方便。不同的是,Hessian 是将对象以中性的二进制消息使用HTTP进行传送,而不像RMI使用java的序列化格式,由于二进制消息是中性的,因此不受限于编程语言所实现的客户端或移动端。 

1.首先我们还是创建一个spring_test作为服务端,spring_client作为客户端。
2.在spring_test中,创建自己的api:

其中,我们也只需看HessianDemo.java和HessianDemoImpl.java,HessianDemo.java是接口,HessianDemoImpl.java是实现类:
代码如下:
HessianDemo.java:
public interface HessianDemo {
	public String work();
}
HessianDemoImpl.java:
public class HessianDemoImpl implements HessianDemo {

	@Override
	public String work() {
		return "我是远程服务HessionDemoImpl-->work()";
	}

}
3.配置spring配置文件: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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:cache="http://www.springframework.org/schema/cache"
	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.0.xsd
						http://www.springframework.org/schema/cache
						http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 实现服务 -->
	<bean id="hessianDemoImpl" class="com.zhong.myspring.server.HessianDemoImpl" ></bean>
	<!-- 开发的接口 -->
	<bean id="serviceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter" >
		<!-- 接口服务 -->
		<property name="service">
			<ref bean="hessianDemoImpl"/>
		</property>
		<!-- 接口 -->
		<property name="serviceInterface">
			<value>com.zhong.myspring.server.HessianDemo</value>
		</property>
	</bean>
	<!-- 配置访问路径 -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
		<property name="mappings">
			<props>
				<prop key="/hessianService">serviceExporter</prop>
			</props>
		</property>
	</bean>
</beans>
配好之后加上
可以启动tomcat开启远程服务了。
4.在客户端spring_client调用服务,工程目录如下:

配置: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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:cache="http://www.springframework.org/schema/cache"
	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.0.xsd
						http://www.springframework.org/schema/cache
						http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 接口服务代理 -->
	<bean id="hessianServiceProxy" class="org.springframework.remoting.caucho.HessianProxyFactoryBean" >
		<!-- 接口路径 -->
		<property name="serviceUrl">
			<value>http://localhost:8080/spring_test/hessianService</value>
		</property>
		<!-- 接口 -->
		<property name="serviceInterface">
			<value>com.zhong.myspring.server.HessianDemo</value>
		</property>
	</bean>
</beans>
调用:RMIClient.java:
public static void test_hession(){context = new ClassPathXmlApplicationContext("hessian-client.xml");
		HessianDemo hessianDemo = (HessianDemo) context.getBean("hessianServiceProxy");
		System.out.println("start");
		System.out.println("work()+"+hessianDemo.work());
		System.out.println("end");
	}
	public static void main(String[] args) {
		test_hession();
	}
结果分析:
start
work()+我是远程服务HessionDemoImpl-->work()
end
=============================================
至此调用成功。大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值