欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法

 更新時(shí)間:2019年03月29日 09:29:19   作者:NemoWZ  
這篇文章主要介紹了Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

大家可以自行百度下阿里分布式事務(wù),在這里我就不啰嗦了。下面是阿里分布式事務(wù)開源框架的一些資料,本文是springboot+dubbo+fescar的集成。

快速開始

https://github.com/alibaba/fescar/wiki/Quick-Start

GIT地址

https://github.com/alibaba/fescar

1、sql

CREATE TABLE `undo_log` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `branch_id` bigint(20) NOT NULL,
 `xid` varchar(100) NOT NULL,
 `rollback_info` longblob NOT NULL,
 `log_status` int(11) NOT NULL,
 `log_created` datetime NOT NULL,
 `log_modified` datetime NOT NULL,
 `ext` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_unionkey` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8;
 
DROP TABLE IF EXISTS `storage_tbl`;
CREATE TABLE `storage_tbl` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `commodity_code` varchar(255) DEFAULT NULL,
 `count` int(11) DEFAULT 0,
 PRIMARY KEY (`id`),
 UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` varchar(255) DEFAULT NULL,
 `commodity_code` varchar(255) DEFAULT NULL,
 `count` int(11) DEFAULT 0,
 `money` int(11) DEFAULT 0,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
 
DROP TABLE IF EXISTS `account_tbl`;
CREATE TABLE `account_tbl` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` varchar(255) DEFAULT NULL,
 `money` int(11) DEFAULT 0,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `storage_tbl` VALUES ('1', 'C00321', '100'); 
INSERT INTO `account_tbl` VALUES ('1', 'U100001', '9999');

2、下載fescar-server,并啟動(dòng)

https://github.com/alibaba/fescar/releases

解壓

sh fescar-server.sh 8091 /home/admin/fescar/data/

win操作系統(tǒng)直接啟動(dòng)

fescar-server.bat

3、下載zookeeper并啟動(dòng)。

4、demo結(jié)構(gòu)

分為庫(kù)存微服務(wù)storage、賬號(hào)微服務(wù)account、訂單微服務(wù)order和購(gòu)買下單的消費(fèi)者purchase。

購(gòu)買流程如下,先調(diào)用storage微服務(wù)減庫(kù)存,然后調(diào)用order微服務(wù)更新賬戶余額和生成訂單。訂單微服務(wù)order也作為消費(fèi)者消費(fèi)account賬號(hào)微服務(wù),賬號(hào)微服務(wù)主要用來(lái)更新賬號(hào)余額。

5、demo地址如下

https://github.com/TalkIsCheapGiveMeMoney/springboot-dubbo-fescar.git

6、依次運(yùn)行storage微服務(wù)、account微服務(wù)和order微服務(wù)、purchase消費(fèi)者。

我們?cè)趧?chuàng)建訂單的時(shí)候模擬異常,具體類如下

package com.hcsoc.order.service; 
import com.alibaba.fescar.core.context.RootContext;
import com.hcsoc.account.api.AccountService;
import com.hcsoc.order.api.OrderService;
import com.hcsoc.order.api.bean.Order;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
@Setter
public class OrderServiceImpl implements OrderService{
 
  private static final Logger LOGGER = LoggerFactory.getLogger(OrderService.class);
  @Autowired
  private AccountService accountService;
  @Autowired
  private JdbcTemplate jdbcTemplate;
 
 
  public Order create(String userId, String commodityCode, int orderCount) {
    LOGGER.info("Order Service Begin ... xid: " + RootContext.getXID());
 
    // 計(jì)算訂單金額
    int orderMoney = calculate(commodityCode, orderCount);
 
    // 從賬戶余額扣款
    accountService.debit(userId, orderMoney);
 
    final Order order = new Order();
    order.userId = userId;
    order.commodityCode = commodityCode;
    order.count = orderCount;
    order.money = orderMoney;
    //模擬異常
    Integer.parseInt("2u");
 
    KeyHolder keyHolder = new GeneratedKeyHolder();
 
    jdbcTemplate.update(new PreparedStatementCreator() {
 
      public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
        PreparedStatement pst = con.prepareStatement(
            "insert into order_tbl (user_id, commodity_code, count, money) values (?, ?, ?, ?)",
            PreparedStatement.RETURN_GENERATED_KEYS);
        pst.setObject(1, order.userId);
        pst.setObject(2, order.commodityCode);
        pst.setObject(3, order.count);
        pst.setObject(4, order.money);
        return pst;
      }
    }, keyHolder);
 
    order.id = keyHolder.getKey().longValue();
 
    LOGGER.info("Order Service End ... Created " + order);
 
    return order;
  }
 
  private int calculate(String commodityId, int orderCount) {
    return 200 * orderCount;
  }
}

在購(gòu)買下單之前確認(rèn)下數(shù)據(jù)庫(kù)中的數(shù)據(jù),以便購(gòu)買后對(duì)比,商品單價(jià)200.

我們看到 account_tbl 賬號(hào)表,用戶U100001賬戶余額為9999,庫(kù)存表storage_tbl,商品庫(kù)存是100.訂單表order_tbl沒(méi)有訂單數(shù)據(jù)。

下面我們執(zhí)行購(gòu)買下單操作,

執(zhí)行以下url

http://127.0.0.1:9094/purchase?userId=U100001&commodityCode=C00321&orderCount=4

userId 用戶id,commodityCode  商品編號(hào),orderCount 購(gòu)買數(shù)量。

再去,返回?cái)?shù)據(jù)如下

{
  "timestamp": "2019-02-01T02:54:27.422+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "For input string: \"2u\"",
  "path": "/purchase"
}

查看上面提到的account_tbl 賬號(hào)表,用戶U100001賬戶余額為9999,庫(kù)存表storage_tbl,商品庫(kù)存是100.訂單表order_tbl沒(méi)有訂單數(shù)據(jù)。說(shuō)明分布式事務(wù)成功。

我們把模擬的異常去掉,再次執(zhí)行購(gòu)買操作。

http://127.0.0.1:9094/purchase?userId=U100001&commodityCode=C00321&orderCount=5

我們發(fā)現(xiàn)商品庫(kù)存由100變成了95,用戶余額由9999變成了8999,并且生成了一個(gè)訂單號(hào),前面提到了商品的單價(jià)是200,數(shù)據(jù)正確。

其他的分布式事務(wù)框架,比如TCC和byteTcc需要操作冪等性,了解TCC和ByteTcc的應(yīng)該知道,開發(fā)工作量很大而且操作還需要冪等性,而阿里的fescar則完全不需要,而且不需要那么多的開發(fā)工作量,希望fescar越來(lái)越成熟。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論