- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
异常拦截器ExceptionMapper 在JAX-RS(Java API for RESTful Web Services)中,ExceptionMapper接口用于将Java异常映射到HTTP响
1、RPC原理 一次完整的RPC调用流程如下: 1)服务消费方(client)调用以本地调用方式调用服务; 2)client stub接收到调用后负责将方法、参数等组装成能够进行网络传输的消息体; 3
Dubbo 什么是Dubbo dubbo:是一个基于soa思想的rpc框架 soa思想:面向服务的架构 给每一个模块暴露对应的ip和端口,当做一个服务进行运行 重点在于服务的管理(负载均衡,容灾模式,
分布式框架-Dubbo Dubbo与spring整合!! 导入坐标 org.apache.dubbo dubbo 3.0.6 org.apache.cu
分布式基础理论 1. 什么是分布式系统? 分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个系统 2. 应用架构演变 单一应用架构 当网站流量很小时,只需一个应用,将所有功能都部署在一起
一、基础知识 1. 为什么要用 Dubbo? 随着服务化的进一步发展,服务越来越多,服务之间的调用和依赖关系也越来越复杂,诞生了面向服务的架构体系(SOA),也因此衍生出了一系列相应的技术,如对服
前言 在Dubbo中有Filter使用,对于Filter来说我们会遇到这样的问题,Filter自身有很多的实现,我们希望某种条件下使用A实现,另外情况下使用B实现,这个时候
前言 Dubbo源码阅读分享系列文章,欢迎大家关注点赞 SPI实现部分 Dubbo-SPI机制
dubbo logback输出日志 两者皆为国内使用比较广的两个开源框架:一个是服务治理框架,另一个则是日志框架。这个问题的解决方案一部分就在dubbo官网上: dubbo的日志适配 缺省自动
关于dubbo的配置使用已经配置好了简单的示例,下面先记录下dubbo管理控制台的安装和使用(用的zookeeper的注册中心),在网上找了些按照示例 dubbo管理控制台开源部分主要包含: 提供
Dubbo 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动式计算架构势在必行,急需一个治理系统确保架构有条不紊的演进。 单一应用架构 当网站流量很小时,
在dubbo-admin-0.0.1-SNAPSHOT.jar的路径下启动dubbo,java -jar dubbo-admin-0.0.1-SNAPSHOT.jar dubbo-admin-
1、下载tomcat 镜像 1
javax.validation和jakarta.validation都是用于Java中进行数据验证(validation)的相关API,它们提供了一套标准的验证框架,用于验证Java对象的属性是否符
一般情况我们使用dubbo)通过rpc调用dubbo提供方的服务,首先要在消费者的项目中引入接口提供者的jar包(provider端暴露的接口和方法),然后使用jar包里面的类和方法,两端才能正常
Zipkin是一个分布式跟踪系统。它有助于收集解决服务体系结构中的延迟问题所需的计时数据。功能包括此数据的收集和查找。 如果日志文件中有跟踪 ID,则可以直接跳转到该 ID。否则,您可以根据服务、操
先说两句 我常常在散步时思考很多技术上的「为什么问题」,有时一个问题会想很久,直到问题的每一个点都能说服自己时,才算完结。于是想把这些思考记录下来,形成文章,可以当做一个新的系列。这些
大家可以自行百度下阿里分布式事务,在这里我就不啰嗦了。下面是阿里分布式事务开源框架的一些资料,本文是springboot+dubbo+fescar的集成。 快速开始 https://github
昨天发布了注解方式,有人给我发了邮件希望能出一版本xml格式的,本来12点前能搞定的但是电脑稍微出了问题,导致idea 疯狂奔溃,搞了很久废话不多说了,有错误之处望大家指出发我邮箱. 用dubbo
公司后端服务使用 java 重构后,很多接口采用了阿里的 dubbo 协议。而 python 是无法直接调用 dubbo 接口的,但可以通过 telnet 调用,具体可以通过 telnetlib
我是一名优秀的程序员,十分优秀!