1、Spring HTTP invoker简介Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于webservice,但又不同于webservice,区别如下:webserviceHTTP invoker跨平台,跨语言只支持java语言支持SOAP,提供wsdl不支持结构庞大,依赖特定的webservice实现,如xfire等结构简单,只依赖于spring框架本身项目中使用哪种远程调用机制取决于项目本身的
2、要求。HTTP invoker服务模式说明:1.服务器端:通过HTTP invoker服务将服务接口的某个实现类提供为远程服务2.客户端:通过HTTP invoker代理向服务器端发送请求,远程调用服务接口的方法3.服务器端与客户端通信的数据需要序列化配置服务器端和客户端的步骤配置服务器端1.添加springJAR文件建议使用spring2+.jar版本2.创建服务接口3.创建服务接口的具体实现类4.公开服务配置客户端1.添加springJAR文件建议使用spring2+.jar版本2.创建服务接口3.访问服务实例讲解服务器端1.服务接口:UcService.java它提供两项服务,查询用户信
3、息和记录日志,如下:publicinterfaceUcService publicUserInfo getUserInfobyName(String userName);publicintrecordLog(String username, String point, String operate, String desc);说明:举这个列子是因为其比较有代表性,它将展示普通数据类型(int,long等)和复杂数据类型(DTO等)的远程调用方式。UserInfo是一个普通的DTO,代码如下:publicclassUserInfoimplementsSerializable privatesta
4、ticfinallongserialVersionUID= -6970967506712260305L;/*用户名*/privateStringuserName;/*电子邮箱*/privateStringemail;/*注册日期*/privateDateregistDate;publicString getUserName() returnuserName;publicvoidsetUserName(String userName) this.userName= userName;publicString getEmail() returnemail;publicvoidsetEmail(St
5、ring email) this.email= email;publicDate getRegistDate() returnregistDate;publicvoidsetRegistDate(Date registDate) this.registDate= registDate;注意:因为是在网络间传输对象,所以需要将UserInfo实现Serializable接口,并指定一个serialVersionUID(任意值即可,同时客户端也要有这个类,否则在客户端接收对象时会因为serialVersionUID不匹配而出现异常)回到UcService.java,它提供了两个服务(在这里一个方法
6、代表一个服务功能),我们需要具体的实现类来实现真正的服务2.实现类是UCServiceImpl.javapublicclassUCServiceImplimplementsUcService privatestaticLoggerpointrecordlog= Logger.getLogger(pointrecordlog);privatestaticLoggerlogger= Logger.getLogger(UCServiceImpl.class);privateUcFacadeucFacade;publicvoidsetUcFacade(UcFacade ucFacade) this.u
7、cFacade= ucFacade;publicUserInfo getUserInfobyName(String userName) UserInfo user =null;tryuser =ucFacade.getUserInfoDetail(userName);logger.debug(get userinfo success byusername:+ userName);catch(Throwable t) logger.error(get userinfo fail byusername:+ userName, t);returnuser;publicintrecordLog(Str
8、ing username, String point, String operate, String desc) intresult = 0;trypointrecordlog.info(username + - + point + - + operate + - + desc);catch(Throwable t) result = -1;logger.error(t);returnresult;说明:ucFacade是通过spring注入的一个数据查询类,因为它与http invoker没有直接关系,所以不进行介绍。3.公开服务UcService.javaWEB-INF/applicati
9、on-context.xml:将接口声明为HTTP invoker服务说明:HttpInvokerServiceExporter实际上是一个spring mvc控制器,它处理客户端的请求并调用服务实现。WEB-INF/service-servlet.xml:HttpInvokerServiceExporter实际上是一个spring mvc控制器,所以需要为其提供spring URL处理器,这里我们使用SimpleUrlHandlerMappinghttpServiceWEB-INF/web.xml:配置spring监听及DispatcherServletcontextConfigLocati
10、on/WEB-INF/application-context.xmlorg.springframework.web.context.ContextLoaderListenerserviceorg.springframework.web.servlet.DispatcherServlet1service/service/*说明:不了解为什么这么配置的可以去看看spring mvc方面的资料。好了,经过以上配置,一个基于spring HTTP invoker的远程服务就完成了,服务的地址为:http:/$serviceName:$port/$contextPath/service/httpServ
11、ice客户端1.创建服务接口及网络间传输的DTO类为了方便,可以将服务器端创建好的的UcService.java和UserInfo.java拷贝到客户端,或打个jar包放到lib下。2.配置访问服务WEB-INF/application-context.xml:如果项目中已经存在spring配置文件,则不需要创建该文件,需要配置HTTP invoker的代理http:/$serviceName:$port/$contextPath/service/httpService说明:客户端使用HttpInvokerProxyFactoryBean代理客户端向服务器端发送请求,请求接口为UcServic
12、e的服务注意:需要修改serviceUrl为实际的服务器地址WEB-INF/web.xml:配置spring监听如果项目没有spring环境,则需要在web.xml中加入对spring的支持contextConfigLocation/WEB-INF/application-context.xmlorg.springframework.web.context.ContextLoaderListener3.访问服务方法u读取spring上下文,以远程调用getUserInfobyName方法为例在jsp,servlet,action等等文件中UcServiceservice= (UcService
13、) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext().getBean(httpService);UserInfouser=service.getUserInfobyName(hanqunfeng);如果不想配置spring运行环境,可以使用如下方式:ApplicationContext applicationContext=newFileSystemXmlApplicationContext(classpath:application-cont
14、ext.xml);service = (UcService) applicationContext.getBean(httpService);u依赖注入,远程调用recordLog方法为例在WEB-INF/application-context.xml中加入如下配置:为qin.test.abc中加入对service的set方法:privateUcServiceservice;publicvoidsetService(UcService service)this.service= service;publicString recordUserLog(String username,String
15、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.Controll
16、er 接口的处理器,我就是受其影响,认为 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter实现的是 org.springframework.web.HttpRequestHandler接口,所以按理说应该使用的处理适配器是 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,但实际上并不会出现异 常。其实,原因是因为spring默认会使用四个处理适配器(参看DispatcherServlet.properties,spring2.5,sp
17、ring2.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.AnnotationMethodH
18、andlerAdapter关于DispatcherServlet.properties的详细信息可以参看:但是,如果明确声明了其它的处理适配器,比如 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 等等,则默认规则则会覆盖,需要明确声明 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器,否则系 统会抛异常:javax.servlet.ServletException: No adapter for
19、handler org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter179bd14: Does your handler implement a supported interface like Controller?所以,建议在使用spring invoker时,最好明确声明org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器补充:默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接
20、即org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor,可以通过设置httpInvokerRequestExecutor属性来改变默认配置,spring提供了另外一种HttpClient,org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor。修改配置如下::4080/ucs/service/httpService需要在项目中引入两个jar包:commons-codec-x.x.jarcommons-httpclient-x.x.x.jarCommonsHttpInvokerRequestExecutor具有HTTP connection pooling,不过通过使用jmeter进行压力测试发现,SimpleHttpInvokerRequestExecutor性能高于CommonsHttpInvokerRequestExecutor