springcloud + nacos + mybatis ,增加seata分布式事务

此配置基于seata.1.2集成,安装也请安装1.2版本seata服务

maven引用

<!-- 父级pom关键配置,其他版本将会导致无法成功-->
<properties>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    <com.alibaba.cloud>2.2.0.RELEASE</com.alibaba.cloud>
</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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${com.alibaba.cloud}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<!-- 子级pom关键配置-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

yml关键配置,不用再加入file.conf和 registry.conf文件

seata:
  application-id: ${spring.application.name} 
  tx-service-group: default 
  # 服务配置项
  service:
    # 虚拟组和分组的映射 1.0.0以上好像将vgroup-mapping 改为 vgroupMapping, 此处是否影响未测试
    vgroupMapping:
      # 此处Key对应 tx-service-group 的 Value, 此处 value 默认 default
      default: default
    # 分组和 Seata 服务的映射 默认端口8091
    grouplist:
      default: 172.168.1.35:8091

 

每个库执行表:https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT(20)   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(11)      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
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

 

启动类上面增加绿色部分

@SpringBootApplication
@ComponentScan(basePackages= {"com.haogonge.*.config","com.haogonge.*.mq"})
@EnableDiscoveryClient
@EnableFeignClients(
		clients={
		        WorkerProjectService.class,
				ProjectService.class,
				WorkerService.class,
				UserService.class
		}
)
@EnableScheduling
@EnableAsync
@EnableAutoDataSourceProxy
public class AppStart{

	public static void main(String[] args) {
        //把服务名写成常亮,方便开发人员快速隔离自己的服务
        args = ArrayUtils.add(args, "--spring.application.name="+FeignConstant.YHGB_SERVER_NAME);
		SpringApplication.run(YhgbAppStart.class, args);
	}
}    

  

使用:

在需要分布式事物的方法上增加 @GlobalTransactional 即可

 

使用到断流和熔断的,请参考

https://blog.csdn.net/qq_36476972/article/details/105842611?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-17&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-17

 

上一篇:如何通过阿里开源中间件seata的@GlobalTransactional实现分布式事务


下一篇:WinForm RichTextBox 常用操作