Spring使用远程服务之Hessian
2017-08-29 16:49
686 查看
原文地址:http://blog.csdn.net/junshuaizhang/article/details/28441907
Hessian像RMI一样,使用二进制消息进行客户端和服务端的交互,它的二进制消息可以移植到其他非Java的语言中包括PHP、Python、C++和C#。因为Hessian是基于HTTP的,所以HessianSeriviceExporter实现为一个spring MVC控制器。
HessianSeriviceExporter是一个SpringMVC控制器,它可以接收Hessian请求,并将这些请求翻译成对POJO的调用来,从而将POJO导出为一个Hessian服务
为了使用导出Hessian服务,我们需要执行两个额外的配置步骤:
1、在web.xml中配置Spring的DispatcherServlet,并把我们的应用部署为Web应用;
2、在Spring的配置文件中配置一个URL处理器,将Hessian服务的URL分发给对应的Hessian服务Bean。
下面我们距离说明
我的项目分层
web.xml
[html] view
plain copy
<?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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-hessian.xml
</param-value>
</context-param>
<servlet>
<!-- servlet-name保持与spring-hessian.xml中一致 -->
<servlet-name>HelloServiceExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServiceExporter</servlet-name>
<url-pattern>/HelloService</url-pattern>
</servlet-mapping>
</web-app>
spring-hessian.xml
[html] view
plain copy
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd"
default-lazy-init="true">
<context:annotation-config />
<!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="hessian" />
<!-- 自动装配 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- Name保持与web.xml中的一致,web.xml下文中描述 -->
<bean name="HelloServiceExporter"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->
<property name="service" ref="helloService" />
<!-- 接口的路径 -->
<property name="serviceInterface"
value="hessian.HelloService" />
</bean>
</beans>
java类
[java] view
plain copy
package hessian;
public interface HelloService {
void sayHello(String name);
}
[java] view
plain copy
package hessian;
import org.springframework.stereotype.Service;
@Service("helloService")
public class HelloServiceImpl implements HelloService {
/* (non-Javadoc)
* @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String)
*/
@Override
public void sayHello(String name) {
System.out.println("Hello " + name + "!");
}
}
客户端的调用
首先将Service的打成jar包加入到客户端程序中
测试代码
[java] view
plain copy
package hessionclient;
import hessian.HelloService;
import com.caucho.hessian.client.HessianProxyFactory;
public class ArticleManager {
public static void main(String[] args) {
try {
String url = "http://localhost:8080/SpringTest/HelloService";
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(
HelloService.class, url);
helloService.sayHello("张三");
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端调用图
参考:
《Spring实战(第3版)》
PS:代码主体来源于网络
Hessian像RMI一样,使用二进制消息进行客户端和服务端的交互,它的二进制消息可以移植到其他非Java的语言中包括PHP、Python、C++和C#。因为Hessian是基于HTTP的,所以HessianSeriviceExporter实现为一个spring MVC控制器。
HessianSeriviceExporter是一个SpringMVC控制器,它可以接收Hessian请求,并将这些请求翻译成对POJO的调用来,从而将POJO导出为一个Hessian服务
为了使用导出Hessian服务,我们需要执行两个额外的配置步骤:
1、在web.xml中配置Spring的DispatcherServlet,并把我们的应用部署为Web应用;
2、在Spring的配置文件中配置一个URL处理器,将Hessian服务的URL分发给对应的Hessian服务Bean。
下面我们距离说明
我的项目分层
web.xml
[html] view
plain copy
<?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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-hessian.xml
</param-value>
</context-param>
<servlet>
<!-- servlet-name保持与spring-hessian.xml中一致 -->
<servlet-name>HelloServiceExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServiceExporter</servlet-name>
<url-pattern>/HelloService</url-pattern>
</servlet-mapping>
</web-app>
spring-hessian.xml
[html] view
plain copy
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd"
default-lazy-init="true">
<context:annotation-config />
<!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="hessian" />
<!-- 自动装配 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- Name保持与web.xml中的一致,web.xml下文中描述 -->
<bean name="HelloServiceExporter"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->
<property name="service" ref="helloService" />
<!-- 接口的路径 -->
<property name="serviceInterface"
value="hessian.HelloService" />
</bean>
</beans>
java类
[java] view
plain copy
package hessian;
public interface HelloService {
void sayHello(String name);
}
[java] view
plain copy
package hessian;
import org.springframework.stereotype.Service;
@Service("helloService")
public class HelloServiceImpl implements HelloService {
/* (non-Javadoc)
* @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String)
*/
@Override
public void sayHello(String name) {
System.out.println("Hello " + name + "!");
}
}
客户端的调用
首先将Service的打成jar包加入到客户端程序中
测试代码
[java] view
plain copy
package hessionclient;
import hessian.HelloService;
import com.caucho.hessian.client.HessianProxyFactory;
public class ArticleManager {
public static void main(String[] args) {
try {
String url = "http://localhost:8080/SpringTest/HelloService";
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(
HelloService.class, url);
helloService.sayHello("张三");
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端调用图
参考:
《Spring实战(第3版)》
PS:代码主体来源于网络
相关文章推荐
- Spring使用远程服务之Hessian
- SpringBoot+MyBatis使用Hessian远程服务通讯
- Spring Framework 开发参考手册 之十六 通过Spring使用远程访问和web服务
- spring技术内幕18-Spring使用Hessian实现远程调用
- (二)spring 远程服务之Hessian
- 通过Spring使用远程访问和web服务
- Spring中使用Hessian暴露服务
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- Spring - Java/J2EE Application Framework 应用框架 第 16 章 通过Spring使用远程访问和web服务
- 通过Spring使用远程访问和web服务
- 通过Spring使用远程访问和web服务(转载)
- 通过Spring使用远程访问和web服务
- 使用PHPHessian调用Java Spring Hessian暴露服务­
- 使用Spring进行远程访问与Web服务[转]
- Spring整合Hessian访问远程服务 - OnTheRoad_Lee
- 使用Spring进行远程访问(rmi)与Web服务
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- spring集成blazeds,使用@RemoteDestination实现远程服务
- Spring使用Hessian实现远程调用
- 使用分布式缓存来群集Spring远程服务