首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring 2 遠程服務統一封裝:RMI、Hessian、Burlap、HTTP Invoker 完整配置指南

     編輯:Java 相關知識

Spring Remote 核心價值

Spring 2 org.springframework.remoting 提供統一抽象,讓業務介面無感知切換底層協議:RMI、Hessian/Burlap(HTTP 二進制/XML)、HTTP Invoker(Java 序列化)。

共享介面與實現

public interface IBookService { Book getById(String id); }
public class Book { public String name, id, author; }
public class BookService implements IBookService {
    public Book getById(String id) { return BookStore.getById(id); }
}
// 客戶端
public class BookQueryService {
    private IBookService bookService;
    public void setAccountService(IBookService s) { this.bookService = s; }
    public Book getBookById(String id) { return bookService.getById(id); }
}

1. RMI 服務

服務端

<bean id="bookService" class="com.xmatthew.spring.remote.BookService"/>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="bookService"/>
    <property name="service" ref="bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
    <property name="registryPort" value="1800"/>
</bean>

客戶端

<bean class="com.xmatthew.spring.remote.client.BookQueryService">
    <property name="bookService" ref="bookService"/>
</bean>
<bean id="bookService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://localhost:1800/bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
</bean>

2. Hessian(HTTP 二進制)

需在 web.xml 配置 DispatcherServlet 映射 /remoting/*。

服務端

<bean name="/bookService" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
</bean>

客戶端

<bean id="bookService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="https://localhost:8080/remoting/bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
</bean>

3. Burlap(HTTP XML)

配置同 Hessian,僅將類名替換:
BurlapServiceExporter / BurlapProxyFactoryBean。

4. HTTP Invoker(Java 序列化)

同樣需 DispatcherServlet。

服務端

<bean name="/bookService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
</bean>

客戶端

<bean id="bookService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    <property name="serviceUrl" value="https://localhost:8080/remoting/bookService"/>
    <property name="serviceInterface" value="com.xmatthew.spring.remote.IBookService"/>
</bean>

協議選型建議

協議序列化防火牆友好跨語言適用場景
RMIJava 原生差(需開高埠)否內網 Java 互調
Hessian二進制好(HTTP)是(多語言支援)高效能、跨語言
BurlapXML好是除錯、異構系統
HTTP InvokerJava 序列化好否Spring 生態內部調用
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved