SpringCloud + Seata1.5.0(使用docker安装配置Seata;数据存储mysql、配置中心与注册中心nacos)
1、seata介绍
Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
四种模式对比(默认使用AT模式)
特性 XA AT TCC SAGA
一致性 强一致 弱一致 弱一致 最终一致
隔离性 完全隔离 基于全局锁隔离 基于资源预留隔离 无隔离
代码侵入 无 无 有 要编写三个接口 有,要编写状态机和补偿业务
性能 差 好 非常好 非常好
试用场景
XA:对一致性、隔离性有高要求的业务
AT:基于关系型数据库的大多数分布式事务场景都可以
TCC:对性能要求较高的事务。有非关系型数据库要参与的事务。
SAGA:业务流程长、业务流程多参与者包含其它公司或遗留系统服务,无法提供 TCC 模式要求的三个接口
2、seata术语
TC (Transaction Coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。
TM (Transaction Manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。
RM (Resource Manager) - 资源管理器
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚
3、docker部署seata-1.5.0
seata版本 1.4启动配置文件与1.5. 不一样,1.4使用到了file.conf和registry.conf,而1.5.0以后的版本只用到了application.yml 一个配置文件,本案例使用1.5.0
3.1:拉取docker镜像
docker pull seataio/seata-server:1.5.0
3.2:在mysql数据库中创建seate所需要的表
-- the table to store GlobalSession data
CREATE 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_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- the table to store BranchSession data
CREATE 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 = utf8;
-- the table to store lock data
CREATE 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),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
3.3:创建配置文件,启动seata获取配置文件
docker run -d --name seata-server -p 8091:8091 seataio/seata-server:1.5.0
3.4:将容器内的配置文件拷贝到宿主机目录(创建宿主机目录:mkdir /home/seata)
docker cp seata-server:/seata-server/resources /home/seata
3.5:修改/home/seata/seata-server/resources/application.yaml文件,配置nacos注册中心与配置中心,配置mysql为store存储
server:
port: 7091
spring:
application:
name: seata-server
logging:
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_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 192.168.168.210:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
##if use MSE Nacos with auth, mutex with username/password attribute
# access-key: ""
# secret-key: ""
data-id: seataServer.propertie
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
preferred-networks: 30.240.*
nacos:
application: seata-server
server-addr: 192.168.168.210:8848
group: SEATA_GROUP
namespace:
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:mysql://192.168.168.194: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
# server:
# 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
3.6:删除之前容器(docker rm -f seata-server)重新使用docker启动Seata
docker run -d --name seata-server -p 8091:8091 -e SEATA_IP=192.168.168.200 -v /home/seata:/seata-server seataio/seata-server:1.5.0
3.7:查看启动log,docker logs -f seata-server
3.8:nacos注册中心查看服务列表seata-server是否注册成功
4、SpringCloud 整合Seata项目
4.1:项目架构
4.1:引入pom.xml依赖
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<!--seata支持YML配置-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version}</version>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
4.2、seata-account-samples yaml 与其他两个服务配置基本差不多(seata的配置中心与注册中心必须和seata-server保持一致)
server:
port: 8082
spring:
application:
name: seata-account-samples
cloud:
nacos:
discovery:
#nacos服务地址
server-addr: 192.168.168.210:8848
#nacos命名空间ID 30d9ed4b-2d03-46e3-a37d-5fd3796aaa8f
namespace: ""
alibaba:
seata:
#事务群组,要和下方vgroup-mapping保持一致(可以每个应用独立取名,也可以使用相同的名字),要与服务端nacos-config.txt中service.vgroup_mapping中存在,并且要保证多个群组情况下后缀名要保持一致-tx_group
tx-service-group: ${spring.application.name}-tx_group
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata_account?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
seata:
application-id: ${spring.application.name}
#数据源代理模式使用AT模式(可以不用配置,seata默认使用AT模式)
data-source-proxy-mode: AT
#事务群组(可以每个应用独立取名,也可以使用相同的名字),要与服务端nacos-config.txt中service.vgroup_mapping中存在,并且要保证多个群组情况下后缀名要保持一致-tx_group
service:
vgroup-mapping:
seata-account-samples-tx_group: default
registry:
type: nacos
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: nacos
password: nacos
#seata分组名称
group: SEATA_GROUP
#nacos命名空间ID
namespace: ""
#seata服务名
application: seata-server
config:
type: nacos
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: nacos
password: nacos
#seata分组名称
group: SEATA_GROUP
#nacos命名空间ID
namespace: ""
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.3:seata-account-samples扣减余额业务业务,此处模拟异常发生,全局事务回滚
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{
@Autowired
private AccountMapper accountMapper;
private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);
@Override
public void decrease(Long userId, BigDecimal money) {
System.out.println("seata-account-samples中扣减账户余额开始");
LOGGER.info("------->seata-account-samples中扣减账户余额开始-------<");
//模拟超时异常,全局事务回滚
try {
// 这段代码的主要作用就是 给你时间去看数据库的变化
Thread.sleep(20*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这段代码就是模拟 发生异常
System.out.println(1/0);
accountMapper.decrease(userId,money);
System.out.println("seata-account-samples中扣减账户余额结束");
LOGGER.info("------->seata-account-samples中扣减账户余额结束-------<");
}
4.4:seata-storage-samples 扣减库存业务
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {
private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class);
@Autowired
private StorageMapper storageMapper;
@Override
public void decrease(Long productId, Integer count) {
System.out.println("seata-storage-samples中扣减库存开始");
LOGGER.info("------->seata-storage-samples中扣减库存开始-------<");
storageMapper.decrease(productId, count);
System.out.println("seata-storage-samples中扣减库存结束");
LOGGER.info("------->seata-storage-samples中扣减库存结束-------<");
}
}
4.5:seata-order-samples rpc远程调用扣减库存与扣减金额
@Repository
@FeignClient(value = "seata-account-samples")
public interface AccountFeignClient {
// 扣减账户余额
@PostMapping("/account/decrease/{userId}/{money}")
CommonResult decrease(@PathVariable("userId") Long userId, @PathVariable("money") BigDecimal money);
}
@Repository
@FeignClient(value = "seata-storage-samples")
public interface StorageFeignClient {
/**
* @param productId 产品id
* @return com.seata.order.config.CommonResult
* @throws
* @param: count 数量*/
@PostMapping("/storage/decrease/{productId}/{count}")
CommonResult decrease(@PathVariable("productId") Long productId, @PathVariable("count") Integer count);
}
订单业务(业务方法上加上@GlobalTransactional注解,开启分布式事务)
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);
@Autowired
private AccountFeignClient accountFeignClient;
@Autowired
private StorageFeignClient storageFeignClient;
@Autowired
private OrderMapper orderMapper;
/**
* 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
*/
@Override
//开启分布式事务
@GlobalTransactional
public void create(Order order) {
LOGGER.info("------->下单开始");
//本应用创建订单
orderMapper.insertOrder(order);
//远程调用库存服务扣减库存
LOGGER.info("------->seata-order-samples中扣减库存开始-------<");
storageFeignClient.decrease(order.getProductId(), order.getCount());
LOGGER.info("------->seata-order-samples中扣减库存结束-------<");
//远程调用账户服务扣减余额
LOGGER.info("------->seata-order-samples中扣减余额开始-------<");
LOGGER.info("扣减金额:{}",order.getMoney());
accountFeignClient.decrease(order.getUserId(), order.getMoney());
LOGGER.info("------->seata-order-samples中扣减余额结束-------<");
//修改订单状态为已完成
LOGGER.info("------->seata-order-samples中修改订单状态开始-------<");
orderMapper.update(order.getUserId(), 0);
LOGGER.info("------->seata-order-samples中修改订单状态结束-------<");
LOGGER.info("------->下单结束");
}
}
4.6:业务表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_id` bigint(11) NULL DEFAULT NULL COMMENT '用户id',
`total` decimal(10, 0) NULL DEFAULT NULL COMMENT '总额度',
`used` decimal(10, 0) NULL DEFAULT NULL COMMENT '已用余额',
`residue` decimal(10, 0) NULL DEFAULT 0 COMMENT '剩余可用额度',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES (1, 1, 1000, 0, 1000);
-- ----------------------------
-- Table structure for 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) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
`ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for order
-- ----------------------------
DROP TABLE IF EXISTS `order`;
CREATE TABLE `order` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`user_id` bigint(11) NULL DEFAULT NULL COMMENT '用户id',
`product_id` bigint(11) NULL DEFAULT NULL COMMENT '产品id',
`count` int(11) NULL DEFAULT NULL COMMENT '数量',
`money` decimal(11, 0) NULL DEFAULT NULL COMMENT '金额',
`status` int(1) NULL DEFAULT NULL COMMENT '订单状态:0:创建中;1:已完结',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for storage
-- ----------------------------
DROP TABLE IF EXISTS `storage`;
CREATE TABLE `storage` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`product_id` bigint(11) NULL DEFAULT NULL COMMENT '产品id',
`total` int(11) NULL DEFAULT NULL COMMENT '总库存',
`used` int(11) NULL DEFAULT NULL COMMENT '已用库存',
`residue` int(11) NULL DEFAULT NULL COMMENT '剩余库存',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
4.7:启动服务
观察seata日志 docker logs -f seata-server
查看服务是否已注册到nacos,已注册成功
4.8:调用创建订单接口:http://localhost:8083/order/create
查看Seata日志
查看控制台输出
数据库动态变化可自行查看,全局事务回滚后,所有的undo_log日志将会被删除