spring in action一書的讀書筆記 html
使用spring實現遠程方法調用能夠極大簡化開發 java
將一個spring bean包裝成爲RMI service web
- <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有一個限制就是對防火牆不太友好,不容易穿過防火牆,這在局域網能夠不用考慮,可是廣域網就須要考慮這一點 spring
Hessian,Burlap是Caucho科技提供的兩種方法 app
Hessian基於二進制消息進行通訊 url
Burlap基於xml格式消息進行通訊 spa
發佈Hessian service .net
- <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>
調用發佈的Hessian service
- <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 xml
- <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中的類不一樣