Spring HTTP invoker
2016-12-27 13:50
204 查看
Spring HTTP invoker简介
Spring HTTP invoker是
spring框架中的一个远程调用模型,执行基于
HTTP的远程调用(意味着可以通过防火墙),并使用
java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于
webservice,但又不同于
webservice,区别如下:
项目中使用哪种远程调用机制取决于项目本身的要求。
²HTTP invoker服务模式
说明:
1.服务器端:通过HTTP invoker
服务将服务接口的某个实现类提供为远程服务
<
19047
span>2.客户端:通过HTTP invoker
代理向服务器端发送请求,远程调用服务接口的方法
3.服务器端与客户端通信的数据需要序列化
springJAR 文件
建议使用spring2+.jar
版本
2.创建服务接口
3.创建服务接口的具体实现类
4.公开服务
springJAR文件
建议使用spring2+.jar
版本
2.创建服务接口
3.访问服务
UcService.java
它提供两项服务,查询用户信息和记录日志,如下:
publicinterfaceUcService
{
publicUserInfo getUserInfobyName(String userName);
publicintrecordLog(String
username, String point, String operate, String desc);
}
说明:举这个列子是因为其比较有代表性,它将展示普通数据类型(int,long
等)和复杂数据类型(DTO
等)的远程调用方式。 UserInfo
是一个普通的 DTO,代码如下:
publicclassUserInfo
implementsSerializable {
privatestaticfinallongserialVersionUID=
-6970967506712260305L;
/**用户名
*/
privateString
userName;
/**电子邮箱
*/
privateString
email;
/**注册日期
*/
privateDate
registDate;
publicString getUserName() {
returnuserName;
}
publicvoidsetUserName(String
userName) {
this.
userName
= userName;
}
publicString getEmail() {
returnemail;
}
publicvoidsetEmail(String
email) {
this.
email
= email;
}
publicDate getRegistDate() {
returnregistDate;
}
publicvoidsetRegistDate(Date
registDate) {
this.
registDate
= registDate;
}
}
注意:因为是在网络间传输对象,所以需要将UserInfo
实现 Serializable
接口,并指定一个 serialVersionUID(任意值即可,同时客户端也要有这个类,否则在客户端接收对象时会因为
serialVersionUID 不匹配而出现异常)
回到UcService.java,它提供了两个服务(在这里一个方法代表一个服务功能),我们需要具体的实现类来实现真正的服务
2.实现类是
UCServiceImpl.java
publicclassUCServiceImpl
implementsUcService {
privatestaticLogger
pointrecordlog= Logger.getLogger ("pointrecordlog"
);
privatestaticLogger
logger= Logger.getLogger (UCServiceImpl.class
);
privateUcFacade
ucFacade;
publicvoidsetUcFacade(UcFacade
ucFacade) {
this.
ucFacade
= ucFacade;
}
publicUserInfo getUserInfobyName(String userName) {
UserInfo user =null
;
try{
user =ucFacade
.getUserInfoDetail(userName);
logger.debug(
"get userinfo success by
username:" + userName);
}catch
(Throwable t) {
logger.error(
"get userinfo fail by
username:" + userName, t);
}
returnuser;
}
publicintrecordLog(String
username, String point, String operate, String desc) {
intresult = 0;
try{
pointrecordlog.info(username +
" - "+ point +
" - "+ operate +
" - "+ desc);
}catch
(Throwable t) {
result = -1;
logger.error(t);
}
returnresult;
}
}
说明:ucFacade
是通过 spring 注入的一个数据查询类,因为它与
http invoker没有直接关系,所以不进行介绍。
3.公开服务
UcService.java
²WEB-INF/application-context.xml:将接口声明为
HTTP invoker服务
<
bean id
= "httpService"
class=
"org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
>
<property
name=
"service"
>
<ref
bean=
"ucService" />
</property
>
<property
name=
"serviceInterface"
value=
"com.netqin.baike.service.UcService"
>
</property
>
</bean
>
<bean
id=
"ucService" class=
"com.netqin.baike.service.impl.UCServiceImpl"
/>
说明:HttpInvokerServiceExporter实际上是一个
spring mvc 控制器,它处理客户端的请求并调用服务实现。
²WEB-INF/service-servlet.xml:
HttpInvokerServiceExporter实际上是一个
spring mvc 控制器,所以需要为其提供
spring URL处理器,这里我们使用
SimpleUrlHandlerMapping
<
bean
class=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<property
name=
"mappings"
>
<props
>
<prop
key=
"/httpService"
> httpService
</ prop
>
</props
>
</property
>
</bean
>
²WEB-INF/web.xml:配置
spring监听及
DispatcherServlet
<
context-param >
<param-name
>
contextConfigLocation </
param-name >
<param-value
>
/WEB-INF/application-context.xml
</param-value
>
</context-param
>
<listener
>
<listener-class
>
org.springframework.web.context.ContextLoaderListener
</listener-class
>
</listener
>
<
servlet >
<servlet-name
>
service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
load-on-startup
>1
</
load-on-startup>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>service
</
servlet-name>
<
url-pattern
>/service/*
</
url-pattern>
</
servlet-mapping
>
说明:不了解为什么这么配置的可以去看看spring mvc
方面的资料。
好了,经过以上配置,一个基于spring HTTP invoker
的远程服务就完成了,服务的地址为: http://${serviceName}:${port}/${contextPath}/service/httpService
客户端
1.
创建服务接口及网络间传输的DTO
类
为了方便,可以将服务器端创建好的的UcService.java
和UserInfo.java
拷贝到客户端 ,
或打个 jar
包放到 lib
下。
2.
配置访问服务
² WEB-INF/application-context.xml:如果项目中已经存在
spring配置文件,则不需要创建该文件,需要配置
HTTP invoker的代理
<bean
id=
"httpService"
class=
"org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
>
<
property
name=
"serviceUrl"
>
<
value
> http://${serviceName}:${port}/${contextPath}/service/httpService
</
value >
</
property
>
<
property
name=
"serviceInterface"
value=
"com.netqin.baike.service.UcService"
>
</
property
>
</bean
>
说明:客户端使用
HttpInvokerProxyFactoryBean 代理客户端向服务器端发送请求,请求接口为
UcService 的服务
注意:需要修改
serviceUrl 为实际的服务器地址
² WEB-INF/web.xml:配置
spring监听
如果项目没有
spring 环境,则需要在web.xml中加入对
spring的支持
<
context-param >
<
param-name
>
contextConfigLocation
</ param-name>
<
param-value
>
/WEB-INF/application-context.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</listener
>
3.
访问服务方法
u 读取
spring上下文,以远程调用
getUserInfobyName方法为例
² 在
jsp,servlet,action等等文件中
UcService service= (UcService) WebApplicationContextUtils
.getRequiredWebApplicationContext(
request.getSession().getServletContext()).getBean(
"httpService");
UserInfouser
= service.getUserInfobyName("hanqunfeng");
² 如果不想配置
spring运行环境,可以使用如下方式:
ApplicationContext applicationContext
= newFileSystemXmlApplicationContext("classpath:application-context.xml");
service = (UcService) applicationContext.getBean("httpService");
u 依赖注入,远程调用
recordLog方法为例
² 在
WEB-INF/application-context.xml中加入如下配置:
<
bean id=
"abc" class=
"com.netqin.test.abc"
>
<
property
name=
"service"
>
<
ref bean=
"httpService"
/>
</
property
>
</bean
>
² 为
com.netqin.test.abc中加入对
service的
set方法:
privateUcService
service
;
publicvoidsetService(UcService
service){
this.
service
= service;
}
publicString
recordUserLog(String username,String point,String operate,String desc){
String result =
service.recordLog(username, point, operate, desc);
returnresult;
}
关于服务器端配置的补充说明:
有一个误区:有些关于springMVC的书 上说,如果没有明确声明一个处理适配器,默认会使用 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,这个适配器 专门负责处理所有实现了
org.springframework.web.servlet.mvc.Controller 接口的处理器,我就是受其影响,认为 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter实现的是 org.springframework.web.HttpRequestHandler接口,所以按理说应该使用的处理适配器是 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,但实际上并不会出现异
常。
其实,原因是因为spring默认会使用四个处理适配器(参看DispatcherServlet.properties,spring2.5,spring2.0只默认三个,2.5增加注解方式):
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,/
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,/
org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter,/
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
关于DispatcherServlet.properties的详细信息可以参看:
http://blog.csdn.net/hanqunfeng/archive/2010/01/08/5161319.aspx
但是,如果明确声明了其它的处理适配器,比如 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 等等,则默认规则则会覆盖,需要明确声明 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器,否则系 统会抛异常:
javax.servlet.ServletException: No adapter for handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@179bd14]: Does your handler implement a supported interface like Controller?
所以,建议在使用spring invoker时,最好明确声明org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器
转载来自:http://hanqunfeng.iteye.com/blog/868210#comments
Spring HTTP invoker简介
Spring HTTP invoker是
spring框架中的一个远程调用模型,执行基于
HTTP的远程调用(意味着可以通过防火墙),并使用
java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于
webservice,但又不同于
webservice,区别如下:
webservice | HTTP invoker |
跨平台,跨语言 | 只支持java 语言 |
支持SOAP ,提供 wsdl | 不支持 |
结构庞大,依赖特定的webservice 实现,如xfire 等 | 结构简单,只依赖于spring 框架本身 |
²HTTP invoker服务模式
说明:
1.服务器端:通过HTTP invoker
服务将服务接口的某个实现类提供为远程服务
<
19047
span>2.客户端:通过HTTP invoker
代理向服务器端发送请求,远程调用服务接口的方法
3.服务器端与客户端通信的数据需要序列化
配置服务器端和客户端的步骤
配置服务器端
1.添加springJAR 文件
建议使用spring2+.jar
版本
2.创建服务接口
3.创建服务接口的具体实现类
4.公开服务
配置客户端
1.添加springJAR文件
建议使用spring2+.jar
版本
2.创建服务接口
3.访问服务
实例讲解
服务器端
1.服务接口:UcService.java
它提供两项服务,查询用户信息和记录日志,如下:
publicinterfaceUcService
{
publicUserInfo getUserInfobyName(String userName);
publicintrecordLog(String
username, String point, String operate, String desc);
}
说明:举这个列子是因为其比较有代表性,它将展示普通数据类型(int,long
等)和复杂数据类型(DTO
等)的远程调用方式。 UserInfo
是一个普通的 DTO,代码如下:
publicclassUserInfo
implementsSerializable {
privatestaticfinallongserialVersionUID=
-6970967506712260305L;
/**用户名
*/
privateString
userName;
/**电子邮箱
*/
privateString
email;
/**注册日期
*/
privateDate
registDate;
publicString getUserName() {
returnuserName;
}
publicvoidsetUserName(String
userName) {
this.
userName
= userName;
}
publicString getEmail() {
returnemail;
}
publicvoidsetEmail(String
email) {
this.
= email;
}
publicDate getRegistDate() {
returnregistDate;
}
publicvoidsetRegistDate(Date
registDate) {
this.
registDate
= registDate;
}
}
注意:因为是在网络间传输对象,所以需要将UserInfo
实现 Serializable
接口,并指定一个 serialVersionUID(任意值即可,同时客户端也要有这个类,否则在客户端接收对象时会因为
serialVersionUID 不匹配而出现异常)
回到UcService.java,它提供了两个服务(在这里一个方法代表一个服务功能),我们需要具体的实现类来实现真正的服务
2.实现类是
UCServiceImpl.java
publicclassUCServiceImpl
implementsUcService {
privatestaticLogger
pointrecordlog= Logger.getLogger ("pointrecordlog"
);
privatestaticLogger
logger= Logger.getLogger (UCServiceImpl.class
);
privateUcFacade
ucFacade;
publicvoidsetUcFacade(UcFacade
ucFacade) {
this.
ucFacade
= ucFacade;
}
publicUserInfo getUserInfobyName(String userName) {
UserInfo user =null
;
try{
user =ucFacade
.getUserInfoDetail(userName);
logger.debug(
"get userinfo success by
username:" + userName);
}catch
(Throwable t) {
logger.error(
"get userinfo fail by
username:" + userName, t);
}
returnuser;
}
publicintrecordLog(String
username, String point, String operate, String desc) {
intresult = 0;
try{
pointrecordlog.info(username +
" - "+ point +
" - "+ operate +
" - "+ desc);
}catch
(Throwable t) {
result = -1;
logger.error(t);
}
returnresult;
}
}
说明:ucFacade
是通过 spring 注入的一个数据查询类,因为它与
http invoker没有直接关系,所以不进行介绍。
3.公开服务
UcService.java
²WEB-INF/application-context.xml:将接口声明为
HTTP invoker服务
<
bean id
= "httpService"
class=
"org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
>
<property
name=
"service"
>
<ref
bean=
"ucService" />
</property
>
<property
name=
"serviceInterface"
value=
"com.netqin.baike.service.UcService"
>
</property
>
</bean
>
<bean
id=
"ucService" class=
"com.netqin.baike.service.impl.UCServiceImpl"
/>
说明:HttpInvokerServiceExporter实际上是一个
spring mvc 控制器,它处理客户端的请求并调用服务实现。
²WEB-INF/service-servlet.xml:
HttpInvokerServiceExporter实际上是一个
spring mvc 控制器,所以需要为其提供
spring URL处理器,这里我们使用
SimpleUrlHandlerMapping
<
bean
class=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<property
name=
"mappings"
>
<props
>
<prop
key=
"/httpService"
> httpService
</ prop
>
</props
>
</property
>
</bean
>
²WEB-INF/web.xml:配置
spring监听及
DispatcherServlet
<
context-param >
<param-name
>
contextConfigLocation </
param-name >
<param-value
>
/WEB-INF/application-context.xml
</param-value
>
</context-param
>
<listener
>
<listener-class
>
org.springframework.web.context.ContextLoaderListener
</listener-class
>
</listener
>
<
servlet >
<servlet-name
>
service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
load-on-startup
>1
</
load-on-startup>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>service
</
servlet-name>
<
url-pattern
>/service/*
</
url-pattern>
</
servlet-mapping
>
说明:不了解为什么这么配置的可以去看看spring mvc
方面的资料。
好了,经过以上配置,一个基于spring HTTP invoker
的远程服务就完成了,服务的地址为: http://${serviceName}:${port}/${contextPath}/service/httpService
客户端
1. 创建服务接口及网络间传输的DTO
类
为了方便,可以将服务器端创建好的的UcService.java
和UserInfo.java
拷贝到客户端 ,
或打个 jar
包放到 lib
下。
2.
配置访问服务
² WEB-INF/application-context.xml:如果项目中已经存在
spring配置文件,则不需要创建该文件,需要配置
HTTP invoker的代理
<bean
id=
"httpService"
class=
"org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
>
<
property
name=
"serviceUrl"
>
<
value
> http://${serviceName}:${port}/${contextPath}/service/httpService
</
value >
</
property
>
<
property
name=
"serviceInterface"
value=
"com.netqin.baike.service.UcService"
>
</
property
>
</bean
>
说明:客户端使用
HttpInvokerProxyFactoryBean 代理客户端向服务器端发送请求,请求接口为
UcService 的服务
注意:需要修改
serviceUrl 为实际的服务器地址
² WEB-INF/web.xml:配置
spring监听
如果项目没有
spring 环境,则需要在web.xml中加入对
spring的支持
<
context-param >
<
param-name
>
contextConfigLocation
</ param-name>
<
param-value
>
/WEB-INF/application-context.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</listener
>
3.
访问服务方法
u 读取
spring上下文,以远程调用
getUserInfobyName方法为例
² 在
jsp,servlet,action等等文件中
UcService service= (UcService) WebApplicationContextUtils
.getRequiredWebApplicationContext(
request.getSession().getServletContext()).getBean(
"httpService");
UserInfouser
= service.getUserInfobyName("hanqunfeng");
² 如果不想配置
spring运行环境,可以使用如下方式:
ApplicationContext applicationContext
= newFileSystemXmlApplicationContext("classpath:application-context.xml");
service = (UcService) applicationContext.getBean("httpService");
u 依赖注入,远程调用
recordLog方法为例
² 在
WEB-INF/application-context.xml中加入如下配置:
<
bean id=
"abc" class=
"com.netqin.test.abc"
>
<
property
name=
"service"
>
<
ref bean=
"httpService"
/>
</
property
>
</bean
>
² 为
com.netqin.test.abc中加入对
service的
set方法:
privateUcService
service
;
publicvoidsetService(UcService
service){
this.
service
= service;
}
publicString
recordUserLog(String username,String point,String operate,String desc){
String result =
service.recordLog(username, point, operate, desc);
returnresult;
}
关于服务器端配置的补充说明:
有一个误区:有些关于springMVC的书 上说,如果没有明确声明一个处理适配器,默认会使用 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,这个适配器 专门负责处理所有实现了
org.springframework.web.servlet.mvc.Controller 接口的处理器,我就是受其影响,认为 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter实现的是 org.springframework.web.HttpRequestHandler接口,所以按理说应该使用的处理适配器是 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,但实际上并不会出现异
常。
其实,原因是因为spring默认会使用四个处理适配器(参看DispatcherServlet.properties,spring2.5,spring2.0只默认三个,2.5增加注解方式):
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,/
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,/
org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter,/
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
关于DispatcherServlet.properties的详细信息可以参看:
http://blog.csdn.net/hanqunfeng/archive/2010/01/08/5161319.aspx
但是,如果明确声明了其它的处理适配器,比如 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 等等,则默认规则则会覆盖,需要明确声明 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器,否则系 统会抛异常:
javax.servlet.ServletException: No adapter for handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@179bd14]: Does your handler implement a supported interface like Controller?
所以,建议在使用spring invoker时,最好明确声明org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器
转载来自:http://hanqunfeng.iteye.com/blog/868210#comments
相关文章推荐
- 动态创建Spring HttpInvoker client
- Spring HTTP invoker简介
- Spring Httpinvoker简单Demo
- spring http invoker 高级篇
- Spring远程调用HttpInvoker
- Spring HttpInvoker 开发
- Spring 远程调用 HTTP invoker
- Spring HttpInvoker和Hessian在Spring MVC下出错
- Spring HTTP invoker简介
- Spring中HttpInvoker远程方法调用使用实例
- Spring HttpInvoker(一)
- Spring HttpInvoker
- Spring HTTP invoker简介
- [转载]spring的httpInvoker远程调用 示例
- 动态创建Spring HttpInvoker client
- Spring发布和使用Http Invoker
- Spring HttpInvoker(二)
- spring http invoker 高级篇
- spring httpinvoker
- Spring中HttpInvoker实例