seata + zookeeper解决分布式事务问题

2023-07-17 19:51:40 浏览数 (2)

公司业务需要用到 springboot dubbo zookeeper 做分布式服务 但是遇到一个两个系统直接项目调用操成数据不同步问题

所以就开始百度找啊找

最终选用 seata做分布式事务

网上大部分都是nanos seata 但是我们公司是zookeeper

所以。。。 首先在pom引入

代码语言:javascript复制
<dependency>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
                <version>1.5.2</version>
</dependency>

然后下载seata1.5.2的程序

在seataconf 修改application.yml 这个文件可以仿照 application.example.yml 文件进行修改

代码语言:javascript复制
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: zk
    zk:
      server-addr: 127.0.0.1:2181
      session-timeout: 6000
      connect-timeout: 2000
      username:
      password:
      node-path: /seata/seata.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: zk
    zk:
      cluster: default
      server-addr: 127.0.0.1:2181
      session-timeout: 6000
      connect-timeout: 2000
      username: ""
      password: ""
  store:
    # support: file 、 db 、 redis
    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: user
      password: password
      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

然后再项目里面的application.yml 添加

代码语言:javascript复制
#AT 模式
seata:
  enabled: true
  application-id: kingow-oa-server  ---自己写
  tx-service-group: kingow-oa-server-group ---自己写,这个和下面的service.vgroup-mapping的key要保持一致
  enable-auto-data-source-proxy: true
  client:
    support:
      spring:
        datasource-autoproxy: true
  registry:
    type: zk
    zk:
      server-addr: 127.0.0.1:2181
      connect-timeout: 2000
      session-timeout: 6000
      username: ""
      password: ""
  service:
    grouplist:
      default: 127.0.0.1:8091
    vgroup-mapping:
      kingow-oa-server-group: default  

seata 要导入它自身的数据库 创建一个数据库导入,然后修改seata的yml文件里面的db链接 seatascriptserverdb ,然后再业务数据库 自行添加一个表

代码语言:javascript复制
CREATE TABLE `undo_log` (
  `branch_id` bigint NOT NULL COMMENT 'branch transaction id',
  `xid` varchar(100) NOT NULL COMMENT 'global transaction id',
  `context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
  `rollback_info` longblob NOT NULL COMMENT 'rollback info',
  `log_status` int NOT NULL COMMENT '0:normal status,1:defense status',
  `log_created` datetime(6) NOT NULL COMMENT 'create datetime',
  `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='AT transaction mode undo table';

然后如果要使用 就在service层添加注解

代码语言:javascript复制
@GlobalTransactional

目录:seatabin linux启动方式:

代码语言:javascript复制
./seata-server.sh -h 127.0.0.1 -p 8091 &

window直接双击

代码语言:javascript复制
seata-server.bat

如果一闪而过可以在bat文件最后里面添加一行

代码语言:javascript复制
pause

seata启动后有个前端页面: http://127.0.0.1:7091/#/ 可以查看全局事务情况 ,debug模块打断点可以表undo_log里面的数据,回滚成功后就会删除记录

0 人点赞