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

Spring Web Flow 與 Terracotta 打造高可用有狀態 Web 應用

     編輯:Java 相關知識

Spring Web Flow 核心價值

Spring Web Flow (SWF) 將「流程」提升為一等公民,提供宣告式流程定義(XML/Java DSL),支援與 Spring MVC、Struts、JSF 整合。核心優勢:

  • 集中管理控制邏輯(如搜尋流程),避免分散在多處。
  • 組合簡單流程構建複雜業務模組。
  • 線性程式模型替代冗長 if/else,定義嚴格導航規則。
  • 流程結束自動清理記憶體。
  • 支援四大受管作用域:request、flash、flow、conversation,各具獨特生命週期語意。
  • 自動處理瀏覽器前進/後退/刷新。
  • 可視化編輯器 (Spring IDE) 支援流程圖編輯。
  • 脫離容器單元測試流程邏輯。

企業級擴展與高可用挑戰

  • 如何水平擴展提升吞吐量?
  • 如何消除單點故障,滿足 SLA(如 99.999% 可用性)?
  • Session 粘滯負載平衡不足以保證故障轉移時狀態無縫遷移。

叢集 vs 快取:叢集 = 多 JVM 共享應用狀態;快取 = 讓狀態貼近執行上下文。快取是叢集子集。

Terracotta for Spring 解決方案

核心特性

  • HTTP Session 叢集:透明共享 SWF 會話狀態與放入 Session 的物件,保證高可用與故障轉移。
  • Spring Bean 叢集:Singleton 與 Session-scoped Bean 生命週期語意在叢集間保持一致,聲明式配置哪些 Bean 需叢集。
  • POJO 透明叢集:無需實作 Serializable、無需修改代碼,類載入期透明生效。傳輸記憶體級差量變更,而非序列化整個物件圖。
  • 虛擬記憶體管理:分散式 GC 與虛擬堆,允許 4GB 物理記憶體運行 200GB 堆應用,無需擔心會話資料超過實體堆。

架構原理

  • 採用 AOP 技術在類載入時植入叢集邏輯,維護 Java 語意(物件引用、方法呼叫、GC)。
  • 中心調度器 管理客戶端節點,採 Lazy 方式僅將「髒資料」推送給引用該資料的節點。
  • 支援 Session 粘滯負載平衡,進一步減少跨節點資料同步。
  • 調度器可配置備援,避免單點故障;可獨立擴展調度器叢集。

實戰範例:Sellitem 應用

Spring MVC 整合

<servlet>
  <servlet-name>sellitem</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/sellitem-servlet-config.xml /WEB-INF/sellitem-webflow-config.xml</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>sellitem</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

Flow Executor 與 Registry 配置

<flow:executor id="flowExecutor" registry-ref="flowRegistry"/>
<flow:registry id="flowRegistry">
  <flow:location path="/WEB-INF/flows/**-flow.xml" />
</flow:registry>

流程定義 (sellitem-flow.xml)

<flow xmlns="https://www.springframework.org/schema/webflow" ...>
  <var name="sale" class="org.springframework.webflow.samples.sellitem.Sale"/>
  <start-state idref="enterPriceAndItemCount"/>

  <view-state id="enterPriceAndItemCount" view="priceAndItemCountForm">
    <render-actions><action bean="formAction" method="setupForm"/></render-actions>
    <transition on="submit" to="enterCategory">
      <action bean="formAction" method="bindAndValidate">
        <attribute name="validatorMethod" value="validatePriceAndItemCount"/>
      </action>
    </transition>
  </view-state>

  <view-state id="enterCategory" view="categoryForm">
    <transition on="submit" to="requiresShipping">
      <action bean="formAction" method="bind"/>
    </transition>
  </view-state>

  <decision-state id="requiresShipping">
    <if test="${flowScope.sale.shipping}" then="enterShippingDetails" else="processSale"/>
  </decision-state>

  <view-state id="enterShippingDetails" view="shippingDetailsForm">
    <transition on="submit" to="processSale">
      <action bean="formAction" method="bind"/>
    </transition>
  </view-state>

  <action-state id="processSale">
    <bean-action bean="saleProcessor" method="process">
      <method-arguments><argument expression="flowScope.sale"/></method-arguments>
    </bean-action>
    <transition on="success" to="finish"/>
  </action-state>

  <end-state id="finish" view="costOverview">
    <entry-actions><action bean="formAction" method="setupForm"/></entry-actions>
  </end-state>

  <import resource="sellitem-beans.xml"/>
</flow>

關鍵點:flowScope 存放跨請求共享的 Sale 物件;FormAction 處理表單綁定與驗證;action-state 呼叫業務邏輯 SaleProcessor。

FormAction 與 Validator 配置

<bean id="formAction" class="org.springframework.webflow.action.FormAction">
  <property name="formObjectName" value="sale"/>
  <property name="validator">
    <bean class="org.springframework.webflow.samples.sellitem.SaleValidator"/>
  </property>
</bean>

Terracotta 叢集配置

tc-config.xml 聲明式配置

<application>
  <spring>
    <jee-application name="swf-sellitem">
      <session-support>true</session-support>
      <instrumented-classes>
        <include>
          <class-expression>org.springframework.webflow.samples.sellitem.Sale</class-expression>
        </include>
      </instrumented-classes>
    </jee-application>
  </spring>
</application>

啟用 session-support 即可自動叢集 HTTP Session 及其中引用的 Sale 類別實例。

啟動 Tomcat 整合 Terracotta

set JAVA_OPTS=-Xbootclasspath/p:"%DSO_BOOT_JAR%"
set JAVA_OPTS=%JAVA_OPTS% -Dtc.install-root="%TC_INSTALL_DIR%"
set JAVA_OPTS=%JAVA_OPTS% -Dtc.config="%LOCAL_DIR%\tc-config.xml"
  • DSO_BOOT_JAR:指向 dso-boot.jar(Terracotta 安裝目錄 common/lib/dso-boot)。
  • TC_INSTALL_DIR:Terracotta 安裝根目錄。
  • LOCAL_DIR:包含 tc-config.xml 的目錄。

總結

Spring Web Flow 提供強大的有狀態 Web 應用開發模型;Terracotta for Spring 以零代碼修改、聲明式配置賦予其企業級高可用、橫向擴展與透明故障轉移能力。兩者結合,開啟「有狀態、基於會話、可擴展、高可用」Web 應用建構新范式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved