一、Spring 远程服务
1. RMI
1.1 发布 RMI 服务
<bean id="accountService" class="example.AccountServiceImpl">
<!-- any additional properties, maybe a DAO? -->
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- does not necessarily have to be the same name as the bean to be exported -->
<property name="serviceName" value="AccountService"/>
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
<!-- 默认情况下,绑定到本机的1099端口,可以设置将 RMI 绑定到指定主机 -->
<property name="registryHost" value="rmi.spitter.com" />
<!-- 绑定端口,默认 1099 -->
<property name="registryPort" value="1199"/>
</bean>
1.2 在客户端连接 RMI
<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1199/AccountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
2. Hessian、Burlap
2.1 发布 Hessian 服务
<!-- 配置 Spring MVC -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
使用 HessianServiceExporter 创建 Hessian Service bean
<!-- 在 /WEB-INF/ 下创建 remoting-servlet.xml -->
<bean id="accountService" class="example.AccountServiceImpl">
<!-- any additional properties, maybe a DAO? -->
</bean>
<!-- 根据 bean 的 name 属性指定调用服务的 URL,<code class="literal">'http://HOST:8080/remoting/AccountService'</code>. -->
<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
也可以通过 web.xml 中配置服务 URL,并在 applicationContext.xml 文件中配置 Service bean
<servlet>
<servlet-name>accountExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>accountExporter</servlet-name>
<url-pattern>/remoting/AccountService</url-pattern>
</servlet-mapping>
<bean name="accountExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
2.2 客户端调用 Hessian
<bean class="example.SimpleObject">
<property name="accountService" ref="accountService"/>
</bean>
<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
Burlap 与 Hessian 都是基于 HTTP 协议的,Just replace the word
Hessian
with
Burlap
and you’re all set to go.
3. HttpInvoker
HttpInvoker 基于 HTTP 协议并使用 Java 序列化机制
<!-- HttpInvoker 创建服务的配置与 Hessian、Burlap 一致 -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
3.2 客户端调用 HttpInvoker 服务
<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>
<property name="serviceInterface" value="example.AccountService"/>
<!-- 可以设置使用哪种 httpclient 进行连接,默认是 JavaSE HTTP 功能 -->
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
</property>
</bean>
4. WebService
4.1 发布 WebService
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
/**
继承 SpringBeanAutowiringSupport 为了可以自动装配需要用的其它类,来实现逻辑
*/
@WebService(serviceName="AccountService")
public class AccountServiceEndpoint extends SpringBeanAutowiringSupport {
@Autowired
private AccountService biz;
@WebMethod
public void insertAccount(Account acc) {
biz.insertAccount(acc);
}
@WebMethod
public Account[] getAccounts(String name) {
return biz.getAccounts(name);
}
}
SimpleJaxWsServiceExporter 开始工作时会自动扫描 @Webservice 注解的 bean,如果你的 WebService 类是标准的 bean,那不需要继承 SpringBeanAutowiringSupport 类也可以实现自动装配
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/"/>
</bean>
<bean id="accountServiceEndpoint" class="example.AccountServiceEndpoint">
...
</bean>
...
4.2 客户端调用 WebService
<bean id="accountWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="example.AccountService"/>
<property name="wsdlDocumentUrl" value="http://localhost:8888/AccountServiceEndpoint?WSDL"/>
<property name="namespaceUri" value="http://example/"/>
<property name="serviceName" value="AccountService"/>
<property name="portName" value="AccountServiceEndpointPort"/>
</bean>
<bean id="client" class="example.AccountClientImpl">
...
<property name="service" ref="accountWebService"/>
</bean>