1.Spring中,HTTPInvoker(HTTP调用器)是通过基于HTTP协议的分布式远程调用解决方案,和java
RMI一样,HTTP调用器也需要使用java的对象序列化机制完成客户端和服务器端的通信。HTTP调用器的远程调用工作原理如下:
(1).客户端:
a.向服务器发送远程调用请求:
远程调用信息——>封装为远程调用对象——>序列化写入到远程调用HTTP请求中——>向服务器端发送。
b.接收服务器端返回的远程调用结果:
服务器端返回的远程调用结果HTTP响应——>反序列化为远程调用结果对象。
(2).服务器端:
a.接收客户端发送的远程调用请求:
客户端发送的远程调用HTTP请求——>反序列化为远程调用对象——>调用服务器端目标对象的目标方法处理。
b.向客户端返回远程调用结果:
服务器端目标对象方法的处理结果——>序列化写入远程调用结果HTTP响应中——>返回给客户端。
接下来我们将从客户端和服务器端分别分析HTTP调用器远程调用的具体实现。
2.HTTP调用器客户端配置:
使用HTTP调用器之前,首先需要对客户端其进行如下的配置:
-
- <bean id=”proxy” class=”org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean”>
- <property name=”serviceUrl”>
- <value>http://yourhost:8080/远程调用URL</value>
- </property>
- <property name=”serviceInterface”>
- <value>远程调用服务接口全路径</value>
- </property>
- </bean>
- <bean id=”客户端bean” class=”客户端Bean全路径”>
- <property name=”remoteService”>
- <ref bean=”proxy”/>
- </property>
- </bean>
在HTTP调用器客户端代理HttpInvokerProxyFactoryBean中封装远程调用服务URL和服务接口,客户端程序通过HTTP调用代理可以调用实现了指定接口的目标服务端对象。
3.HttpInvokerProxyFactoryBean创建远程调用代理对象:
HTTP调用器客户端代理HttpInvokerProxyFactoryBean是一个实现了Spring FactoryBean接口的IoC容器,其作用是对远程服务客户端封装,源码如下:
- public class HttpInvokerProxyFactoryBean extends HttpInvokerClientInterceptor implements FactoryBean<Object> {
-
- private Object serviceProxy;
-
- public void afterPropertiesSet() {
-
- super.afterPropertiesSet();
-
- if (getServiceInterface() == null) {
- throw new IllegalArgumentException("Property 'serviceInterface' is required");
- }
-
-
-
- this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader());
- }
-
- public Object getObject() {
- return this.serviceProxy;
- }
-
- public Class<?> getObjectType() {
- return getServiceInterface();
- }
-
- public boolean isSingleton() {
- return true;
- }
- }
通过上面对HttpInvokerProxyFactoryBean源码的分析我们看到,当通过getObject方法向Spring
IoC容器索取远程调用对象时,触发afterPropertiesSet回调方法,创建远程调用的代理对象,最后将该远程调用代理对象返回。在创建远程调用代理对象时,使用其父类HttpInvokerClientInterceptor作为远程调用代理对象的拦截器,该拦截器将拦截对代理对象的方法调用。下面我们分析HttpInvokerClientInterceptor代理拦截器对代理对象的方法拦截处理。
4.HttpInvokerClientInterceptor拦截对远程调用代理的方法调用:
当客户端通过HTTP请求调用远程调用代理的方法时,将会触发HttpInvokerClientInterceptor拦截器的invoke方法对当前的请求进行封装处理,将客户端的java对象序列化传输到服务器端,在远程服务器端执行完请求之后,又将处理结果java对象序列化返回给客户端。其源码如下:
- public class HttpInvokerClientInterceptor extends RemoteInvocationBasedAccessor
- implements MethodInterceptor, HttpInvokerClientConfiguration {
- private String codebaseUrl;
-
- private HttpInvokerRequestExecutor httpInvokerRequestExecutor;
- public void setCodebaseUrl(String codebaseUrl) {
- this.codebaseUrl = codebaseUrl;
- }
- public String getCodebaseUrl() {
- return this.codebaseUrl;
- }
- public void setHttpInvokerRequestExecutor(HttpInvokerRequestExecutor httpInvokerRequestExecutor) {
- this.httpInvokerRequestExecutor = httpInvokerRequestExecutor;
- }
-
-
- public HttpInvokerRequestExecutor getHttpInvokerRequestExecutor() {
- if (this.httpInvokerRequestExecutor == null) {
- SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor();
- executor.setBeanClassLoader(getBeanClassLoader());
- this.httpInvokerRequestExecutor = executor;
- }
- return this.httpInvokerRequestExecutor;
- }
-
- public void afterPropertiesSet() {
-
- super.afterPropertiesSet();
-
- getHttpInvokerRequestExecutor();
- }
-
-
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
- return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";
- }
-
- RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
-
- RemoteInvocationResult result = null;
- try {
-
- result = executeRequest(invocation, methodInvocation);
- }
- catch (Throwable ex) {
- throw convertHttpInvokerAccessException(ex);
- }
- try {
-
- 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);
- }
- }
- }
-
- protected RemoteInvocationResult executeRequest(
- RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception {
- return executeRequest(invocation);
- }
-
- protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws Exception {
- return getHttpInvokerRequestExecutor().executeRequest(this, invocation);
- }
-
- protected RemoteAccessException convertHttpInvokerAccessException(Throwable ex) {
- if (ex instanceof ConnectException) {
- throw new RemoteConnectFailureException(
- "Could not connect to HTTP invoker remote service at [" + getServiceUrl() + "]", ex);
- }
- else if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError ||
- ex instanceof InvalidClassException) {
- throw new RemoteAccessException(
- "Could not deserialize result from HTTP invoker remote service [" + getServiceUrl() + "]", ex);
- }
- else {
- throw new RemoteAccessException(
- "Could not access HTTP invoker remote service at [" + getServiceUrl() + "]", ex);
- }
- }
- }
通过上面对HttpInvokerClientInterceptor拦截器的源码分析,我们可以看出,拦截器将客户端对远程调用的HTTP请求封装成了MethodInvocation对象,拦截器的在调用远程调用的代理对象时,又将方法调用封装成了RemoteInvocation远程调用,RemoteInvocation数据对象中封装了调用的具体信息,如方法名、方法参数以及参数类型等。
真正执行远程调用的是HTTP调用请求执行器SimpleHttpInvokerRequestExecutor,下面我们继续分析SimpleHttpInvokerRequestExecutor远程调用的具体过程。
5.SimpleHttpInvokerRequestExecutor远程调用:
SimpleHttpInvokerRequestExecutor封装了基于HTTP协议的远程调用过程,具体源码如下:
- public class SimpleHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor {
-
- protected RemoteInvocationResult doExecuteRequest(
- HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
- throws IOException, ClassNotFoundException {
-
- HttpURLConnection con = openConnection(config);
-
- prepareConnection(con, baos.size());
-
- writeRequestBody(config, con, baos);
-
- validateResponse(config, con);
- InputStream responseBody = readResponseBody(config, con);
-
- return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
- }
-
- protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config) throws IOException {
-
- URLConnection con = new URL(config.getServiceUrl()).openConnection();
- if (!(con instanceof HttpURLConnection)) {
- throw new IOException("Service URL [" + config.getServiceUrl() + "] is not an HTTP URL");
- }
- return (HttpURLConnection) con;
- }
-
- protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {
- con.setDoOutput(true);
-
- con.setRequestMethod(HTTP_METHOD_POST);
-
- con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
-
- con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));
- LocaleContext locale = LocaleContextHolder.getLocaleContext();
-
- if (locale != null) {
- con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));
- }
-
- if (isAcceptGzipEncoding()) {
- con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
- }
- }
-
- protected void writeRequestBody(
- HttpInvokerClientConfiguration config, HttpURLConnection con, ByteArrayOutputStream baos)
- throws IOException {
- baos.writeTo(con.getOutputStream());
- }
-
- protected void validateResponse(HttpInvokerClientConfiguration config, HttpURLConnection con)
- throws IOException {
-
- if (con.getResponseCode() >= 300) {
- throw new IOException(
- "Did not receive successful HTTP response: status code = " + con.getResponseCode() +
- ", status message = [" + con.getResponseMessage() + "]");
- }
- }
-
- protected InputStream readResponseBody(HttpInvokerClientConfiguration config, HttpURLConnection con)
- throws IOException {
-
- if (isGzipResponse(con)) {
- return new GZIPInputStream(con.getInputStream());
- }
-
- else {
- return con.getInputStream();
- }
- }
-
- protected boolean isGzipResponse(HttpURLConnection con) {
-
- String encodingHeader = con.getHeaderField(HTTP_HEADER_CONTENT_ENCODING);
- return (encodingHeader != null && encodingHeader.toLowerCase().indexOf(ENCODING_GZIP) != -1);
- }
- }
通过对SimpleHttpInvokerRequestExecutor的分析,我们看到,HTTP调用请求执行器的处理逻辑是:首先,打开指定URL的HTTP连接,设置连接属性。其次,将封装请求的RemoteInvocation对象序列化到请求体中,请HTTP请求发送到服务器端。最后,从服务器端的HTTP响应中读取输入流,并将响应结果转换成RemoteInvocationResult。
将远程调用的HTTP响应转换为RemoteInvocationResult是由AbstractHttpInvokerRequestExecutor的readRemoteInvocationResult方法实现,下面我们将分析其将HTTP响应结果转换成RemoteInvocationResult的实现。
6.AbstractHttpInvokerRequestExecutor其将HTTP响应结果转换成RemoteInvocationResult:
AbstractHttpInvokerRequestExecutor中处理远程调用结果,并HTTP响应转换为RemoteInvocationResult的主要方法如下:
-
- protected RemoteInvocationResult readRemoteInvocationResult(InputStream is, String codebaseUrl)
- throws IOException, ClassNotFoundException {
-
- ObjectInputStream ois = createObjectInputStream(decorateInputStream(is), codebaseUrl);
- try {
-
- return doReadRemoteInvocationResult(ois);
- }
- finally {
- ois.close();
- }
- }
-
- protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois)
- throws IOException, ClassNotFoundException {
-
- Object obj = ois.readObject();
- if (!(obj instanceof RemoteInvocationResult)) {
- throw new RemoteException("Deserialized object needs to be assignable to type [" +
- RemoteInvocationResult.class.getName() + "]: " + obj);
- }
-
- return (RemoteInvocationResult) obj;
- }
7.HTTP调用器的服务器端配置:
和HTTP调用器客户端类似,服务器端也需要进行如下的配置:
- <bean name=”/客户端配置的远程调用URL” class=”org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter”>
- <property name=”service”>
- <ref bean=”服务器端实现bean”/>
- </property>
- <property name=”serviceInterface”>
- <value>远程调用服务接口全路径</value>
- </property>
- </property>
通过对服务器端配置的例子,我们可以看出,真正处理远程调用的服务器端实现是由service属性中指定的服务器端bean提供的,HttpInvokerServiceExporter将远程调用服务接口和服务实现类进行封装,主要提HTTP协议封装和java对象序列化功能。
Spring的HttpInvokerServiceExporter是与Spring的MVC结合在一起的,它本质上是Spring
MVC的一个Controller,客户端发来的远程调用HTTP请求有Spring
MVC的中央控制器DispatcherServlet转发到指定URL的HttpInvokerServiceExporter上。
8.HttpInvokerServiceExporter导出和执行远程调用服务:
HttpInvokerServiceExporter响应客户端发送的远程调用HTTP请求,它从HTTP请求中读取远程调用并将其反序列化为RemoteInvocation对象,然后调用目标服务对象的目标方法完成远程调用服务,当服务执行完成之后,通过HTTP响应把执行结果对象序列化输出到客户端。器源码如下:
- public class HttpInvokerServiceExporter extends RemoteInvocationSerializingExporter implements HttpRequestHandler {
-
- public void handleRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- try {
-
- RemoteInvocation invocation = readRemoteInvocation(request);
-
- RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
-
- writeRemoteInvocationResult(request, response, result);
- }
- catch (ClassNotFoundException ex) {
- throw new NestedServletException("Class not found during deserialization", ex);
- }
- }
-
- protected RemoteInvocation readRemoteInvocation(HttpServletRequest request) throws IOException, ClassNotFoundException {
-
- return readRemoteInvocation(request, request.getInputStream());
- }
-
- protected RemoteInvocation readRemoteInvocation(HttpServletRequest request, InputStream is) throws IOException, ClassNotFoundException {
-
- ObjectInputStream ois = createObjectInputStream(decorateInputStream(request, is));
- try {
-
- return doReadRemoteInvocation(ois);
- }
- finally {
- ois.close();
- }
- }
-
- protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException {
- return is;
- }
-
- protected void writeRemoteInvocationResult(
- HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result) throws IOException {
-
- response.setContentType(getContentType());
-
- writeRemoteInvocationResult(request, response, result, response.getOutputStream());
- }
-
- protected void writeRemoteInvocationResult(
- HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os)
- throws IOException {
-
- ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os));
- try {
-
- doWriteRemoteInvocationResult(result, oos);
- }
- finally {
- oos.close();
- }
- }
-
- protected OutputStream decorateOutputStream(
- HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException {
- return os;
- }
- }
通过对HttpInvokerServiceExporter的源码分析,我们可以看出,真正执行远程对象调用的是RemoteInvocationResultresult
= invokeAndCreateResult(invocation, getProxy());它调用了RemoteInvocationBasedExporter的invokeAndCreateResult方法调用远程目标对象方法,并创建远程调用执行结果,下面我们继续分析执行服务器端远程调用目标对象方法的实现。
9.RemoteInvocationBasedExporter调用服务器目标对象:
RemoteInvocationBasedExporter的invokeAndCreateResult方法调用服务器目标对象方法,RemoteInvocationBasedExporter源码如下:
- public abstract class RemoteInvocationBasedExporter extends RemoteExporter {
-
- private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor();
- public RemoteInvocationExecutor getRemoteInvocationExecutor() {
- return this.remoteInvocationExecutor;
- }
- protected RemoteInvocationResult invokeAndCreateResult(RemoteInvocation invocation, Object targetObject) {
- try {
-
- Object value = invoke(invocation, targetObject);
-
- return new RemoteInvocationResult(value);
- }
- catch (Throwable ex) {
- return new RemoteInvocationResult(ex);
- }
- }
-
- protected Object invoke(RemoteInvocation invocation, Object targetObject)
- throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- if (logger.isTraceEnabled()) {
- logger.trace("Executing " + invocation);
- }
- try {
-
-
- return getRemoteInvocationExecutor().invoke(invocation, targetObject);
- }
- catch (NoSuchMethodException ex) {
- if (logger.isDebugEnabled()) {
- logger.warn("Could not find target method for " + invocation, ex);
- }
- throw ex;
- }
- catch (IllegalAccessException ex) {
- if (logger.isDebugEnabled()) {
- logger.warn("Could not access target method for " + invocation, ex);
- }
- throw ex;
- }
- catch (InvocationTargetException ex) {
- if (logger.isDebugEnabled()) {
- logger.debug("Target method failed for " + invocation, ex.getTargetException());
- }
- throw ex;
- }
- }
- }
通过上面对RemoteInvocationBasedExporter源码分析我们看到,真正调用目标对象的是DefaultRemoteInvocationExecutor的invoke方法,下面我们继续分析DefaultRemoteInvocationExecutor调用目标对象方法的实现。
10.DefaultRemoteInvocationExecutor调用目标对象的方法实现远程调用:
DefaultRemoteInvocationExecutor用于调用目标对象的指定方法实现远程对象调用服务,其源码如下:
- public class DefaultRemoteInvocationExecutor implements RemoteInvocationExecutor {
-
- public Object invoke(RemoteInvocation invocation, Object targetObject)
- throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
- Assert.notNull(invocation, "RemoteInvocation must not be null");
- Assert.notNull(targetObject, "Target object must not be null");
-
- return invocation.invoke(targetObject);
- }
- }
- RemoteInvocation的invoke方法源码如下:
- public Object invoke(Object targetObject)
- throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
-
- Method method = targetObject.getClass().getMethod(this.methodName, this.parameterTypes);
-
- return method.invoke(targetObject, this.arguments);
- }
|