首页 > 代码库 > spring实现远程方法调用
spring实现远程方法调用
spring in action一书的读书笔记
使用spring实现远程方法调用可以极大简化开发
将一个spring bean包装成为RMI service
[java]
view plain
copy
- <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
[java]
view plain
copy
- String serviceUrl = "rmi:/spitter/SpitterService";
- SpitterService spitterService = (SpitterService)Naming.lookup(serviceUrl);
我们也可以使用spring进行配置
[html]
view plain
copy
- <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
[html]
view plain
copy
- <bean id="hessianSpitterService" class="org.springframework.remoting.caucho.HessianServiceExporter"
- p:service-ref="spitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
[html]
view plain
copy
- 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>
[html]
view plain
copy
- <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
[html]
view plain
copy
- <bean id="burlapSpitterService" class="org.springframework.remoting.caucho.BurlapServiceExporter"
- p:service-ref="spitterService"
- p:serviceInterface="com.habuma.spitter.service.SpitterService"/>
调用 Burlap service
[html]
view plain
copy
- <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
[html]
view plain
copy
- <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中的类不同
spring实现远程方法调用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。