From 347478e8460420359f45ac545b9163a7a8e55a4c Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 7 Aug 2023 15:36:17 +0800 Subject: [PATCH] =?UTF-8?q?seata=E4=BA=8B=E5=8A=A1=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-spring-boot-starter/pom.xml | 5 ++ .../MultiDataSourceAutoConfiguration.java | 13 +++++ .../spring/boot/MybatisFlexProperties.java | 47 +++++++++++++++++++ .../mybatisflex/spring/boot/SeataMode.java | 26 ++++++++++ 4 files changed, 91 insertions(+) create mode 100644 mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java diff --git a/mybatis-flex-spring-boot-starter/pom.xml b/mybatis-flex-spring-boot-starter/pom.xml index a0d685fb..fede16e4 100644 --- a/mybatis-flex-spring-boot-starter/pom.xml +++ b/mybatis-flex-spring-boot-starter/pom.xml @@ -91,6 +91,11 @@ true + + io.seata + seata-spring-boot-starter + 1.5.2 + diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java index bc14eb06..2ae4dd88 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java @@ -19,7 +19,10 @@ import com.mybatisflex.core.datasource.DataSourceBuilder; import com.mybatisflex.core.datasource.DataSourceDecipher; import com.mybatisflex.core.datasource.DataSourceManager; import com.mybatisflex.core.datasource.FlexDataSource; +import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig; import com.mybatisflex.spring.datasource.DataSourceAdvice; +import io.seata.rm.datasource.DataSourceProxy; +import io.seata.rm.datasource.xa.DataSourceProxyXA; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.ObjectProvider; @@ -53,6 +56,8 @@ public class MultiDataSourceAutoConfiguration { private final Map> dataSourceProperties; + private final SeataConfig seataConfig; + //数据源解密器 protected final DataSourceDecipher dataSourceDecipher; @@ -62,6 +67,7 @@ public class MultiDataSourceAutoConfiguration { ) { dataSourceProperties = properties.getDatasource(); dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable(); + seataConfig = properties.getSeataConfig(); } @Bean @@ -76,6 +82,13 @@ public class MultiDataSourceAutoConfiguration { for (Map.Entry> entry : dataSourceProperties.entrySet()) { DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); + if (seataConfig.isEnable()){ + if (seataConfig.getSeataMode() ==SeataMode.XA){ + dataSource = new DataSourceProxyXA(dataSource); + }else { + dataSource = new DataSourceProxy(dataSource); + } + } if (flexDataSource == null) { flexDataSource = new FlexDataSource(entry.getKey(), dataSource); } else { diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java index 2b6e80df..d5bf56fb 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java @@ -122,6 +122,19 @@ public class MybatisFlexProperties { */ private CoreConfiguration configuration; + /** + * A Configuration object for seata + */ + private SeataConfig seataConfig; + + public SeataConfig getSeataConfig() { + return seataConfig; + } + + public void setSeataConfig(SeataConfig seataConfig) { + this.seataConfig = seataConfig; + } + public Map> getDatasource() { return datasource; } @@ -890,4 +903,38 @@ public class MybatisFlexProperties { } + /** + * Seata 配置 + * + * @author life + */ + public static class SeataConfig{ + + /** + * 是否开启 + */ + private boolean enable = false; + + /** + * 事务模式支持,只支持XA或者AT + */ + private SeataMode seataMode = SeataMode.AT; + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } + + public SeataMode getSeataMode() { + return seataMode; + } + + public void setSeataMode(SeataMode seataMode) { + this.seataMode = seataMode; + } + } + } diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java new file mode 100644 index 00000000..b02af607 --- /dev/null +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/SeataMode.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.spring.boot; + +/** + * @author life + */ +public enum SeataMode { + + XA, + + AT +}