COSCUP
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 使用 Spring Cloud Contract 實踐事件驅動的微服務契約測試 {%hackmd @coscup/announcement-2025 %} > [投影片](https://drive.google.com/file/d/1EtTghcPDL_7GvnmjgqC-eW4_FocDYlIj/view?usp=sharing) ## 1. 傳統微服務整合困境 ### 共用資料庫 **優點** - 資料一致性高 - 開發初期整合成本低 **缺點** - 資料庫 schema 變更影響所有服務 - 擴展性差,難以分散負載 - 無法真正實現服務自治 --- ### 各服務獨立資料庫,透過 HTTP 溝通 **優點** - 服務自治,資料耦合度低 - 可以獨立部署與擴展 **缺點** - 高度耦合於 API 規格 - API 變更需同步協調 - 測試與維護成本上升 --- ## 2. 事件驅動架構 (EDA) **副標題**:透過事件傳遞訊息,實現服務間鬆耦合 - 以事件為核心,服務間透過消息系統(Kafka、RabbitMQ 等)交換資訊 - Producer 發布事件,Consumer 訂閱事件 - 提高擴展性與可維護性 --- ## 3. SAGA 模式處理交易一致性 ### Choreography(編舞式) - 無中央協調者,服務依序觸發事件完成交易 - 自治性高,擴展性佳 - 缺點:流程分散,觀察與管理較難 ### Orchestration(編排式) - 有中央 Orchestrator 控制流程 - 流程可觀測、錯誤補償集中 - 缺點:中央節點成為單點風險,耦合度高 --- ## 4. 落實微服務的建築藍圖 ### DDD 與 Clean Architecture - **DDD**:以業務為中心,劃分邊界,建立領域模型 - **Clean Architecture**:依賴方向指向內層,確保業務邏輯不依賴技術細節 --- ### Food Ordering System 範例 - Order Service(唯一對外 HTTP API) - Payment Service(事件消費) - Restaurant Service(事件消費) - 透過 Kafka Event Channel 互通 --- ### 測試策略:E2E 痛點 **E2E 常見問題** - 🐢 效能低:需啟動完整系統,耗時長 - 🔥 脆弱性高:任一服務異常即測試失敗 - 💸 維護成本高:流程或 API 變更需大量改測 - 🧱 部署困難:依賴上下游與完整環境 --- ## 5. Spring Cloud Contract (SCC) 簡介 - 契約測試框架,支援 REST API 與 Messaging - Consumer-Driven Contract:由消費者定義需求,生產者驗證 - 減少整合測試成本,提前發現介面變更風險 --- ## 6. SCC 契約測試流程 1. Producer 撰寫 Groovy Contract 2. SCC 產生並執行 Producer 測試(驗證 response 與事件) 3. 發佈 stub 4. Consumer 引入 stub,使用 StubRunner 模擬事件 5. 驗證 Consumer 處理邏輯與資料寫入 --- ### 案例:訂單建立成功發送事件 ### Producer 測試 - 合約定義 request、response 與 outputMessage - `triggeredBy("createOrder()")`:告訴 SCC 呼叫 ContractTestBase 方法 - KafkaMessageVerifierReceiver:攔截並驗證事件 ### Consumer 測試 - 引入 Producer 發佈的 stub - StubTrigger 觸發事件 - 驗證 Consumer 的業務邏輯與資料落庫 --- ## 7. 收尾 - 契約測試可在開發階段驗證服務邊界 - 減少 E2E 測試的負擔與風險 - 提高跨服務協作的穩定性與可追溯性 ###### tags: `COSCUP2025`, `zhtw`, `intermediate`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully