我们到底能走多远系列(40)

扯淡:

   判断是否加可以效力于这家公司,一个很好的判断是,接触下这公司工作几年的员工,了解下生活工作状态,这就是你几年后的状态,如果满意就可以考虑加入了。

主题:

  场景:项目A作为主项目,业务实现完整,项目B需要调用项目A中的部分服务,那么项目A就需要提供出服务出来。实现分布式调用的方法有很多,这里介绍一下利用Spring Http Invoker 来实现的服务提供和调用。

  demo地址:摸我

  如果你对快速用springmvc搭建web应用感兴趣:摸我

  

  阅读Spring的源码基本的流程实现是这样的:  

下面就详细分析一下这个流程的实现:

1,服务请求流程分析
对远程服务的配置例子如下:
 <bean id="remoteDemoService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:7777/remote/demoService</value>
</property>
<property name="serviceInterface">
<value>com.witown.open.demo.remote.service.DemoService</value>
</property>
</bean>

首先看的是HttpInvokerProxyFactoryBean类,它继承FactoryBean,所以先来看一看这个FactoryBean。

在spring中可以利用FactoryBean来产生一些自定义配置的bean。
 
如果一个类继承 FactoryBean 用户可以自己定义产生实例对象的方法只要实现他的 getObject 方法。然而在 Spring 内部这个 Bean 的实例对象是 FactoryBean,通过调用这个对象的 getObject 方法就能获取用户自定义产生的对象,正是这样获得bean的机制,我们可以在getObject 方法中自定义一些操作,从而为 Spring 提供了很好的扩展性。
public class HttpInvokerProxyFactoryBean extends HttpInvokerClientInterceptor
implements FactoryBean<Object> {
// 代理对象
private Object serviceProxy;
// 初始化后执行方法
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
if (getServiceInterface() == null) {
throw new IllegalArgumentException("Property 'serviceInterface' is required");
} // 使用AOP代理工厂创建代理对象,对这个代理对象的所有方法调用最后都会被拦截
// 调用接口的任何一个方法时都会被拦截去变成http请求
this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader());
}
// 获得bean方法
public Object getObject() {
return this.serviceProxy;
}
public Class<?> getObjectType() {
return getServiceInterface();
}
public boolean isSingleton() {
return true;
}
}

这样DemoService 的调用都会被HttpInvokerClientInterceptor拦截,拦截的方法调用会去执行HttpInvokerClientInterceptor中的invoke方法,这个方法明了的体现了客户端的执行流程的三个步骤:

public Object invoke(MethodInvocation methodInvocation) throws Throwable {
if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";
}
// 1,把方法执行信息封装成RemoteInvocation
RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
RemoteInvocationResult result = null;
try {
// 2,发起http请求,把方法执行信息传过去,正常的话,服务器会返回结果
result = executeRequest(invocation, methodInvocation);
}
catch (Throwable ex) {
throw convertHttpInvokerAccessException(ex);
}
try {
// 3,解析返回的结果,转化成java对象
return recreateRemoteInvocationResult(result);
}
catch (Throwable ex) {
if (result.hasInvocationTargetException()) {
throw ex;
}
else {
throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +
"] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex);
}
}
}
step 1,MethodInvocation是客户端调用的方法,spring的AOP实现了这个接口,第一步就是吧这个对象转变成RemoteInvocation,它是Serializable 的一边后面对这个对象的序列化,RemoteInvocation封装了什么:
 public class RemoteInvocation implements Serializable {
//方法名
private String methodName;
// 类
private Class[] parameterTypes;
// 参数
private Object[] arguments;
// 步骤1中实际就是调用了这个构造函数而已
public RemoteInvocation(MethodInvocation methodInvocation) {
this.methodName = methodInvocation.getMethod().getName();
this.parameterTypes = methodInvocation.getMethod().getParameterTypes();
this.arguments = methodInvocation.getArguments();
}
// ...
}
step 2,调用链下去会调用到AbstractHttpInvokerRequestExecutor的executeRequest方法,这个方法又分成两步:1,将远程调用信息对象转成流,2,将流写入http请求

  public final RemoteInvocationResult executeRequest(HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws Exception {
// RemoteInvocation(远程调用对象),转成可以传输的OutputStream,以便写入http请求中
ByteArrayOutputStream baos = getByteArrayOutputStream(invocation);
if (logger.isDebugEnabled()) {
logger.debug("Sending HTTP invoker request for service at [" + config.getServiceUrl() +
"], with size " + baos.size());
}
// 将流写入http请求,发送请求,并接收响应
return doExecuteRequest(config, baos);
}

继续追踪getByteArrayOutputStream方法,发现使用了ObjectOutputStream最终调用了writeObject方法来将对象转化成流,下面是一段ObjectOutputStream的描述:

     Java中ObjectInputStream 与 ObjectOutputStream这两个包装类可用于输入流中读取对象类数据和将对象类型的数据写入到底层输入流 。ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口。需要注意的是:对象中的 transient 和 static 类型的成员变量不会被读取和写入 。
 
转换好之后得到的是ByteArrayOutputStream ,此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()和 toString()获取数据。
 
将流写入http请求,发送请求,并接收响应:
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
throws IOException, ClassNotFoundException {
// 打开远程的HTTP连接
HttpURLConnection con = openConnection(config); // 设置HTTP连接信息
prepareConnection(con, baos.size()); // 把准备好的序列化的远程方法调用对象的字节流写入到HTTP请求体中
writeRequestBody(config, con, baos); // 校验HTTP响应
validateResponse(config, con); // 获得HTTP相应体的流对象
InputStream responseBody = readResponseBody(config, con);
// 读取远程调用结果对象并返回
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
}

最后的读取远程调用结果对象并返回:

protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
// 获得Object
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocationResult)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocationResult.class.getName() + "]: " + obj);
}
// 服务端对返回的对象封装成RemoteInvocationResult!
return (RemoteInvocationResult) obj;
}

到这里基本完成了,客户端请求远程对象方法的流程。

2,服务提供者流程分析
 

既然对方用http请求,那作为spring,自然是会给予Spring mvc来实现的啦。
从例子的配置中就可以看到:拦截所有/remote/*请求,来调用服务。
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>

remote-servlet.xml文件中配置了处理请求的service:

< bean name= "/demoService" class ="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
< property name= "service" ref = "demoServiceImpl"/>
< property name= "serviceInterface" value ="com.witown.open.demo.service.DemoService" />
</bean >

HttpInvokerServiceExporter继承了HttpRequestHandler,所以请求从handleRequest方法开始。

整个请求到响应的流程像编码解码,很普遍的流程,查看下面代码,步骤十分清晰。
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { try {
// 解析请求,获得RemoteInvocation
RemoteInvocation invocation = readRemoteInvocation(request);
// 根据RemoteInvocation里的信息:方法,参数,执行,把返回的结果封装成RemoteInvocationResult
RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
// 把结果写入响应
writeRemoteInvocationResult(request, response, result);
}
catch (ClassNotFoundException ex) {
throw new NestedServletException("Class not found during deserialization", ex);
}
}

内部调用的代码经过前面请求调用发起的流程学习,就会很好理解了。
至此,整个流程就完整了。

牵涉知识点,有兴趣可以扩展学习:
1,spring FactoryBean
2,spring aop拦截机制
3,序列化
4,java发起http请求
 
 
总结:spring的这个封装很不错,在实际项目中使用方便,研读源码时可以遇到一些常用的知识领域,会有收获!
 
 
 
 
 

让我们继续前行

----------------------------------------------------------------------

努力不一定成功,但不努力肯定不会成功。

服务调用方案(Spring Http Invoker) - 我们到底能走多远系列(40)的更多相关文章

  1. 定时任务管理中心(dubbo+spring)-我们到底能走多远系列47

    我们到底能走多远系列47 扯淡: 又是一年新年时,不知道上一年你付出了多少,收获了多少呢?也许你正想着老板会发多少奖金,也许你正想着明年去哪家公司投靠. 这个时间点好好整理一下,思考总结一下,的确是个 ...

  2. Spring mvc源码url路由-我们到底能走多远系列(38)

    我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...

  3. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

  4. 初始化IoC容器(Spring源码阅读)-我们到底能走多远系列(31)

    我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? 毕竟,对自己的收入的分配差不多体现了自己的 ...

  5. JMS生产者+单线程发送-我们到底能走多远系列(29)

    我们到底能走多远系列(29) 扯淡: “然后我俩各自一端/望着大河弯弯/终于敢放胆/嘻皮笑脸/面对/人生的难”      --- <山丘> “迎着风/迎向远方的天空/路上也有艰难/也有那解 ...

  6. node实现http上传文件进度条 -我们到底能走多远系列(37)

    我们到底能走多远系列(37) 扯淡: 又到了一年一度的跳槽季,相信你一定准备好了,每每跳槽,总有好多的路让你选,我们的未来也正是这一个个选择机会组合起来的结果,所以尽可能的找出自己想要的是什么再做决定 ...

  7. node模拟http服务器session机制-我们到底能走多远系列(36)

    我们到底能走多远系列(36) 扯淡: 年关将至,总是会在一些时间节点上才感觉时光飞逝,在平时浑浑噩噩的岁月里都浪费掉了太多的宝贵.请珍惜! 主题:      我们在编写http请求处理和响应的代码的时 ...

  8. Sharded实现学习-我们到底能走多远系列(32)

    我们到底能走多远系列(32) 扯淡: 工作是容易的赚钱是困难的 恋爱是容易的成家是困难的 相爱是容易的相处是困难的 决定是容易的可是等待是困难的 主题: 1,Sharded的实现    Sharded ...

  9. Spring3整合Hibernate4-我们到底能走多远系列(30)

    我们到底能走多远系列(30) 扯淡: 30篇啦!从2012-08-15开始的系列,东平西凑将近一年的时间也就这么几篇.目标的100篇,按这个速度也要再搞两年呢. 发博客果然不是件容易的事,怪不得更多的 ...

随机推荐

  1. ANGULAR 开发用户选择器指令

    在开发表单时,我们需要使用经常需要使用到用户选择器,用户的数据一般使用如下方式存储: 用户1,用户2,用户3   我们可以使用angular指令实现选择器. <!DOCTYPE html> ...

  2. vim命令/压缩和解压命令

    gzip命令 -d 解压 -#  1 为最快 但容量问题 . 9为最好 .6为默认 gzip install.log 比较好理解,windows里面类似的,记住zip 和unzip是可以目录或者文件解 ...

  3. 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0

    大家好,Node.JS 4.0 发布了,这个流行的服务器端 JS 平台合并了 Node.js 和 io.js 的代码,4.0 版就是这两个项目结合的产物——现在合并为一个代码库.这次最主要的变化是 N ...

  4. linux 执行php文件

    /opt/php5/bin/php /home/Xcar/tag/interface/tag_api4hbase.php tag_export2file "/tmp/guo.php" ...

  5. js基础之数组

    数组方法 添加: push arr.push();//尾部添加 unshift arr.unshift();//头部添加 删除: pop arr.pop();//尾部删除 shift arr.shif ...

  6. Ajax入门

    实例如下: <html> <head> <script type="text/javascript"> function loadXMLDoc( ...

  7. 【数论+技巧】神奇的Noip模拟试题第二试 T1 素数统计

    1.      素数统计 (pcount.pas/.c/.cpp) [问题描述] 小tan的老师揣谙戈给同学们布置了一道题,要求统计给定区间内素数的个数.“这不是很简单吗?”小tan忍不住说.揣谙戈冷 ...

  8. 根据IP定位获取城市代码

    public String getCityID() throws IOException{ URL url = new URL("http://61.4.185.48:81/g/" ...

  9. Xceed WPF 主题皮肤控件Xceed Professional Themes for WPF详细介绍

    Xceed Professional Themes for WPF是一款为你的整个应用程序提供完美WPF主题风格的控件,包含Office 2007和Windows 7,可以应用到任何微软官方的WPF控 ...

  10. C语言实例代码

    绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...