java中可用的远程技术包括RMI、Caucho的Hessian和Burlap|、Spring的HTTP invoker、SOAP和JAX-RPC的web service。
spring简化RMI.
客户端
只需在spring配置文件中声明下面的<bean>
<bean id="testService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://127.0.0.1:1099/testService"></property> <property name="serviceInterface" value="com.pm.service.TestService"></property> </bean>
然后在需要的地方,就可以使用这个远程对象了,如:
<bean id="employeeService" class="com.pm.service.imp.EmployeeServiceImp"> <property name="employeeDao" ref="employeeDao"></property> <property name="testService" ref="testService"></property> </bean>
服务端,输出一个RMI:
1.创建一个普通服务接口,
package com.test; public interface TestService { public void print(String name); }
2.创建一个接口实现类:
package com.test; public class TestServiceImp implements TestService{ public void print(String name) { System.out.println(name+"你好啊!哈哈"); } }
3.在spring配置文件中配置 TestServiceImp bean:
<bean id="testService" class="com.test.TestServiceImp"></bean>
4.使用RmiServiceExporter将testService公开为RMI服务:
<bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="testService"></property> <property name="serviceName" value="testService"></property> <property name="serviceInterface" value="com.test.TestService"></property> <property name="registryPort" value="1099"></property> </bean>
其中registryPort默认就是1099。
至此,一个RMI客户端调用与服务端输出大功告成。但RMI可以使用任意端口,这是防火墙不允许的,还有,RMI是基于java的,这就意味着客户端和服务端都必须用java编写,为了解决这些限制,于是乎,引出了下面这些技术。
spring简化Hessian和Burlap
Hessian和Burlap是基于HTTP的轻量级远程服务,两者其实差不多,Hessian是基于二进制消息来建立客户端与服务端 之间的交流,而Burlap是基于XML的。
首先无论客户端还是服务器端,都得导入hessian.jar
还是以上面的例子,如果是客户端,我们只要修改一下spring配置文件的一个bean即可
Hessian:
<bean id="hessianTestService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/SpringRmi/test.service"></property> <property name="serviceInterface" value="com.pm.service.TestService"></property> </bean>
Burlap:
<bean id="hessianTestService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/SpringRmi/test.service"></property> <property name="serviceInterface" value="com.pm.service.TestService"></property> </bean>
而如果要配置服务端,则比RMI要稍微复杂点点:
1.在spring配置文件中:
<!--配置一个SimpleUrlHandlerMapping:--> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.service">hessianServiceExporter</prop> </props> </property> </bean> <bean id="hessianServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="testService"></property> <property name="serviceInterface" value="com.test.TestService"></property> </bean> <!-- 或者 --> <bean id="burlapServiceExporter" class="org.springframework.remoting.caucho.BurlapServiceExporter"> <property name="service" ref="testService"></property> <property name="serviceInterface" value="com.test.TestService"></property> </bean>
2.在web.xml中:
<servlet> <servlet-name>test</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping>
在测试过程中遇到,在启动客户端容器时会报:
org.springframework.web.HttpRequestMethodNotSupportedException: HessianServiceExporter only supports POST requests
找了好久,不知道是什么原因造成的,但似乎不影响程序运行。
HTTP invoker是基于Spirng的,用法跟Hessian、Burlap差不多,只需把org.springframework.remoting.caucho.HessianProxyFactoryBean与org.springframework.remoting.caucho.HessianServiceExporter替换成org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean与org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter