当前位置:网站首页>Small Tools(4) 整合Seata1.5.2分布式事务
Small Tools(4) 整合Seata1.5.2分布式事务
2022-08-03 15:46:00 【郑清】
文章目录
一、前言
- springboot 2.7.0
- springcloudalibaba 2021.0.1.0
- seata1.5.2
二、docker-compose部署seata
tips: 相应配置记得自行修改
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx/seata/1.5.2
# 修改seata配置文件`./seata-server/resources/application.yml`
# 修改`docker-compose-seata.yml`相关IP配置
# nacos命名空间`prod`下新建配置`seata-server.properties`
# 新建数据库`seata-server`,导入sql脚本`./sql/seata-server.sql`
# 运行
docker-compose -f docker-compose-seata.yml -p seata up -d
# 进入容器
# docker exec -it seata-server sh
# 查看日志
docker logs -f seata-server
访问seata控制台:ip地址:7091
登录账号密码默认:seata/seata
部分配置文件
Liunx/seata/1.5.2/docker-compose-seata.yml
# 可参考 https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose.html
version: '3'
# 网桥 -> 方便相互通讯
networks:
seata:
driver: bridge
services:
seata:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/seata-server:1.5.2 # 原镜像`seataio/seata-server:1.5.2`
container_name: seata-server # 容器名为'seata-server'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./seata-server/resources/application.yml:/seata-server/resources/application.yml"
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
# 注册到nacos上的ip。客户端将通过该ip访问seata服务。
# 注意公网ip和内网ip的差异。
SEATA_IP: www.zhengqingya.com
# 指定seata服务启动端口
SEATA_PORT: 8091
ports: # 映射端口
- "7091:7091"
- "8091:8091"
networks:
- seata
Liunx/seata/1.5.2/seata-server/resources/application.yml
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${
user.home}/logs/seata
console:
user:
username: seata
password: seata
seata:
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: prod
username: nacos
password: nacos
data-id: seata-server.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: prod
cluster: default
username: nacos
password: nacos
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
Liunx/seata/1.5.2/nacos-config/seata-server.properties
# 可参考 https://github.com/seata/seata/blob/develop/script/config-center/config.txt
# 存储模式
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
# 需要根据mysql的版本调整driverClassName
# mysql8及以上版本对应的driver:com.mysql.cj.jdbc.Driver
# mysql8以下版本的driver:com.mysql.jdbc.Driver
store.db.driverClassName=com.mysql.jdbc.Driver
# 注意根据生产实际情况调整参数host和port
store.db.url=jdbc:mysql://www.zhengqingya.com:3306/seata-server?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
# 数据库用户名
store.db.user=root
# 用户名密码
store.db.password=root
# Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
service.vgroupMapping.my_test_tx_group=default
service.vgroupMapping.user-tx-group=default
service.vgroupMapping.order-tx-group=default
service.vgroupMapping.demo-tx-group=default
service.vgroupMapping.system-tx-group=default
三、springcloud引入seata
tips: 小编这里将seata单独抽离了一个公共模块,提供给业务模块使用
1、引入seata依赖
最外层父pom.xml中统一管理seata版本
<dependencyManagement>
<dependencies>
<!-- seata分布式事务 -->
<!-- https://mvnrepository.com/artifact/io.seata/seata-spring-boot-starter -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
</dependencyManagement>
seata模块中引入
<dependencies>
<!-- 最外层父pom.xml中统一管理seata版本 -->
<!-- seata -->
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-seata -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/io.seata/seata-spring-boot-starter -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
</dependencies>
2、application-seata.yml
# seata配置
seata:
# 是否开启seata
enabled: true
# Seata 应用编号,默认为 ${spring.application.name}
application-id: ${
spring.application.name}
# Seata 事务组编号,用于 TC 集群名
tx-service-group: ${
spring.application.name}-tx-group
# 自动代理
enable-auto-data-source-proxy: true
# 服务配置项
# service:
# # 虚拟组和分组的映射
# vgroup-mapping:
# test-tx-group: default
# # 分组和 Seata 服务的映射
# grouplist:
# default: 127.0.0.1:8091
config:
type: nacos
nacos:
serverAddr: ${
spring.cloud.nacos.config.server-addr}
group: SEATA_GROUP
namespace: ${
spring.cloud.nacos.config.namespace}
username: ${
spring.cloud.nacos.config.username}
password: ${
spring.cloud.nacos.config.password}
dataId: seata-server.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: ${
spring.cloud.nacos.config.server-addr}
group: SEATA_GROUP
namespace: ${
spring.cloud.nacos.config.namespace}
username: ${
spring.cloud.nacos.config.username}
password: ${
spring.cloud.nacos.config.password}
# 数据源配置见:application-db.yml
spring:
datasource:
dynamic:
seata: true # 开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭
3、undo_log.sql
tips: 在要使用seata分布式事务的数据库下创建表
undo_log.sql
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;
4、业务模块使用seata
引入seata
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>seata</artifactId>
</dependency>
5、@GlobalTransactional
在主入口中使用注解@GlobalTransactional
实现分布式事务
本文案例demo源码
https://gitee.com/zhengqingya/small-tools.git
今日分享语句:
如果你想在这个世界上获得成功,当你进入某个沙龙时,你必须让你的虚荣心向别人的虚荣心致敬。
边栏推荐
猜你喜欢
方舟开服工具、服务器教程win
49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
证实了,百度没有快照了
基于DMS的数仓智能运维服务,知多少?
JS基础--判断
2021年12月电子学会图形化四级编程题解析含答案:新冠疫苗接种系统
2021年12月电子学会图形化二级编程题解析含答案:绘制多边形
如何将二维空间先验注入到ViT中? UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!...
上亿数据怎么玩深度分页?兼容MySQL + ES + MongoDB
Js array method is summarized
随机推荐
冒烟测试冒烟测试
Phaser(二):小恐龙跑酷游戏
NodeJs - cross domain
16 【过渡 动画】
用户侧有什么办法可以自检hologres单表占用内存具体是元数据、计算、缓存的使用情况?
FATFS | 中文显示 | 长文件名
leetcode: 899. Ordered Queue [Thinking Question]
STM32H743VIT6配置ADC为1M采样率
无内鬼,来点干货!SQL优化和诊断
After the cnpm installation is successful, the prompt is not an internal and external command, nor is it a runnable command solution
高可用版 主数据库数据结构改变 备数据库会自动改变吗
深入浅出Flask PIN
问题3:你提交的缺陷开发认为这不是BUG,怎么办?
leetcode-105 从前序与中序遍历序列构造二叉树-使用栈代替递归
Awesome!Coroutines are finally here!Thread is about to be in the past
问题1:批量测试(正式测试)之前应该怎么做?
Ark server opening tutorial win
红蓝对抗经验分享:CS免杀姿势
devops-3:Jenkins增加静态节点
[微信小程序开发者工具] × #initialize