spring in action一书的读书笔记
使用spring实现远程方法调用可以极大简化开发
将一个spring bean包装成为RMI service
- <bean class="org.springframework.remoting.rmi.RmiServiceExporter"
- p:service-ref="spitterService"
- p:serviceName="SpitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"
- p:registryHost="rmi.spitter.com"
- p:registryPort="1199"/>
调用发布的RMI service
- String serviceUrl = "rmi:/spitter/SpitterService";
- SpitterService spitterService = (SpitterService)Naming.lookup(serviceUrl);
我们也可以使用spring进行配置
- <bean id="spitterService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" p:serviceUrl="rmi://localhost/SpitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
RMI有一个限制就是对防火墙不太友好,不容易穿过防火墙,这在局域网可以不用考虑,但是广域网就需要考虑这一点
Hessian,Burlap是Caucho科技提供的两种方法
Hessian基于二进制消息进行通信
Burlap基于xml格式消息进行通信
发布Hessian service
- <bean id="hessianSpitterService" class="org.springframework.remoting.caucho.HessianServiceExporter"
- p:service-ref="spitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
- HessianServiceExporter在spring中是作为一个controller存在的,所以需要在web.xml(意味着必须是web项目)配置DispatcherServlet,将对Hessian service bean的请求映射到hessianSpitterService
- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <value>
- /spitter.service=hessianSpitterService
- </value>
- </property>
- </bean>
- <bean id="spitterService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"
- p:serviceUrl="http://localhost:8080/Spitter/spitter.service"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
发布Burlap service
- <bean id="burlapSpitterService" class="org.springframework.remoting.caucho.BurlapServiceExporter"
- p:service-ref="spitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
调用 Burlap service
- <bean id="spitterService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"
- p:serviceUrl="http://localhost:8080/Spitter/spitter.service"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
RMI使用jdk中的类库,但是对防火墙不太友好,burlap和hessian对防火墙友好但是使用了第三方的类库,Spring's HttpInvoker解决了这两个矛盾。使用spring's httpInvoker双发必须都是用spring
- <bean class="org.spring.framework.remoting.httpinvoker.HttpInvokerServiceExporter"
- p:service-ref="spitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
- HttpInvokerServiceExporter在spring中也是作为一个controller
- 调用HttpInvoker service
- <bean id="spitterService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
- p:serviceUrl="http://localhost:8080/Spitter/spitter.service"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
几种方式使用方法大体相同,只是使用的spring中的类不同