【seata】分布式事务以模块插件化集成

🌸🌸 分布式事务以模块插件化集成 🌸🌸

  • seatademo:spring-cloud-alibaba
  • 项目模块化集成示例 :pig
  • 优点
    • 采用插件化 + 扩展包的形式,结构解耦,易于扩展。
    • 仅使用的服务引入即可,无需重复编写配置文件,若需要更改默认配置,在当前服务的配置文件覆盖即可

一、cloud2021以下引入方式: Configuration

  • 若低版本有更好方式,欢迎指出,评论区一起交流。
    在这里插入图片描述
  • SeataConfiguration
    • 引入后,自动配置的信息,如注入各项配置seata-config.yml
    • 若当前服务没有 undo_log 表,则自动创建
    • pom文件需要特别注意版本对应,千万注意,否则项目起不来的
      • 版本说明链接https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
      • 手上项目版本较低我采用的如下对应版本,
      • 在这里插入图片描述
  • seatapom引入版本也要和框架对应,seata server最好对应,稍微高点也没事。

代码

  • SeataConfiguration
package com.lyzw.cloud.seata.config;

import com.lyzw.cloud.seata.props.SeataProperties;
import com.lyzw.cloud.util.factory.YamlPropertySourceFactory;
import io.seata.spring.annotation.datasource.EnableAutoDataSourceProxy;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Seata配置
 *
 * @author 陈賝
 * @since 2024/5/20 10:30
 */
@Slf4j
@Configuration
@AllArgsConstructor
@EnableAutoDataSourceProxy
@PropertySource(value = "classpath:seata-config.yml", factory = YamlPropertySourceFactory.class)
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(SeataProperties.class)
public class SeataConfiguration {
    public final DataSource dataSource;
    public final SeataProperties seataProperties;

    public static final String undoLogSql =
            "CREATE TABLE IF NOT EXISTS 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," +
            "`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=5 DEFAULT CHARSET=utf8mb3;";


    @PostConstruct
    public void detectTable() {
        try {
            dataSource.getConnection().prepareStatement(undoLogSql).execute();
        } catch (SQLException exception) {
            log.error("创建 undo_log 错误。", exception);
        }
    }
}
  • SeataProperties
package com.lyzw.cloud.seata.props;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Seata配置
 *
 * @author 陈賝
 * @since 2024/5/20 14:57
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "seata")
public class SeataProperties {

    private String applicationId;

    private String txServiceGroup;

}
  • spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.lyzw.cloud.seata.config.SeataConfiguration
  • ``
spring:
  main:
    allow-bean-definition-overriding: true

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: ${spring.application.name}-tx-group
  config:
    type: nacos
    nacos:
      server-addr: ${NACOS-HOST:ifssc-nacos}:${NACOS-PORT:8848}
      dataId: "seata-server.properties"
      username: 'nacos'
      password: 'nacos'
      namespace: ${spring.cloud.nacos.discovery.namespace}
      group: ${spring.cloud.nacos.discovery.group}
  registry:
    type: nacos
    nacos:
      application: ifssc-seata-server
      server-addr: ${NACOS-HOST:ifssc-nacos}:${NACOS-PORT:8848}
      username: 'nacos'
      password: 'nacos'
      namespace: ${spring.cloud.nacos.discovery.namespace}
      group: ${spring.cloud.nacos.discovery.group}
  enable-auto-data-source-proxy: false

logging:
  level:
    io:
      seata: debug


二、cloud2021以上引入方式: AutoConfiguration

  • eg: RuoYi-Cloud-Plus
  • 全自动配置
/**
 * seata 配置
 *
 * @author Lion Li
 */
@AutoConfiguration
@PropertySource(value = "classpath:common-seata.yml", factory = YmlPropertySourceFactory.class)
public class SeataConfiguration {
}
  • org.springframework.boot.autoconfigure.AutoConfiguration.imports
org.dromara.common.seata.config.SeataConfiguration
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值