🌸🌸 分布式事务以模块插件化集成 🌸🌸
- 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
- 手上项目版本较低我采用的如下对应版本,
seata
的pom
引入版本也要和框架对应,seata server
最好对应,稍微高点也没事。
代码
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;
@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);
}
}
}
package com.lyzw.cloud.seata.props;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@Setter
@ConfigurationProperties(prefix = "seata")
public class SeataProperties {
private String applicationId;
private String txServiceGroup;
}
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
- 全自动配置
@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