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