gpt4 book ai didi

Springboot-dubbo-fescar 阿里分布式事务的实现方法

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 33 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Springboot-dubbo-fescar 阿里分布式事务的实现方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

大家可以自行百度下阿里分布式事务,在这里我就不啰嗦了。下面是阿里分布式事务开源框架的一些资料,本文是springboot+dubbo+fescar的集成.

快速开始 。

https://github.com/alibaba/fescar/wiki/quick-start 。

git地址 。

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

1、sql 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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,并启动 。

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

解压 。

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

win操作系统直接启动 。

?
1
fescar-server.bat

3、下载zookeeper并启动.

4、demo结构 。

分为库存微服务storage、账号微服务account、订单微服务order和购买下单的消费者purchase.

购买流程如下,先调用storage微服务减库存,然后调用order微服务更新账户余额和生成订单。订单微服务order也作为消费者消费account账号微服务,账号微服务主要用来更新账号余额.

5、demo地址如下 。

https://github.com/talkischeapgivememoney/springboot-dubbo-fescar.git 。

6、依次运行storage微服务、account微服务和order微服务、purchase消费者.

我们在创建订单的时候模拟异常,具体类如下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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());
 
     // 计算订单金额
     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;
   }
}

在购买下单之前确认下数据库中的数据,以便购买后对比,商品单价200. 。

我们看到 account_tbl 账号表,用户u100001账户余额为9999,库存表storage_tbl,商品库存是100.订单表order_tbl没有订单数据.

下面我们执行购买下单操作, 。

执行以下url 。

http://127.0.0.1:9094/purchase?userid=u100001&commoditycode=c00321&ordercount=4 。

userid 用户id,commoditycode  商品编号,ordercount 购买数量.

再去,返回数据如下 。

?
1
2
3
4
5
6
7
{
   "timestamp" : "2019-02-01t02:54:27.422+0000" ,
   "status" : 500 ,
   "error" : "internal server error" ,
   "message" : "for input string: \"2u\"" ,
   "path" : "/purchase"
}

查看上面提到的account_tbl 账号表,用户u100001账户余额为9999,库存表storage_tbl,商品库存是100.订单表order_tbl没有订单数据。说明分布式事务成功.

我们把模拟的异常去掉,再次执行购买操作.

http://127.0.0.1:9094/purchase?userid=u100001&commoditycode=c00321&ordercount=5 。

我们发现商品库存由100变成了95,用户余额由9999变成了8999,并且生成了一个订单号,前面提到了商品的单价是200,数据正确.

其他的分布式事务框架,比如tcc和bytetcc需要操作幂等性,了解tcc和bytetcc的应该知道,开发工作量很大而且操作还需要幂等性,而阿里的fescar则完全不需要,而且不需要那么多的开发工作量,希望fescar越来越成熟.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/zsj777/article/details/86702338 。

最后此篇关于Springboot-dubbo-fescar 阿里分布式事务的实现方法的文章就讲到这里了,如果你想了解更多关于Springboot-dubbo-fescar 阿里分布式事务的实现方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com