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

    45e11f039d494613b8fe5033dc1b0bab.png

  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
    eeb48c04634648bea7b52b5e30e2662f.png
    2d168b13784d45e5b8819b3bb9bea60a.png
  5.  Seata分布式事务测试
    47da6d86198746ffa36a527dd264ac6e.png
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.seata</groupId>
        <artifactId>seatatest</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>seata-common</module>
            <module>seata-order</module>
            <module>seata-account</module>
            <module>seata-web</module>
        </modules>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <druid.version>1.1.10</druid.version>
            <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
            <springboot.version>2.3.2.RELEASE</springboot.version>
            <springcloudalibaba.version>2.2.4.RELEASE</springcloudalibaba.version>
            <fastjson.version>1.2.35</fastjson.version>
            <commons-lang3.version>3.6</commons-lang3.version>
            <seata-common.version>1.0-SNAPSHOT</seata-common.version>
            <mybatis.version>2.1.0</mybatis.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${springboot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>${springcloudalibaba.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!-- 连接池 -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>fastjson</artifactId>
                    <version>${fastjson.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>${commons-lang3.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>com.seata</groupId>
                    <artifactId>seata-common</artifactId>
                    <version>${seata-common.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.version}</version>
                </dependency>
    
            </dependencies>
        </dependencyManagement>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>seatatest</artifactId>
            <groupId>com.seata</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>seata-common</artifactId>
    
        <dependencies>
    
            <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                     <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
          </dependency>
    
            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>1.4.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </dependency>
    
            <!-- spring boot redis 缓存引入 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <!-- lettuce pool 缓存连接池 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
    
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
            </dependency>
    
        </dependencies>
    
    
    </project>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>seatatest</artifactId>
            <groupId>com.seata</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>seata-web</artifactId>
    
        <dependencies>
    
            <dependency>
                <groupId>com.seata</groupId>
                <artifactId>seata-common</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
        </dependencies>
    
    
    </project>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>seatatest</artifactId>
            <groupId>com.seata</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>seata-order</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.seata</groupId>
                <artifactId>seata-common</artifactId>
            </dependency>
        </dependencies>
    
    
    </project>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>seatatest</artifactId>
            <groupId>com.seata</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>seata-account</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.seata</groupId>
                <artifactId>seata-common</artifactId>
            </dependency>
        </dependencies>
    
    
    </project>

    8a7886b03a974817ae2f5a3971d2d469.png

    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
    
    

    503ceb6ada2140fcb13ed52195c1fd04.png
    5abc9caff4ed460aa4d98482a19df26c.png

  6.  Seata分布式事务回滚
    25736408da904cc89516936c306b5b0c.png
    3c0bd67678784efabf027ef05b18d8ff.png
    e3b9152f8ff64045949049e0b5fd3079.png
    3620e1d4ec234170b7e2555f2ef49a81.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童心同萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值