springboot整合动态多数据源+分布式事务(亲测可用) 您所在的位置:网站首页 spring分布式事务管理 springboot整合动态多数据源+分布式事务(亲测可用)

springboot整合动态多数据源+分布式事务(亲测可用)

2024-04-30 09:05| 来源: 网络整理| 查看: 265

package com.zt.common.config;import com.zt.common.datasource.DynamicDataSource;import com.zt.common.enums.DataSourceType;import com.zt.common.interceptor.PrepareInterceptor;import com.zt.common.transaction.MultiDataSourceTransactionFactory;import com.zt.common.transaction.PackagesSqlSessionFactoryBean;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.Primary;import org.springframework.core.env.Environment;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.lang.Nullable;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;import java.util.Properties;/** * druid 配置多数据源 * * @author lyh */@Configuration@EnableTransactionManagement //开启事务//@MapperScan("com.zt.*.mapper")@Import({PrepareInterceptor.class})public class DruidMutilConfig {

@AutowiredPrepareInterceptor prepareInterceptor;@Bean(name = "masterDataSource") public DataSource masterDataSource(Environment env) { String sourceName = "master";Properties prop = build(env, "spring.datasource.druid.master.");AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();//druid的数据库驱动换成xa的xaDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");xaDataSource.setUniqueResourceName(sourceName);//下面两行尝试解决 exceptionSorter com.alibaba.druid.pool.vendor.MySqlExceptionSorterxaDataSource.setMaintenanceInterval(28000);xaDataSource.setTestQuery("SELECT 1");xaDataSource.setPoolSize(5);xaDataSource.setXaProperties(prop); return xaDataSource;}

@Bean(name = "slaveDataSource") public DataSource slaveDataSource(Environment env) { String sourceName = "slave";Properties prop = build(env, "spring.datasource.druid.slave.");AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();//druid的数据库驱动换成xa的xaDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");xaDataSource.setUniqueResourceName(sourceName);//下面两行尝试解决 exceptionSorter com.alibaba.druid.pool.vendor.MySqlExceptionSorterxaDataSource.setMaintenanceInterval(28000);xaDataSource.setTestQuery("SELECT 1");xaDataSource.setPoolSize(5);xaDataSource.setXaProperties(prop); return xaDataSource;}

private Properties build(Environment env, String prefix) {

Properties prop = new Properties();prop.put("url", env.getProperty(prefix + "url"));prop.put("username", env.getProperty(prefix + "username"));prop.put("password", env.getProperty(prefix + "password"));prop.put("driverClassName", env.getProperty(prefix + "driverClassName", ""));//这里只设置了简单的几个属性,如果想做更多的配置可以继续往下添加即可return prop;}

/** * 动态数据源,在这继续添加 DataSource Bean */@Bean(name = "dynamicDataSource") @Primarypublic DynamicDataSource dataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Nullable @Qualifier("slaveDataSource") DataSource slaveDataSource) { Map targetDataSources = new HashMap();targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); if (slaveDataSource != null){ targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);} // 还有数据源,在targetDataSources中继续添加return new DynamicDataSource(masterDataSource, targetDataSources);}

@Bean(name = "sqlSessionFactory") @Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception { //参照的别人的代码说需要将会话工厂改成mybatis-plus的sql会话工厂, //经测试发现使用mybatis的会话工厂也可以运行,不会报错// MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); //使用了PackagesSqlSessionFactoryBean继承SqlSessionFactoryBean,重写了配置别名的方法PackagesSqlSessionFactoryBean bean = new PackagesSqlSessionFactoryBean();bean.setPlugins(new Interceptor[]{prepareInterceptor});bean.setDataSource(dataSource);//设置多数据源分布式事务bean.setTransactionFactory(new MultiDataSourceTransactionFactory());bean.setVfs(SpringBootVFS.class);bean.setTypeAliasesPackage("com.zt");//通配符设置包别名bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*Mapper.xml"));// 扫描指定目录的xmlreturn bean.getObject();}

@Bean(name = "sqlSessionTemplate") @Primarypublic SqlSessionTemplate sqlSessionTemplate( @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory);}}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有