目录
一、部署Seata
1、使用windows部署
1.1、下载Seata启动包
1.2、创建数据库表
1.3、修改seata的application.yml
1.4、启动seata
二、业务系统集成
1、本项目基础依赖版本
2、添加依赖
3、添加配置
4、添加注解@GlobalTransactional
5、创建UNDO_LOG表
官方下载地址:https://github.com/apache/incubator-seata/releases/tag/v2.0.0
首先创建名为seata的数据库,将seata启动包解压后,进入seata/script/server/db目录下,并执行对应数据库类型的sql文件,完成数据库表创建。
从application.example.yml将需要的配置复制进来,并进行修改。使用nacos、mysql。
最终版:
server: port: 7091 spring: application: name: seata-server logging: config: classpath:logback-spring.xml file: path: ${log.home:${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: 127.0.0.1:8848 namespace: group: SEATA_GROUP username: password: context-path: data-id: seataServer.properties registry: # support: nacos, eureka, redis, zk, consul, etcd3, sofa type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 group: SEATA_GROUP namespace: cluster: default username: password: context-path: server: service-port: 8091 #If not configured, the default is '${server.port} + 1000' max-commit-retry-timeout: -1 max-rollback-retry-timeout: -1 rollback-retry-timeout-unlock-enable: false enable-check-auth: true enable-parallel-request-handle: true enable-parallel-handle-branch: false retry-dead-threshold: 130000 xaer-nota-retry-timeout: 60000 enableParallelRequestHandle: true recovery: committing-retry-period: 1000 async-committing-retry-period: 1000 rollbacking-retry-period: 1000 timeout-retry-period: 1000 undo: log-save-days: 7 log-delete-period: 86400000 session: branch-async-queue-size: 5000 #branch async remove queue size enable-branch-async-remove: false #enable to asynchronous remove branchSession store: # support: file 、 db 、 redis 、 raft mode: db db: datasource: druid db-type: mysql driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true user: mysql password: mysql min-conn: 10 max-conn: 100 global-table: global_table branch-table: branch_table lock-table: lock_table distributed-lock-table: distributed_lock query-limit: 1000 max-wait: 5000 metrics: enabled: false registry-type: compact exporter-list: prometheus exporter-prometheus-port: 9898 transport: rpc-tc-request-timeout: 15000 enable-tc-server-batch-send-response: false shutdown: wait: 3 thread-factory: boss-thread-prefix: NettyBoss worker-thread-prefix: NettyServerNIOWorker boss-thread-size: 1 security: secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017 tokenValidityInMilliseconds: 1800000 ignore: urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/metadata/v1/**
进入seata/bin目录下,执行启动命令。
org.springframework.boot spring-boot-dependencies2.2.13.RELEASE pom import org.springframework.cloud spring-cloud-dependenciesHoxton.SR3 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2.2.1.RELEASE pom import
注意:使用其他版本依赖,相应配置会有所不同。请按照官方发布的版本关系对照表进行添加依赖,否则会出现不兼容的bug:SpringCloud Alibaba版本依赖关系说明
io.seata seata-spring-boot-starter2.0.0 io.seata seata-all2.0.0
seata: registry: type: nacos nacos: server-addr: localhost:8848 namespace: "" group: SEATA_GROUP application: seata-server # 事务组名称 tx-service-group: xxx(自定义事务组名称) service: # 事务组与tc集群的映射关系 vgroup-mapping: # default - tc服务的集群名称 xxx(自定义事务组名称,与上面一致): "default"
在需要开启全局事务的方法上添加注解@GlobalTransactional
在业务数据库中创建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, `ext` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_general_ci;