spring HTTP invokers 实现远程调用
2012-10-11 11:24
405 查看
20.4 Exposing services using HTTP invokers
As opposed to Burlap and Hessian, which are both lightweight protocols using their own slim serialization mechanisms, Spring HTTP invokers use the standard Java serialization mechanism to expose services through HTTP. This has a huge advantage if your argumentsand return types are complex types that cannot be serialized using the serialization mechanisms Hessian and Burlap use (refer to the next section for more considerations when choosing a remoting technology).
Under the hood, Spring uses either the standard facilities provided by J2SE to perform HTTP calls or Commons
HttpClient. Use the latter if you need more advanced and easy-to-use functionality. Refer to
jakarta.apache.org/commons/httpclient for more info.
//服务器端:
20.4.1 Exposing the service object
Setting up the HTTP invoker infrastructure for a service object resembles closely the way you would do the same using Hessian or Burlap. Just as Hessian support provides theHessianServiceExporter, Spring's HttpInvoker support provides the
org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.
To expose the
AccountService(mentioned above) within a Spring Web MVC
DispatcherServlet, the following configuration needs to be in place in the dispatcher's application context:
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
Such an exporter definition will be exposed through the
DispatcherServlet's standard mapping facilities, as explained in the section on Hessian.
Alternatively, create an
HttpInvokerServiceExporterin your root application context (e.g. in
'WEB-INF/applicationContext.xml'):
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
In addition, define a corresponding servlet for this exporter in
'web.xml', with the servlet name matching the bean name of the target exporter:
<servlet> <servlet-name>accountExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>accountExporter</servlet-name> <url-pattern>/remoting/AccountService</url-pattern> </servlet-mapping>
If you are running outside of a servlet container and are using Sun's Java 6, then you can use the built-in HTTP server implementation. You can configure the
SimpleHttpServerFactoryBeantogether with a
SimpleHttpInvokerServiceExporteras is shown in this example:
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean> <bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean"> <property name="contexts"> <util:map> <entry key="/remoting/AccountService" value-ref="accountExporter"/> </util:map> </property> <property name="port" value="8080" /> </bean>
<property
name="port" value="8080" /> tomcat 服务器端口一致
//客服端代码
20.4.2 Linking in the service at the client
Again, linking in the service from the client much resembles the way you would do it when using Hessian or Burlap. Using a proxy, Spring will be able to translate your calls to HTTP POST requests to the URL pointing to the exported service.<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
As mentioned before, you can choose what HTTP client you want to use. By default, the
HttpInvokerProxyuses the J2SE HTTP functionality, but you can also use the Commons
HttpClientby setting the
httpInvokerRequestExecutorproperty:
<property name="httpInvokerRequestExecutor"> <bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/> </property
相关文章推荐
- 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
- 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
- spring HTTP invoker:实现远程调用
- 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
- spring技术内幕17-Spring HTTP调用器实现远程调用
- 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
- 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
- 《pro Spring》学习笔记之Spring HTTP 远程方法调用集成Tomcat实现安全验证
- java远程通讯轻量级HttpInvoker实现理赔调用承保核心系统打印出险前保单实现
- Spring实现远程调用
- Spring中HttpInvoker远程方法调用总结
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring之集成之使用Hession或Burlap通过http远程调用业务
- Spring 实现远程访问详解——httpclient
- Spring HTTP Invoker-远程调用模式
- Spring Boot 中使用Feign作为HTTP客户端调用远程HTTP服务
- Spring HttpInvokerProxyFactoryBean 远程调用示例
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- Spring + RMI + JND实现远程调用