最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中seata框架的XA模式詳解

 更新時間:2023年08月30日 08:45:30   作者:等後那場雪  
這篇文章主要介紹了Java中seata框架的XA模式詳解,Seata?是一款開源的分布式事務解決方案,致力于提供高性能和簡單易用的分布式事務服務,Seata?將為用戶提供了?AT、TCC、SAGA?和?XA?事務模式,為用戶打造一站式的分布式解決方案,需要的朋友可以參考下

XA模式

XA 模式屬于一種強一致性的事務模式。

前提

支持 XA 模式的數(shù)據(jù)庫。

Java 應用通過 JDBC 訪問數(shù)據(jù)庫。

整體機制

在 Seata 定義的分布式事務框架內,利用事務資源(數(shù)據(jù)庫、消息服務等)對 XA 協(xié)議提供可回滾、持久化的支持,使用 XA 協(xié)議的機制來管理分支事務。

img

執(zhí)行階段

執(zhí)行 XA Start、業(yè)務 SQL、XA End =》注冊分支,XA Prepare => 報告分支事務的狀態(tài)。

完成階段

執(zhí)行 XA Commit / XA Rollback 操作進行分支事務的提交或者回滾。

XA 模式需要 XAConnection,而獲取 XAConnection 的方式有兩種:

  • 方式一、要求開發(fā)者配置 XADataSource。給開發(fā)者增加了認知負擔,需要為 XA 模式專門去學習和使用 XA 數(shù)據(jù)源,與透明化 XA 編程模型的設計目標相悖。
  • 方式二、根據(jù)開發(fā)者的普通 DataSource 來創(chuàng)建。對開發(fā)者比較友好,和 AT 模式一樣,開發(fā)者完全不需要關心 XA 層面的任何問題,保持本地編程模型即可。

優(yōu)先設計實現(xiàn)第二種方式,數(shù)據(jù)源代理根據(jù)普通數(shù)據(jù)源中獲取的普通 JDBC 連接創(chuàng)建出相應的 XAConnection。

類比 AT 模式的數(shù)據(jù)源代理機制,如下:

ds1

但是,第二種方法有局限:無法保證兼容的正確性。

實際上,這種方法是在做數(shù)據(jù)庫驅動程序要做的事情。不同的廠商、不同版本的數(shù)據(jù)庫驅動實現(xiàn)機制是廠商私有的,我們只能保證在充分測試過的驅動程序上是正確的,開發(fā)者使用的驅動程序版本差異很可能造成機制的失效。

綜合考慮,XA 模式的數(shù)據(jù)源代理設計需要同時支持第一種方式:基于 XA 數(shù)據(jù)源進行代理。

類比 AT 模式的數(shù)據(jù)源代理機制,如下:

ds2

使用方法

每個服務的 file.conf、registry.conf 配置文件的配置這里先不提供。

Business服務
|
|------> Stock服務
|
|------> Order服務 -----> Account服務

Business服務

BusinessService

@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount, boolean rollback) {
    String xid = RootContext.getXID();
    LOGGER.info("New Transaction Begins: " + xid);
    String result = stockFeignClient.deduct(commodityCode, orderCount);
    if (!SUCCESS.equals(result)) {
        throw new RuntimeException("庫存服務調用失敗,事務回滾!");
    }
    result = orderFeignClient.create(userId, commodityCode, orderCount);
    if (!SUCCESS.equals(result)) {
        throw new RuntimeException("訂單服務調用失敗,事務回滾!");
    }
    if (rollback) {
        throw new RuntimeException("Force rollback ... ");
    }
}

BusinessXADataSourceConfiguration

@Configuration
public class BusinessXADataSourceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource dataSource() {
        return new DruidDataSource();
    }
}

Stock服務

StockBusiness

public void deduct(String commodityCode, int count) {
    String xid = RootContext.getXID();
    LOGGER.info("deduct stock balance in transaction: " + xid);
    jdbcTemplate.update("update seata_stock set count = count - ? where commodity_code = ?",
        new Object[] {count, commodityCode});
}

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.211.55.6:3306/seata_stock?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root

StockXADataSourceConfiguration

@Configuration
public class StockXADataSourceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
    @Bean("dataSourceProxy")
    public DataSource dataSource(DruidDataSource druidDataSource) {
        return new DataSourceProxyXA(druidDataSource);
    }
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(DataSource dataSourceProxy) {
        return new JdbcTemplate(dataSourceProxy);
    }
}

Order服務

OrderService

public void create(String userId, String commodityCode, Integer count) {
    String xid = RootContext.getXID();
    LOGGER.info("create order in transaction: " + xid);
    // 定單總價 = 訂購數(shù)量(count) * 商品單價(100)
    int orderMoney = count * 100;
    // 生成訂單
    jdbcTemplate.update("insert seata_order(user_id,commodity_code,count,money) values(?,?,?,?)",
        new Object[] {userId, commodityCode, count, orderMoney});
    // 調用賬戶余額扣減
    String result = accountFeignClient.reduce(userId, orderMoney);
    if (!SUCCESS.equals(result)) {
        throw new RuntimeException("Failed to call Account Service. ");
    }
}

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.211.55.6:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root

OrderXADataSourceConfiguration

@Configuration
public class OrderXADataSourceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
    @Bean("dataSourceProxy")
    public DataSource dataSource(DruidDataSource druidDataSource) {
        return new DataSourceProxyXA(druidDataSource);
    }
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(DataSource dataSourceProxy) {
        return new JdbcTemplate(dataSourceProxy);
    }
}

Account服務

AccountService

@Transactional
public void reduce(String userId, int money) {
    String xid = RootContext.getXID();
    LOGGER.info("reduce account balance in transaction: " + xid);
    jdbcTemplate.update("update seata_account set money = money - ? where user_id = ?", new Object[] {money, userId});
    int balance = jdbcTemplate.queryForObject("select money from seata_account where user_id = ?",
        new Object[] {userId}, Integer.class);
    LOGGER.info("balance after transaction: " + balance);
    if (balance < 0) {
        throw new RuntimeException("Not Enough Money ...");
    }
}

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.211.55.6:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root

AccountXADataSourceConfiguration

@Configuration
public class AccountXADataSourceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
    @Bean("dataSourceProxy")
    public DataSource dataSource(DruidDataSource druidDataSource) {
        return new DataSourceProxyXA(druidDataSource);
    }
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(DataSource dataSourceProxy) {
        return new JdbcTemplate(dataSourceProxy);
    }
    @Bean
    public PlatformTransactionManager txManager(DataSource dataSourceProxy) {
        return new DataSourceTransactionManager(dataSourceProxy);
    }
}

啟動類需要標注 @EnableTransactionManagement 注解。

到此這篇關于Java中seata框架的XA模式詳解的文章就介紹到這了,更多相關seata框架的XA模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中使用HashMap時指定初始化容量性能解析

    Java中使用HashMap時指定初始化容量性能解析

    這篇文章主要為大家介紹了Java中使用HashMap時指定初始化容量性能解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • springboot連接不上redis的三種解決辦法

    springboot連接不上redis的三種解決辦法

    這篇文章主要介紹了springboot連接不上redis的三種解決辦法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 基于Hutool的圖片驗證碼功能模塊實現(xiàn)

    基于Hutool的圖片驗證碼功能模塊實現(xiàn)

    為了提高系統(tǒng)的安全性,防止接口被暴力刷新,驗證碼是個好的手段,圖片驗證碼沒有短信驗證碼的費用,其是個人開發(fā)者學習的重點,這篇文章主要介紹了基于Hutool的圖片驗證碼功能模塊實現(xiàn),需要的朋友可以參考下
    2022-10-10
  • 使用Java實現(xiàn)一個解析CURL腳本小工具

    使用Java實現(xiàn)一個解析CURL腳本小工具

    文章介紹了如何使用Java實現(xiàn)一個解析CURL腳本的工具,該工具可以將CURL腳本中的Header解析為KV Map結構,獲取URL路徑、請求類型,解析URL參數(shù)列表和Body請求體,感興趣的小伙伴跟著小編一起來看看吧
    2025-02-02
  • 解決mybatis一對多關聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題

    解決mybatis一對多關聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題

    這篇文章主要介紹了解決mybatis一對多關聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • springboot的四種啟動方式

    springboot的四種啟動方式

    本文主要介紹了springboot的四種啟動方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • java使用Cookie判斷用戶登錄情況的方法

    java使用Cookie判斷用戶登錄情況的方法

    這篇文章主要為大家詳細介紹了java使用Cookie判斷用戶登錄情況,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 關于java中多個JDK和切換版本介紹

    關于java中多個JDK和切換版本介紹

    大家好,本篇文章主要講的是關于java中多個JDK和切換版本介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • SpringBoot @Cacheable自定義KeyGenerator方式

    SpringBoot @Cacheable自定義KeyGenerator方式

    這篇文章主要介紹了SpringBoot @Cacheable自定義KeyGenerator方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結構教程)

    使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結構教程)

    這篇文章主要介紹了使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結構教程)的相關資料,需要的朋友可以參考下
    2016-03-03

最新評論

洛隆县| 邵阳县| 石首市| 左权县| 凤山县| 龙里县| 什邡市| 临邑县| 兴文县| 鱼台县| 宁南县| 宜阳县| 固安县| 扎囊县| 马鞍山市| 肇东市| 武鸣县| 北海市| 明溪县| 墨玉县| 米脂县| 金川县| 万荣县| 雅江县| 中阳县| 武穴市| 岳阳县| 视频| 无棣县| 靖边县| 项城市| 云霄县| 同仁县| 田林县| 尼勒克县| 乐山市| 娱乐| 金门县| 苍山县| 巩留县| 邯郸市|