Seata分布式事务使用!!!!

1.版本说明

版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

2.建立Seata Server数据库(TC-带头大哥的数据库) 

incubator-seata/script/server/db at v1.6.1 · apache/incubator-seata · GitHub

3.业务库建表

incubator-seata/script/client/at/db/mysql.sql at v1.6.1 · apache/incubator-seata · GitHub

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,1
  `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=utf8;

4.安装Seata-Server

        4.1 编写docker-compose.yml文件

 先把配置文件使用docker cp copy到指定目录:

//先拉去容器
docker run -d -it  seataio/seata-server:1.6.1

//cp容器中的内容到指定目录
docker cp 容器id:/seata-server/resources /opt/seata/seata-server/

编写docker-compose.yml :修改一下SEATA_IP作为host注册seata server的地址(虚拟机)

version: "3.1"
services:
  seata-server:
    image: seataio/seata-server:1.6.1
    ports:
      - "7091:7091"
      - "8091:8091"
    environment:
      - STORE_MODE=db
      # 以SEATA_IP作为host注册seata server
      - SEATA_IP=192.168.11.99
      - SEATA_PORT=8091
    volumes:
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime"        #设置系统时区
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"  #设置时区
      # 假设我们通过docker cp命令把资源文件拷贝到相对路径`./seata-server/resources`中
      # 如有问题,请阅读上面的[注意事项]以及[使用自定义配置文件]
      - "./seata-server/resources:/seata-server/resources"

4.2 修改 application.yml文件

 你需要修改一下你的nacos配置地址:

server:
  port: 7091

spring:
  application:
    name: seata-server



console:
  user:
    username: seata
    password: seata

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 192.168.11.99:8848
      namespace: public
      group: DEFAULT_GROUP
      username: nacos
      password: nacos
      context-path:
      ##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: 192.168.11.99:8848
      group: DEFAULT_GROUP
      namespace: public
      cluster: default
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
#  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

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

4.3 去Nacos配置对应的信息

 你需要修改一下你的数据库信息:

#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
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

#Transaction routing rules configuration, only for the client
# 此处的mygroup名字可以自定义,但是最好不要改保存默认
service.vgroupMapping.mygroup=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

#Transaction rule configuration, only for the client
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
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

#Log rule configuration, for client and server
log.exceptionRate=100

#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
# 默认为file,一定要改为db,我们自己的服务启动会连接不到seata
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption

#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
# 修改mysql的配置
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.11.37:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
store.db.user=root
store.db.password=root123
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


#Transaction rule configuration, only for the server
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 configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

4.4 重新启动docker容器

//删除原本的容器
docker rm -f 容器id

//进入到你上边配置的文件的目录中,重新启动容器
docker-compose up -d 

 也可以直接查看控制台页面:

 

 5.SpringCloud整合Seata

项目已上传:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

业务场景:

用户下单,整个业务逻辑由两个微服务构成:

  • 订单服务:根据采购需求创建订单。

  • 库存服务:对给定的商品扣除库存数量

5.1 pom.xml依赖引入

很多依赖在父模块中管理了版本号:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>

5.2 bootstrap.yml

需要修改:数据库地址,库名,nacos的地址:

logging:
  level:
    com:
      beiyou: debug
    root: error
server:
  port: 3999
spring:
  application:
    name: order-app
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.11.91:8848
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 10
      max-active: 100
      max-wait: 60000
      min-idle: 10
      url: jdbc:mysql://192.168.11.32:3306/beimao_order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowMultiQueries=true
      username: root
      password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
feign:
  client:
    config:
      default:
        read-timeout: 6000000


seata:
  enabled: true
  enable-auto-data-source-proxy: true
  tx-service-group: mygroup # 事务分组名称,要和服务端对应
  config:
    type: nacos
    nacos:
      server-addr: 192.168.11.91:8848
      group: DEFAULT_GROUP
      namespace: public
      username: nacos
      password: nacos
      data-id: seataServer.properties
  registry:
    type: nacos
    nacos:
      server-addr: 192.168.11.91:8848
      application: seata-server
      namespace: public
      username: nacos
      password: nacos
      cluster: default
      group: DEFAULT_GROUP
  service:
    vgroup-mapping:
      mygroup: default # key是事务分组名称 value要和服务端的保持一致
    disable-global-transaction: false
  client:
    rm:
      report-success-enable: false

5.3 启动服务

我们自己的服务要与seata-server服务在同一个namespace下,查看服务是否启动成功。

5.4 使用

5.4.1 准备数据表

beimao_stock库存库添加如下表:、

CREATE TABLE `inventory` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '唯一id',
  `productId` int NOT NULL COMMENT '商品Id',
  `qty` int DEFAULT '0' COMMENT '库存数量',
  PRIMARY KEY (`id`)
) 

beimao_order添加以下表:

CREATE TABLE `order_master` (
  `id` bigint NOT NULL COMMENT '订单id',
  `name` varchar(100) DEFAULT NULL COMMENT '用户名',
  `phone` varchar(11) DEFAULT NULL COMMENT '手机号',
  PRIMARY KEY (`id`)
);
CREATE TABLE `order_item` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '唯一id',
  `orderId` bigint NOT NULL COMMENT '订单id',
  `productName` varchar(100) DEFAULT NULL COMMENT '商品名称',
  `productId` int DEFAULT NULL COMMENT '商品id',
  `qty` int DEFAULT NULL COMMENT '购买数量',
  PRIMARY KEY (`id`)
)

5.4.2在外层接口上增加@GlobalTransactional注解即可

@GlobalTransactional(name="createOrder")
    public String ordering(){
        orderDao.insert();
        storageService.deduct(2);
        return "ok";
    }

5.5 验证结果

测试

分布式事务成功,模拟正常下单、扣库存,扣余额

分布式事务失败,模拟下单扣库存成功、扣余额失败,事务是否回滚

 项目已上传:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

注意:使用服务跨模块调用的时候涉及到openfeign的使用,需要引入依赖,创建接口使用openfeign。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值