我的第一个Hessian服务

本文介绍如何使用Spring和Hessian技术搭建Web服务。主要内容包括:所需Spring包的引入、定义服务接口、配置applicationContext.xml及web.xml文件等关键步骤。

开了账号这么多年了,今天第一次来这写东西。我今天要说说的写的第一个hessian服务。

 

这次我用的是spring+hessian来做的web服务。

 

首先建立这样一个服务要引入srping的一些包,主要有core,web,remoting还有一个重要的包叫aop。

就是因为没有这个包让的原本写好的程序莫名其妙的报错,找了半天才找到的。

 

首先发布服务我们要对外做一个接口:下面是代码

public interface IHelloHessian {
	public String SayHello();
}

有了接口你总得通过接口做一些事吧,下面我们来做事

public interface IHelloHessian {
	public String SayHello();
}

 

事是做完了。可是外部还是不能调用啊,spring要做的大量的工作就是配置,接下来我们来置applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="helloHessian" class="com.kane.HelloHessian">
	</bean>

</beans>

 

这完了以后,你要向外中指定用那个吧,那就再来一个对外用的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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 业务类 -->  
	<bean id="hessianService" class="com.kane.HelloHessian"/>  
	           
	<!-- 远程服务 -->  
	<bean name="/helloHessian" class="org.springframework.remoting.caucho.HessianServiceExporter">  
	    <property name="service" ref="helloHessian"/>  
	    <property name="serviceInterface">  
	        <value>  
	            com.kane.IHelloHessian  
	        </value>  
	    </property>  
	</bean> 
</beans> 

 这好了,spring + hessian是通过servlet拦截实现的,那现在我们就要配web.xml文件了。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name> 
		<param-value>
			/WEB-INF/config/applicationContext.xml
		</param-value>   
	</context-param>
	<listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
	<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>  
</web-app>

 

到现在为止你可以项目放到你的服务器上进行发布了。

 

我的文章到也就写完了。希望大家不要走弯路了。

 

 

Twisted 是一个用于编写异步网络服务器和客户端的 Python 框架,支持多种协议,包括 HTTP、FTP、SSH 等。虽然 Twisted 本身并未直接提供 Hessian 协议的内置实现,但可以通过第三方库或自定义协议的方式在 Twisted 中使用 HessianHessian 是一种轻量级的二进制 Web 服务协议,由 Caucho Technology 开发,常用于远程过程调用(RPC)。它支持跨语言通信,常用于 Java 和其他语言之间的服务交互。Python 社区中存在一些 Hessian 实现,如 `hessianlib` 或 `pyhessian`,可以与 Twisted 配合使用以构建异步 RPC 服务或客户端。 在 Twisted 中使用 Hessian 协议的一种常见方式是借助 `txhessian` 这样的第三方库,它为 Twisted 提供了 Hessian 协议的支持。通过 `txhessian`,可以实现基于 Hessian 的客户端和服务端通信。例如,使用 `txhessian` 调用远程服务的方法如下: ```python from twisted.internet import reactor, defer from txhessian.client import HessianProxy @defer.inlineCallbacks def call_remote_service(): service = HessianProxy("http://localhost:8080/hessian/TestService") result = yield service.sayHello("World") print(result) reactor.stop() reactor.run() ``` 上述代码展示了如何使用 `txhessian` 创建一个 Hessian 客户端代理,并异步调用远程服务的方法。服务端的实现则需要结合 Twisted 的 Web 或 Protocols 模块来监听请求并处理 Hessian 编码的数据[^1]。 对于更复杂的场景,可能需要自定义协议解析器来处理 Hessian 编码的消息。这通常涉及对 Hessian 协议规范的理解,以及对 Twisted 的 `Protocol` 类的子类化,以实现特定的读写逻辑。 此外,如果项目需求中涉及到与 Java 服务的互操作性,确保 Hessian 的版本兼容性尤为重要。Hessian 有两个主要版本,即 Hessian 1.0 和 Hessian 2.0,后者提供了更好的性能和扩展性。因此,在选择 Python Hessian 库时,应确认其支持的 Hessian 版本是否满足与目标服务的兼容性要求。 总之,尽管 Twisted 不直接提供 Hessian 协议的支持,但通过第三方库和自定义协议开发,可以在 Twisted 应用程序中有效地集成和使用 Hessian 协议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值