Seata入门开发

连接:https://pan.baidu.com/s/14Wg4r6v7m9OAfRxtCBs9Ow?pwd=3phv

  1.  Seata配置文件application.yml
    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: 127.0.0.1:8848
          namespace:
          group: SEATA_GROUP
          username:
          password:
          ##if use MSE Nacos with auth, mutex with username/password attribute
          #access-key: ""
          #secret-key: ""
          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:
          ##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.cj.jdbc.Driver
          url: jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=Asia/Shanghai
          user: root
          password: 123456
          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'
        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
        retry-dead-threshold: 130000
        xaer-nota-retry-timeout: 60000
        recovery:
          handle-all-session-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
      security:
        secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
        tokenValidityInMilliseconds: 1800000
        ignore:
          urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
  2.  Nacos添加seataServer.properties
    transport.type=TCP
    transport.server=NIO
    transport.heartbeat=true
    transport.enableTmClientBatchSendRequest=false
    transport.enableRmClientBatchSendRequest=true
    transport.enableTcServerBatchSendResponse=false
    transport.rpcRmRequestTimeout=30000
    transport.rpcTmRequestTimeout=30000
    transport.rpcTcRequestTimeout=30000
    transport.threadFactory.bossThreadPrefix=NettyBoss
    transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
    transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
    transport.threadFactory.shareBossWorker=false
    transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
    transport.threadFactory.clientSelectorThreadSize=1
    transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
    transport.threadFactory.bossThreadSize=1
    transport.threadFactory.workerThreadSize=default
    transport.shutdown.wait=3
    transport.serialization=seata
    transport.compressor=none
    service.vgroupMapping.my_test_tx_group=default
    service.default.grouplist=127.0.0.1:8091
    service.enableDegrade=false
    service.disableGlobalTransaction=false
    client.rm.asyncCommitBufferLimit=10000
    client.rm.lock.retryInterval=10
    client.rm.lock.retryTimes=30
    client.rm.lock.retryPolicyBranchRollbackOnConflict=true
    client.rm.reportRetryCount=5
    client.rm.tableMetaCheckEnable=true
    client.rm.tableMetaCheckerInterval=60000
    client.rm.sqlParserType=druid
    client.rm.reportSuccessEnable=false
    client.rm.sagaBranchRegisterEnable=false
    client.rm.sagaJsonParser=fastjson
    client.rm.tccActionInterceptorOrder=-2147482648
    client.tm.commitRetryCount=5
    client.tm.rollbackRetryCount=5
    client.tm.defaultGlobalTransactionTimeout=60000
    client.tm.degradeCheck=false
    client.tm.degradeCheckAllowTimes=10
    client.tm.degradeCheckPeriod=2000
    client.tm.interceptorOrder=-2147482648
    client.undo.dataValidation=true
    client.undo.logSerialization=jackson
    client.undo.onlyCareUpdateColumns=true
    server.undo.logSaveDays=7
    server.undo.logDeletePeriod=86400000
    client.undo.logTable=undo_log
    client.undo.compress.enable=true
    client.undo.compress.type=zip
    client.undo.compress.threshold=64k
    tcc.fence.logTableName=tcc_fence_log
    tcc.fence.cleanPeriod=1h
    log.exceptionRate=100
    store.mode=db
    store.lock.mode=file
    store.session.mode=file
    store.file.dir=file_store/data
    store.file.maxBranchSessionSize=16384
    store.file.maxGlobalSessionSize=512
    store.file.fileWriteBufferCacheSize=16384
    store.file.flushDiskMode=async
    store.file.sessionReloadReadSize=100
    store.db.datasource=druid
    store.db.dbType=mysql
    store.db.driverClassName=com.mysql.cj.jdbc.Driver
    store.db.url=jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=Asia/Shanghai
    store.db.user=root
    store.db.password=123456
    store.db.minConn=5
    store.db.maxConn=30
    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.distributedLockTable=distributed_lock
    store.db.queryLimit=100
    store.db.lockTable=lock_table
    store.db.maxWait=5000
    store.redis.mode=single
    store.redis.single.host=127.0.0.1
    store.redis.single.port=6379
    store.redis.maxConn=10
    store.redis.minConn=1
    store.redis.maxTotal=100
    store.redis.database=0
    store.redis.queryLimit=100
    server.recovery.committingRetryPeriod=1000
    server.recovery.asynCommittingRetryPeriod=1000
    server.recovery.rollbackingRetryPeriod=1000
    server.recovery.timeoutRetryPeriod=1000
    server.maxCommitRetryTimeout=-1
    server.maxRollbackRetryTimeout=-1
    server.rollbackRetryTimeoutUnlockEnable=false
    server.distributedLockExpireTime=10000
    server.xaerNotaRetryTimeout=60000
    server.session.branchAsyncQueueSize=5000
    server.session.enableBranchAsyncRemove=false
    server.enableParallelRequestHandle=false
    metrics.enabled=false
    metrics.registryType=compact
    metrics.exporterList=prometheus
    metrics.exporterPrometheusPort=9898

    Seata入门开发_第1张图片

  3.  Seata数据库建表sql
    /*
    Navicat MySQL Data Transfer
    
    Source Server         : mysql
    Source Server Version : 80020
    Source Host           : localhost:3306
    Source Database       : seata
    
    Target Server Type    : MYSQL
    Target Server Version : 80020
    File Encoding         : 65001
    
    Date: 2022-07-27 18:35:46
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for `branch_table`
    -- ----------------------------
    DROP TABLE IF EXISTS `branch_table`;
    CREATE TABLE `branch_table` (
      `branch_id` bigint NOT NULL,
      `xid` varchar(128) NOT NULL,
      `transaction_id` bigint DEFAULT NULL,
      `resource_group_id` varchar(32) DEFAULT NULL,
      `resource_id` varchar(256) DEFAULT NULL,
      `branch_type` varchar(8) DEFAULT NULL,
      `status` tinyint DEFAULT NULL,
      `client_id` varchar(64) DEFAULT NULL,
      `application_data` varchar(2000) DEFAULT NULL,
      `gmt_create` datetime(6) DEFAULT NULL,
      `gmt_modified` datetime(6) DEFAULT NULL,
      PRIMARY KEY (`branch_id`),
      KEY `idx_xid` (`xid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of branch_table
    -- ----------------------------
    
    -- ----------------------------
    -- Table structure for `global_table`
    -- ----------------------------
    DROP TABLE IF EXISTS `global_table`;
    CREATE TABLE `global_table` (
      `xid` varchar(128) NOT NULL,
      `transaction_id` bigint DEFAULT NULL,
      `status` tinyint NOT NULL,
      `application_id` varchar(32) DEFAULT NULL,
      `transaction_service_group` varchar(32) DEFAULT NULL,
      `transaction_name` varchar(128) DEFAULT NULL,
      `timeout` int DEFAULT NULL,
      `begin_time` bigint DEFAULT NULL,
      `application_data` varchar(2000) DEFAULT NULL,
      `gmt_create` datetime DEFAULT NULL,
      `gmt_modified` datetime DEFAULT NULL,
      PRIMARY KEY (`xid`),
      KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
      KEY `idx_transaction_id` (`transaction_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of global_table
    -- ----------------------------
    
    -- ----------------------------
    -- Table structure for `lock_table`
    -- ----------------------------
    DROP TABLE IF EXISTS `lock_table`;
    CREATE TABLE `lock_table` (
      `row_key` varchar(128) NOT NULL,
      `xid` varchar(96) DEFAULT NULL,
      `transaction_id` bigint DEFAULT NULL,
      `branch_id` bigint NOT NULL,
      `resource_id` varchar(256) DEFAULT NULL,
      `table_name` varchar(32) DEFAULT NULL,
      `pk` varchar(36) DEFAULT NULL,
      `gmt_create` datetime DEFAULT NULL,
      `gmt_modified` datetime DEFAULT NULL,
      PRIMARY KEY (`row_key`),
      KEY `idx_branch_id` (`branch_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of lock_table
    -- ----------------------------
    
  4.  Seata控制台启动、访问
    启动Nacos并添加seataServer.properties配置,Mysql创建seata数据库并执行建表语句,双击seata-server.bat启动Seata服务,Seata服务启动后Nacos服务列表可以看到该服务, Seata控制台访问: http://localhost:7091,用户名和密码均为seata
    Seata入门开发_第2张图片
    Seata入门开发_第3张图片
  5.  Seata分布式事务测试
    Seata入门开发_第4张图片
    
    
        4.0.0
    
        com.seata
        seatatest
        pom
        1.0-SNAPSHOT
        
            seata-common
            seata-order
            seata-account
            seata-web
        
    
        
            UTF-8
            1.8
            1.8
            1.1.10
            Hoxton.SR8
            2.3.2.RELEASE
            2.2.4.RELEASE
            1.2.35
            3.6
            1.0-SNAPSHOT
            2.1.0
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
                
                    org.springframework.boot
                    spring-boot-dependencies
                    ${springboot.version}
                    pom
                    import
                
                
                    com.alibaba.cloud
                    spring-cloud-alibaba-dependencies
                    ${springcloudalibaba.version}
                    pom
                    import
                
                
                
                    com.alibaba
                    druid
                    ${druid.version}
                
    
                
                    com.alibaba
                    fastjson
                    ${fastjson.version}
                
    
                
                    org.apache.commons
                    commons-lang3
                    ${commons-lang3.version}
                
    
                
                    com.seata
                    seata-common
                    ${seata-common.version}
                
    
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                    ${mybatis.version}
                
    
            
        
    
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    
    
    
        
            seatatest
            com.seata
            1.0-SNAPSHOT
        
        4.0.0
    
        seata-common
    
        
    
            
                    com.alibaba.cloud
                     spring-cloud-starter-alibaba-seata
          
    
            
                com.alibaba.nacos
                nacos-client
                1.4.0
            
    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
    
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
            
    
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-config
            
    
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                mysql
                mysql-connector-java
                runtime
            
            
                org.projectlombok
                lombok
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
    
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
            
    
    
            
                com.alibaba
                druid
            
    
            
            
                org.springframework.boot
                spring-boot-starter-data-redis
            
            
            
                org.apache.commons
                commons-pool2
            
    
            
                com.alibaba
                fastjson
            
    
            
                commons-codec
                commons-codec
            
    
            
                org.apache.commons
                commons-lang3
            
    
        
    
    
    
    
    
        
            seatatest
            com.seata
            1.0-SNAPSHOT
        
        4.0.0
    
        seata-web
    
        
    
            
                com.seata
                seata-common
            
    
            
                org.springframework.cloud
                spring-cloud-starter-openfeign
            
    
        
    
    
    
    
    
        
            seatatest
            com.seata
            1.0-SNAPSHOT
        
        4.0.0
    
        seata-order
    
        
            
                com.seata
                seata-common
            
        
    
    
    
    
    
        
            seatatest
            com.seata
            1.0-SNAPSHOT
        
        4.0.0
    
        seata-account
    
        
            
                com.seata
                seata-common
            
        
    
    
    

    Seata入门开发_第5张图片

    server:
      port: 8088
      servlet:
        context-path: /
    
    
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        alibaba:
          seata:
            tx-service-group: my_test_tx_group
      application:
        name: seata-web
    
    seata:
      service:
        vgroup-mapping:
          my_test_tx_group: default
        grouplist:
          default: 127.0.0.1:8091
        enable-degrade: false
        disable-global-transaction: false
    server:
      port: 8070
      servlet:
        context-path: /
    
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_account?serverTimezone=Asia/Shanghai
        username: root
        password: 123456
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        alibaba:
          seata:
            tx-service-group: my_test_tx_group
      application:
        name: seata-account
    
    mybatis:
      mapper-locations: classpath:mybatis/mapper/*.xml
    
    #seata:
    #  config:
    #    type: nacos
    #    nacos:
    #      server-addr: 127.0.0.1:8848
    #      group: SEATA_GROUP
    #      namespace: ""
    #  registry:
    #    type: nacos
    #    nacos:
    #      application: seata-server
    #      server-addr: 127.0.0.1:8848
    #      group: SEATA_GROUP
    #      namespace: ""
    seata:
      service:
        vgroup-mapping:
          my_test_tx_group: default
        grouplist:
          default: 127.0.0.1:8091
        enable-degrade: false
        disable-global-transaction: false
    server:
      port: 8090
      servlet:
        context-path: /
    
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_order?serverTimezone=Asia/Shanghai
        username: root
        password: 123456
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        alibaba:
          seata:
            tx-service-group: my_test_tx_group
      application:
        name: seata-order
    
    mybatis:
      mapper-locations: classpath:mybatis/mapper/*.xml
    
    seata:
      service:
        vgroup-mapping:
          my_test_tx_group: default
        grouplist:
          default: 127.0.0.1:8091
        enable-degrade: false
        disable-global-transaction: false
    
    

    Seata入门开发_第6张图片
    Seata入门开发_第7张图片

  6.  Seata分布式事务回滚
    Seata入门开发_第8张图片
    Seata入门开发_第9张图片
    Seata入门开发_第10张图片
    Seata入门开发_第11张图片

https://blog.csdn.net/caoli201314?type=blog

你可能感兴趣的:(seata)