环境搭建 nacos搭建 最新版本快速搭建 使用mysql模式
nacos直接启动即可。控制台默认账号密码是nacos/nacos,mysql账户密码有两个 root/root 和 nacos/nacos。
seata搭建 seata版本1.5.0 快速搭建
seata1.5.0版本直接是一个springboot项目,下载后修改application.yml 文件中注册中心、配置中心、存储模式配置。参考resources/application.example.yml 文件 ,修改后如下
server: port: 7091spring: application: name: seata-serverlogging: config: classpath:logback-spring.xml file: path: ${user.home}/logs/seata extend: logstash-appender: destination: 127.0.0.1:4560 kafka-appender: bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstashconsole: user: username: seata password: seataseata: config: # support: nacos, consul, apollo, zk, etcd3 type: file registry: # support: nacos, eureka, redis, zk, consul, etcd3, sofa type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 namespace: group: seata_group cluster: default username: nacos password: nacos ##if use mse nacos with auth, mutex with username/password attribute #access-key: #secret-key: store: # support: file 、 db 、 redis mode: db db: datasource: druid db-type: mysql driver-class-name: com.mysql.jdbc.driver url: jdbc//127.0.0.1:3306/seata?rewritebatchedstatements=true user: root password: root min-conn: 5 max-conn: 100 global-table: global_table branch-table: branch_table lock-table: lock_table distributed-lock-table: distributed_lock query-limit: 100 max-wait: 5000> 基于 spring boot + mybatis plus + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能>> * 项目地址:> * 视频教程:# server:> 基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能>> * 项目地址:> * 视频教程:# service-port: 8091 #if not configured, the default is '${server.port} + 1000' security: secretkey: seatasecretkey0c382ef121d778043159209298fd40bf3850a017 tokenvalidityinmilliseconds: 1800000 ignore: urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login 创建seata数据库,执行脚本建表
-- -------------------------------- the script used when storemode is 'db' ---------------------------------- the table to store globalsession datacreate table if not exists `global_table`( `xid` varchar(128) not null, `transaction_id` bigint, `status` tinyint not null, `application_id` varchar(32), `transaction_service_group` varchar(32), `transaction_name` varchar(128), `timeout` int, `begin_time` bigint, `application_data` varchar(2000), `gmt_create` datetime, `gmt_modified` datetime, primary key (`xid`), key `idx_status_gmt_modified` (`status` , `gmt_modified`), key `idx_transaction_id` (`transaction_id`)) engine = innodb default charset = utf8mb4;-- the table to store branchsession datacreate table if not exists `branch_table`( `branch_id` bigint not null, `xid` varchar(128) not null, `transaction_id` bigint, `resource_group_id` varchar(32), `resource_id` varchar(256), `branch_type` varchar(8), `status` tinyint, `client_id` varchar(64), `application_data` varchar(2000), `gmt_create` datetime(6), `gmt_modified` datetime(6), primary key (`branch_id`), key `idx_xid` (`xid`)) engine = innodb default charset = utf8mb4;-- the table to store lock datacreate table if not exists `lock_table`( `row_key` varchar(128) not null, `xid` varchar(128), `transaction_id` bigint, `branch_id` bigint not null, `resource_id` varchar(256), `table_name` varchar(32), `pk` varchar(36), `status` tinyint not null default '0' comment '0:locked ,1:rollbacking', `gmt_create` datetime, `gmt_modified` datetime, primary key (`row_key`), key `idx_status` (`status`), key `idx_branch_id` (`branch_id`), key `idx_xid` (`xid`)) engine = innodb default charset = utf8mb4;create table if not exists `distributed_lock`( `lock_key` char(20) not null, `lock_value` varchar(20) not null, `expire` bigint, primary key (`lock_key`)) engine = innodb default charset = utf8mb4;insert into `distributed_lock` (lock_key, lock_value, expire) values ('asynccommitting', ' ', 0);insert into `distributed_lock` (lock_key, lock_value, expire) values ('retrycommitting', ' ', 0);insert into `distributed_lock` (lock_key, lock_value, expire) values ('retryrollbacking', ' ', 0);insert into `distributed_lock` (lock_key, lock_value, expire) values ('txtimeoutcheck', ' ', 0); 启动seata-server,控制台登录页面如下,账号密码为seata/seata。
项目搭建 业务背景 用户购买商品的业务逻辑。整个业务逻辑由3个微服务提供支持:
仓储服务: 对给定的商品扣除仓储数量。 订单服务: 根据采购需求创建订单。 帐户服务: 从用户帐户中扣除余额。 架构 业务表创建 -- ------------------------------ table structure for t_account-- ----------------------------drop table if exists `t_account`;create table `t_account`( `id` int(11) not null auto_increment, `user_id` varchar(255) default null, `amount` double(14, 2) default '0.00', primary key (`id`)) engine=innodb auto_increment=2 default charset=utf8;-- ------------------------------ records of t_account-- ----------------------------insert into `t_account`values ('1', '1', '4000.00');-- ------------------------------ table structure for t_order-- ----------------------------drop table if exists `t_order`;create table `t_order`( `id` int(11) not null auto_increment, `order_no` varchar(255) default null, `user_id` varchar(255) default null, `commodity_code` varchar(255) default null, `count` int(11) default '0', `amount` double(14, 2) default '0.00', primary key (`id`)) engine=innodb auto_increment=64 default charset=utf8;-- ------------------------------ records of t_order-- ------------------------------ ------------------------------ table structure for t_stock-- ----------------------------drop table if exists `t_stock`;create table `t_stock`( `id` int(11) not null auto_increment, `commodity_code` varchar(255) default null, `name` varchar(255) default null, `count` int(11) default '0', primary key (`id`), unique key `commodity_code` (`commodity_code`)) engine=innodb auto_increment=2 default charset=utf8;-- ------------------------------ records of t_stock-- ----------------------------insert into `t_stock`values ('1', 'c201901140001', '水杯', '1000');-- ------------------------------ table structure for undo_log-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log-- ----------------------------drop table if exists `undo_log`;create table `undo_log`( `id` bigint(20) not null auto_increment, `branch_id` bigint(20) not null, `xid` varchar(100) not null, `context` varchar(128) not null, `rollback_info` longblob not null, `log_status` int(11) not null, `log_created` datetime not null, `log_modified` datetime not null, primary key (`id`), unique key `ux_undo_log` (`xid`,`branch_id`)) engine=innodb auto_increment=1 default charset=utf8;-- ------------------------------ records of undo_log-- ----------------------------setforeign_key_checks=1; 服务创建 业务服务 以order服务为例,引入依赖、配置参数、提供创建订单接口。
pom.xml文件中引入依赖
org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-seata com.alibaba druid com.baomidou mybatis-plus-boot-starter mysql mysql-connector-java application.properties配置参数
server.port=81spring.application.name=orderspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848spring.cloud.nacos.username=nacosspring.cloud.nacos.password=nacosspring.datasource.type=com.alibaba.druid.pool.druiddatasourcespring.datasource.driver-class-name=com.mysql.cj.jdbc.driverspring.datasource.url=jdbc//127.0.0.1:3306/seata_samples?allowpublickeyretrieval=true&usessl=false&useunicode=true&characterencoding=utf-8&allowmultiqueries=truespring.datasource.username=rootspring.datasource.password=rootmybatis-plus.mapper-locations=classpath:mapper/*.xmlmybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.stdoutimpl 提供创建订单接口
@requestmapping(/add) public void add(string userid, string commoditycode, integer count, bigdecimal amount) { order order = new order(); order.setorderno(uuid.randomuuid().tostring()); order.setuserid(userid); order.setamount(amount); order.setcommoditycode(commoditycode); order.setcount(count); orderservice.save(order); } 聚合服务 business服务远程调用仓储、订单、帐户服务,完成下单流程。
1.pom.xml文件中引入依赖
org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-seata com.alibaba druid org.springframework.cloud spring-cloud-starter-openfeign 2.application.properties配置参数
server.port=80spring.application.name=businessspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848spring.cloud.nacos.username=nacosspring.cloud.nacos.password=nacosspring.datasource.type=com.alibaba.druid.pool.druiddatasourcespring.datasource.driver-class-name=com.mysql.cj.jdbc.driverspring.datasource.url=jdbc//127.0.0.1:3306/seata_samples?allowpublickeyretrieval=true&usessl=false&useunicode=true&characterencoding=utf-8&allowmultiqueries=truespring.datasource.username=rootspring.datasource.password=rootservice.disableglobaltransaction=false# 连接超时时间ribbon.connecttimeout=3000# 响应超时时间ribbon.readtimeout=5000 3.声明account、stock、order的feign接口。
@feignclient(value = account)public interface accountfeign { @requestmapping(/account/reduce) public void reduce(@requestparam(userid) string userid, @requestparam(amount) bigdecimal amount);}@feignclient(value = order)public interface orderfeign { @requestmapping(/order/add) public void add(@requestparam(userid) string userid, @requestparam(commoditycode) string commoditycode, @requestparam(count) integer count, @requestparam(amount) bigdecimal amount);}@feignclient(value = stock)public interface stockfeign { @requestmapping(/stock/deduct) public void deduct(@requestparam(commoditycode) string commoditycode, @requestparam(count) integer count);} 4.全局事务开启,调用feign接口
@autowiredprivate orderfeign orderfeign;@autowiredprivate stockfeign stockfeign;@autowiredprivate accountfeign accountfeign;@globaltransactional@requestmapping(/toorder)public void toorder(string userid, string commoditycode, integer count, bigdecimal amount) { accountfeign.reduce(userid, amount); stockfeign.deduct(commoditycode, count); orderfeign.add(userid, commoditycode, count, amount);} 测试验证 当前资金账户4000,库存1000,模拟用户购买商品2000个,消费4000,业务调用后,数据库数据状态应该如下:
用户资金满足4000,数据库更新用户资金为0。 商品库存不满足2000个,异常。 business服务提交全局回滚。 资金服务回滚操作,更新资金为4000。 验证:
1.浏览器输入地址请求访问
http://127.0.0.1/business/toorder?userid=1&commoditycode=c201901140001&count=2000&amount=4000 2.account、stock服务日志观察。
3.数据库数据依然为原始状态。
注意事项 1.seata1.5版本的mysql驱动是5.7,需要为8,在libs文件夹删除mysql-connector-java-5.xx.jar,替换mysql-connector-java-8.xx.jar即可
2.spring boot &spring cloud&spring cloud alibaba版本兼容问题
spring cloud alibaba版本说明
3.druid和数据驱动版本兼容
通过druid仓库版本查看各依赖版本说明
代码仓库 https://gitee.com/codewbg/springcloud_alibaba
关于电子-电路板组件的分析和介绍
关于汽车电动转向控制系统方案的分析
电解电容可以用无极电容代替吗
伺服传感器是如何进行工作的?
蓝牙耳机什么牌子好?买完回家当宝的五大蓝牙耳机
保姆级教程:Spring Cloud 集成Seata分布式事务
索尼推出售价700美元的电子纸平板电脑
全新英特尔开放式FPGA开发堆栈使定制平台开发变得更轻松
中关村示范区在5G等方面已取得一系列进展
ARM是什么东西?
电动汽车用铅酸电池好还是用锂电池好
创业必看:一亿、十亿、百亿公司在创业时要怎么去做
阿科玛新型电解液添加剂LiTDI能提升电池寿命
LG公司推出大尺寸电视LG MAGNIT,具有防眩光和防指纹功能
ArkUI,更高效的框架设计
iphone 12跌破发行价,iphone12降价的原因是什么
欧菲光转让深圳科技园部分房屋及土地使用权,预估交易总额
微软的增强现实全息透镜技术被用来进行下肢手术
Aurora的自动驾驶系统成为今年的自动机械人原型
快速了解运放的输入偏置电压Vos和输入偏置电压平均漂移TCVos参数(1)