Spring技术内幕——深入解析Spring架构与设计原理(五)Spring与远端调用

来源:互联网 发布:淘宝可以申请几个小号 编辑:程序博客网 时间:2024/05/29 14:17

转自:http://jiwenke.iteye.com/blog/518439


在应用开发中,常常涉及服务器系统中各种不同进程之间的通信与计算交互,远端调用(RMI)是实现这种计算场景的一种有效方式。此外,还存在着另一种情况,在这种应用场景中,与那些典型的基于HTML的B/S应用不同,客户端程序需要完成对服务器端应用的直接调用,这也是需要远端调用大显身手的场合。 

Spring中提供了轻量级的远端调用模块,从而为我们在上面提到的应用场景开发,提供平台支持。根据Spring的既定策略,它依然只是起到一个集成平台的作用,而并不期望在实现方案上,与已有的远端调用方案形成竞争。也就是说,在Spring远端调用架构中,具体的通信协议设计、通信实现,以及在服务器和客户端对远端调用的处理封装,Spring没有将其作为实现重点,在这个技术点上,并不需要重新发明轮子。对Spring来说,它所要完成的工作,是在已有远端调用技术实现的基础上,通过IoC与AOP的封装,让应用更方便地使用这些远端调用服务,并能够更方便灵活地与现有应用系统实现集成。通过Spring封装以后,应用使用远端过程调用非常方便,既不需要改变原来系统的相关实现接口,也不需要为远端调用功能增加新的封装负担。因此,这种使用方式,在某种程度上,可以称为轻量级的远端调用方案。 

在实现远端调用的过程中,往往需要涉及客户端和服务器端的相关设置,这些设置通过Spring的IoC容器就可以很好的完成,这是我们已经很熟悉的IoC容器的强项了。同时,Spring为远端调用的实现,提供了许多不同的方案,玲琅满目,任君选择。如RMI、HTTP调用器、第三方远端调用库Hessian/Burlap、基于Java RMI的解决方案,等等。 

Spring对不同的远端调用的实现封装,基本上,都采用了类似的模式来完成,比如在客户端,都是通过相关的ProxyFactoryBean和ClientInterceptor来完成的,在服务器端是通过ServiceExporter来导出远端的服务对象的。有了这些统一的命名规则,应用配置和使用远端调用会非常方便,同时,通过对这些Spring远端调用基础设施实现原理的分析,还可以看到一些常用处理方法的技术实现,比如对代理对象的使用、拦截器的使用、通过afterPropertiesSet来启动远端调用基础设施的建立,等等,这些都是在Spring中常用的技术。 

HTTP调用器客户端的实现 
在HtttpInvokerProxyFactory中,设置了serviceProxy对象作为远端服务的本地代理对象;同时,在依赖注入完成以后,通过afterPropertiesSet来对远端调用完成设置。 

Java代码  收藏代码
  1. public class HttpInvokerProxyFactoryBean extends HttpInvokerClientInterceptor  
  2.         implements FactoryBean<Object> {  
  3.     //这是远端对象的代理  
  4.     private Object serviceProxy;  
  5.   
  6.     @Override  
  7.     //在注入完成之后,设置远端对象代理  
  8.     public void afterPropertiesSet() {  
  9.         super.afterPropertiesSet();  
  10.         //需要配置远端调用的接口  
  11.         if (getServiceInterface() == null) {  
  12.             throw new IllegalArgumentException("Property 'serviceInterface' is required");  
  13.         }//这里使用ProxyFactory来生成远端代理对象,注意这个this,因为HttpInvokerProxyFactoryBean的基类是HttpInvokerClientInterceptor,所以代理类的拦截器被设置为HttpInvokerClientInterceptor  
  14.         this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader());  
  15.     }  
  16.   
  17.     //FactoryBean生产对象的入口。返回的是serviceProxy对象,这是一个代理对象  
  18.     public Object getObject() {  
  19.         return this.serviceProxy;  
  20.     }  
  21.   
  22.     public Class<?> getObjectType() {  
  23.         return getServiceInterface();  
  24.     }  
  25.   
  26.     public boolean isSingleton() {  
  27.         return true;  
  28.     }  

可以看到,为这个代理对象配置了一个拦截器HttpInvokerClientInterceptor,在这个拦截器中,拦截了对代理对象的方法调用。如以下代码所示: 
Java代码  收藏代码
  1. //对代理对象的方法调用入口  
  2. public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
  3.     if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {  
  4.         return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";  
  5.     }  
  6.     //创建RemoteInvocation对象,这个对象封装了对远端的调用,这些远端调用通过序列化的机制完成  
  7.     RemoteInvocation invocation = createRemoteInvocation(methodInvocation);  
  8.     RemoteInvocationResult result = null;  
  9.     try {  
  10.         //这里是对远端调用的入口  
  11.         result = executeRequest(invocation, methodInvocation);  
  12.     }  
  13.     catch (Throwable ex) {  
  14.         throw convertHttpInvokerAccessException(ex);  
  15.     }  
  16.     try {//返回远端调用的结果  
  17.         return recreateRemoteInvocationResult(result);  
  18.     }  
  19.     catch (Throwable ex) {  
  20.         if (result.hasInvocationTargetException()) {  
  21.             throw ex;  
  22.         }  
  23.         else {  
  24.             throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +  
  25.                     "] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex);  
  26.         }  
  27.     }  
  28. }  

远端调用的具体实现过程,是由executeRequest来完成的,也就是在SimpleHttpInvokerRequestExecutor的实现中,封装了整个HTTP调用器客户端实现的基本过程,如下所示: 
Java代码  收藏代码
  1. //这是HTTP调用器实现的基本过程,通过HTTP的request和reponse来完成通信,在通信的过程中传输的数据是序列化的对象  
  2. protected RemoteInvocationResult doExecuteRequest(  
  3.         HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)  
  4.         throws IOException, ClassNotFoundException {  
  5.     //打开一个标准J2SE HttpURLConnection  
  6.     HttpURLConnection con = openConnection(config);  
  7.     prepareConnection(con, baos.size());  
  8.     //远端调用封装成RemoteInvocation对象,这个对象通过序列化被写到对应的HttpURLConnection中去  
  9.     writeRequestBody(config, con, baos);  
  10.     //这里取得远端服务返回的结果,然后把结果转换成RemoteInvocationResult返回  
  11.     validateResponse(config, con);  
  12.     InputStream responseBody = readResponseBody(config, con);  
  13.   
  14.     return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());  
  15. }  
  16.   
  17. //把序列化对象输出到HttpURLConnection去  
  18. protected void writeRequestBody(  
  19.         HttpInvokerClientConfiguration config, HttpURLConnection con, ByteArrayOutputStream baos)  
  20.         throws IOException {  
  21.   
  22.     baos.writeTo(con.getOutputStream());  
  23. }  
  24.   
  25. //为使用HttpURLConnection完成对象序列化,需要进行一系列的配置  
  26. //比如配置请求方式为post,请求属性等等  
  27. protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {  
  28.     con.setDoOutput(true);  
  29.     con.setRequestMethod(HTTP_METHOD_POST);  
  30.     con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());  
  31.     con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));  
  32.     LocaleContext locale = LocaleContextHolder.getLocaleContext();  
  33.     if (locale != null) {  
  34.         con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));  
  35.     }  
  36.     if (isAcceptGzipEncoding()) {  
  37.         con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);  
  38.     }  
  39. }  
  40. //获得HTTP响应的IO流  
  41. protected InputStream readResponseBody(HttpInvokerClientConfiguration config, HttpURLConnection con)  
  42.         throws IOException {  
  43.     //如果是通过gzip压缩,那么需要先解压  
  44.     if (isGzipResponse(con)) {  
  45.         // GZIP response found - need to unzip.  
  46.         return new GZIPInputStream(con.getInputStream());  
  47.     }  
  48.     else {  
  49.         // Plain response found.  
  50.         // 正常的HTTP响应输出  
  51.         return con.getInputStream();  
  52.     }  
  53. }  


HTTP调用器服务器端的实现 

在服务器端使用Spring HTTP远端调用,需要配置HttpInvokerServiceExporter,作为远端服务的服务导出器,来接收HTTP服务请求。在通过HTTP请求,得到客户端传过来的RemoteInvocation对象以后,就可以进行服务方法的调用了。服务调用需要的基本信息,都封装在RemoteInvocation对象中。这个服务调用过程,是由invokeAndCreateResult方法来实现的,如RemoteInvocationSerializingExporter的invoke实现所示: 
Java代码  收藏代码
  1. protected Object invoke(RemoteInvocation invocation, Object targetObject)  
  2.         throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {  
  3.   
  4.     if (logger.isTraceEnabled()) {  
  5.         logger.trace("Executing " + invocation);  
  6.     }  
  7.     try {//调用RemoteInvocationExecutor,这个执行器是DefaultRemoteInvocationExecutor  
  8.         return getRemoteInvocationExecutor().invoke(invocation, targetObject);  
  9.     }  
  10.     catch (NoSuchMethodException ex) {  
  11.         if (logger.isDebugEnabled()) {  
  12.             logger.warn("Could not find target method for " + invocation, ex);  
  13.         }  
  14.         throw ex;  
  15.     }  
  16.     catch (IllegalAccessException ex) {  
  17.         if (logger.isDebugEnabled()) {  
  18.             logger.warn("Could not access target method for " + invocation, ex);  
  19.         }  
  20.         throw ex;  
  21.     }  
  22.     catch (InvocationTargetException ex) {  
  23.         if (logger.isDebugEnabled()) {  
  24.             logger.debug("Target method failed for " + invocation, ex.getTargetException());  
  25.         }  
  26.         throw ex;  
  27.     }  
  28. }  

看到的invoke方法封装了服务器端调用的主体,这个invoke方法在HttpInvokerServiceExporter的基类RemoteInvocationSerializingExporter中实现,服务对象的方法调用完成之后,会把调用结果,通过HTTP响应和对象序列化,传给HTTP调用器客户端,从而完成整个HTTP调用器的远端调用过程,如以下代码所示: 
Java代码  收藏代码
  1. protected void writeRemoteInvocationResult(  
  2.         HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result)  
  3.         throws IOException {  
  4. //设置Response的ContentType属性,设置为application/x-java-serialized-object  
  5.     response.setContentType(getContentType());  
  6.     writeRemoteInvocationResult(request, response, result, response.getOutputStream());  
  7. }  
  8. //输出到HTTP的Response,然后把Response关闭  
  9. protected void writeRemoteInvocationResult(  
  10.         HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os)  
  11.         throws IOException {  
  12.   
  13.     ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os));  
  14.     try {  
  15.         doWriteRemoteInvocationResult(result, oos);  
  16.         oos.flush();  
  17.     }  
  18.     finally {  
  19.         oos.close();  
  20.     }  
  21. }  

经过这一系列的处理过程,服务执行结果对象又回到了HTTP的远端调用客户端。在客户端从HTTP响应读取对象之后,它把这个看起来像是在本地实现,其实是由远端服务对象完成的调用结果,交给发起远端调用的客户端调用方法,从而最终完成整个远端调用的过程。这个过程很有特点,它使用了HTTP的请求和响应作为通信通道,在这个通信通道里面,并没有再做进一步的附加的通信协议的封装,而且,在这个处理过程中,使用的都是Java和Spring框架已有的特性,比如,通过IoC的配置,以及代理对象拦截器的封装处理,再加Java的序列化和反序列化,以及在服务器端的Spring MVC框架的使用,通过这些已有的技术实现,让使用者感觉,它的实现风格非常的简洁轻快,整个代码实现,阅读起来,也让人感到非常的赏心悦目。 

阅读全文
0 0
原创粉丝点击