1,先看个目录结构图

可以看到,我把要设置的配置文件都放在了config文件夹下面

2,Application.java是程序启动项,里面必须设置

3,application.properties是多数据源切换的配置文件

好,接下来开始进行多数据源的切换!!!

一、先看看application.properties文件吧,配置多数据源:dbType用的超高可用的com.zaxxer.hikari.HikariDataSource,这里比较关键

#springboot\u5355\u636E\u6E90\u914D\u7F6E
spring.datasource.url=jdbc:mysql://localhost:3306/fcoc-schedule?useUnicode=true&useSSL=false&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dbType=MYSQL
spring.datasource.validateQuery=select 1customer.datasource.names=db1,db2,db3
#springboot\u591A\u6570\u636E\u6E90\u914D\u7F6E
#\u6570\u636E\u6E901
customer.datasource.db1.url=jdbc:sqlserver://123.6.21.123:2433;databaseName=ecology8
customer.datasource.db1.username=spbcd
customer.datasource.db1.password=spb116688
customer.datasource.db1.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
customer.datasource.db1.dbType=com.zaxxer.hikari.HikariDataSource
customer.datasource.db1.max-idle=10
customer.datasource.db1.max-wait=10000
customer.datasource.db1.min-idle=5
customer.datasource.db1.initial-size=5#\u6570\u636E\u6E902
customer.datasource.db2.url=jdbc:mysql://localhost:3306/fcoc-control?useUnicode=true&useSSL=false&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
customer.datasource.db2.username=root
customer.datasource.db2.password=123456
customer.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
customer.datasource.db2.dbType=com.zaxxer.hikari.HikariDataSource
customer.datasource.db2.max-idle=10
customer.datasource.db2.max-wait=10000
customer.datasource.db2.min-idle=5
customer.datasource.db2.initial-size=5customer.datasource.db3.url = jdbc:mysql://123.105.123.123:3306/iretail-web?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
customer.datasource.db3.username = root
customer.datasource.db3.password = 123456
customer.datasource.db3.driver-class-name = com.mysql.jdbc.Driver
customer.datasource.db3.dbType=com.zaxxer.hikari.HikariDataSource
customer.datasource.db3.max-idle=10
customer.datasource.db3.max-wait=10000
customer.datasource.db3.min-idle=5
customer.datasource.db3.initial-size=5#mybatis
mybatis.mapper-locations=classpath*:repo/*.xml
mybatis.type-aliases-package=com.dhc.fcoc

在applicationContext-dao.xml文件里面把默认的数据库配置给添加进去,根据自己项目的实际情况来添加。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"default-lazy-init="true"><bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="properties"><props><prop key="SQLServer">sqlserver</prop><!--  <prop key="DB2">db2</prop><prop key="Oracle">oracle</prop><prop key="MySQL">mysql</prop> --></props></property></bean><bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"><property name="properties" ref="vendorProperties"/></bean><!-- MyBatis配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:conf/mybatis/mybatis-config.xml"/><!-- 自动扫描domain目录, 省掉mybatis-config.xml里的手工配置  --><property name="typeAliasesPackage" value="com.dhc.**.domain" /><!-- 显式指定Mapper文件位置 --><property name="mapperLocations" value="classpath*:com/dhc/**/repo/*Mapper.xml" /><!-- <property name="mapperLocations" value="classpath:conf/mybatis/*Mapper.xml" /> --><property name="databaseIdProvider" ref="databaseIdProvider"/></bean><bean class="com.dhc.ilead.pa.spring.MapperScannerConfigurer"><property name="basePackage" value="com.dhc.**.repo" /><!-- 支持过滤由marker interface和annotation指定的mapper。annotation属性定义了供搜索的annotation。marker interface属性定义了供搜索的父接口。若以上两个属性均定义,则匹配其中任何一个标准的mapper均包含其中。默认情况下,两个属性均为null,则指定base package下的所有interface均被加载为mapper。 --><property name="markerInterface" value="com.dhc.ilead.base.repo.BaseRepository" /><property name="annotationClass" value="com.dhc.ilead.base.repo.ILeadRepo"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><property name="properties"><props><prop key="notEmpty">TRUE</prop></props></property></bean><!-- druid连接池配置 begin --><bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource" abstract="true"><!-- 配置获取连接等待超时的时间 --><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="${spring.datasource.validateQuery}" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize" value="20" /><!-- 配置监控统计拦截的filters --><property name="filters" value="slf4j" /><property name="proxyFilters"><list><ref bean="stat-filter" /></list></property>    </bean><bean id="nativeDataSource" parent="parentDataSource" init-method="init" destroy-method="close"><!--  基本属性 url,user,password --><property name="driverClassName" value="${spring.datasource.driver-class-name}" /><property name="url" value="${spring.datasource.url}" /><property name="username" value="${spring.datasource.username}" /><property name="password" value="${spring.datasource.password}" /><!-- 配置初始化大小,最小,最大 --><property name="initialSize" value="10" /><property name="minIdle" value="5" /> <property name="maxActive" value="50" /></bean><bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"><property name="slowSqlMillis" value="3000" /><property name="logSlowSql" value="true" /></bean>  <bean id="dataSource" class="org.ilead.base.dao.ds.ILeadDataSource"><property name="nativeDataSource" ><ref bean="nativeDataSource"/></property><property name="databaseType" value="${spring.datasource.dbType}" /></bean><!-- druid连接池配置 end --><!-- 单数据源事务 --><!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> --><!-- 使用annotation定义事务 --><tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" order="0"/><!-- 使用aop定义事务 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true" /><tx:method name="find*" read-only="true" /><!-- add at 20150514 begin --><tx:method name="query*" read-only="true" /><tx:method name="count*" read-only="true" /><tx:method name="list*" read-only="true" /><tx:method name="select*" read-only="true" /><!-- add at 20150514 end --><tx:method name="exclusive__*" propagation="REQUIRES_NEW"rollback-for="Exception" /><tx:method name="*" propagation="REQUIRED" rollback-for="Exception" /></tx:attributes></tx:advice><!--  <aop:config><aop:pointcut id="txPointcut"expression="execution(* *..as*..*(..)) or (execution(* *..api*..*(..)) and !execution(* com.dhc.ilead.base.api.*..*(..)))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" order="1" /></aop:config> --></beans>

二、DynamicDataSourceRegister动态注册数据源的类,贴代码,仔细看里面的有些要根据自己项目来设置的项!别头铁去一顿瞎复制!!里面会有报错的,是因为所需要的其他类在下面要继续创建!!!别

package com.dhc.config;import java.util.HashMap;
import java.util.Map;import javax.sql.DataSource;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;/*** * @FileName DynamicDataSourceRegister.java* @Author 肖高翔* @At 2018年6月16日 上午10:08:31* @Desc 动态数据源注册,启动动态数据源请在启动类中(Application.java中添加 @Import(DynamicDataSourceRegister.class))* */
@Configuration
public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceRegister.class);private ConversionService conversionService = new DefaultConversionService();private PropertyValues dataSourcePropertyValues;// 如配置文件中未指定数据源类型,使用该默认值private static final Object DATASOURCE_TYPE_DEFAULT = "com.zaxxer.hikari.HikariDataSource";// 数据源private DataSource defaultDataSource;private Map<String, DataSource> customDataSources = new HashMap<>();@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {Map<Object, Object> targetDataSources = new HashMap<Object, Object>();// 将主数据源添加到更多数据源中targetDataSources.put("dataSource", defaultDataSource);DynamicDataSourceContextHolder.dataSourceIds.add("dataSource");// 添加更多数据源targetDataSources.putAll(customDataSources);for (String key : customDataSources.keySet()) {DynamicDataSourceContextHolder.dataSourceIds.add(key);}// 创建DynamicDataSourceGenericBeanDefinition beanDefinition = new GenericBeanDefinition();beanDefinition.setBeanClass(DynamicDataSource.class);beanDefinition.setSynthetic(true);MutablePropertyValues mpv = beanDefinition.getPropertyValues();mpv.addPropertyValue("defaultTargetDataSource", defaultDataSource);mpv.addPropertyValue("targetDataSources", targetDataSources);registry.registerBeanDefinition("dataSource", beanDefinition);logger.info("Dynamic DataSource Registry");}/*** 加载多数据源配置*/@Overridepublic void setEnvironment(Environment env) {initDefaultDataSource(env);initCustomDataSources(env);}/*** 初始化主数据源* * @Title initDefaultDataSource* @Author 肖高翔* @param env*            void*/private void initDefaultDataSource(Environment env) {// 读取主数据源(默认数据源)RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");Map<String, Object> dsMap = new HashMap<>();dsMap.put("type", null);dsMap.put("driver-class-name", propertyResolver.getProperty("driver-class-name"));dsMap.put("url", propertyResolver.getProperty("url"));dsMap.put("username", propertyResolver.getProperty("username"));dsMap.put("password", propertyResolver.getProperty("password"));// 创建(注册)数据源defaultDataSource = buildDataSource(dsMap);dataBinder(defaultDataSource, env);}/*** * 创建DataSource** @Title buildDataSource* @Author 肖高翔* @param dsMap*            数据源的map* @param type*            数据源的类型,这里用了超高效的:com.zaxxer.hikari.HikariDataSource* @param driverClassName*            注册驱动名* @param url*            连接数据库url* @param username*            连接数据库用户名* @param password*            连接数据库密码* @return DataSource*/@SuppressWarnings("unchecked")public DataSource buildDataSource(Map<String, Object> dsMap) {try {Object type = dsMap.get("type");if (type == null)type = DATASOURCE_TYPE_DEFAULT;// 默认DataSourceClass<? extends DataSource> dataSourceType;dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);String driverClassName = dsMap.get("driver-class-name").toString();String url = dsMap.get("url").toString();String username = dsMap.get("username").toString();String password = dsMap.get("password").toString();// 数据库注册对象DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url).username(username).password(password).type(dataSourceType);// 创建数据库return factory.build();} catch (ClassNotFoundException e) {e.printStackTrace();}return null;}/*** 为DataSource绑定更多数据* * @Title dataBinder* @Author 肖高翔* @param dataSource*            数据源* @param env*            识别配置文件的类 void*/private void dataBinder(DataSource dataSource, Environment env) {RelaxedDataBinder dataBinder = new RelaxedDataBinder(dataSource);dataBinder.setConversionService(conversionService);dataBinder.setIgnoreNestedProperties(false);dataBinder.setIgnoreInvalidFields(false);dataBinder.setIgnoreUnknownFields(true);// 将主数据源给清除,因为最开始初始化时候已经将主数据源注册了,将其他数据源添加进来if (dataSourcePropertyValues == null) {Map<String, Object> rpr = new RelaxedPropertyResolver(env, "spring.datasource").getSubProperties(".");Map<String, Object> values = new HashMap<>(rpr);// 排除已经设置的属性values.remove("dbType");values.remove("driver-class-name");values.remove("url");values.remove("username");values.remove("password");dataSourcePropertyValues = new MutablePropertyValues(values);}dataBinder.bind(dataSourcePropertyValues);}/*** 初始化更多数据源* * @Title initCustomDataSources* @Author 肖高翔* @param env*            void*/private void initCustomDataSources(Environment env) {// 读取配置文件获取更多数据源,也可以通过defaultDataSource读取数据库获取更多数据源RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "customer.datasource.");// 这里从application.properties配置文件里面读取names属性String dsPrefixs = propertyResolver.getProperty("names");// 多个数据源for (String dsPrefix : dsPrefixs.split(",")) {Map<String, Object> dsMap = propertyResolver.getSubProperties(dsPrefix + ".");DataSource ds = buildDataSource(dsMap);customDataSources.put(dsPrefix, ds);dataBinder(ds, env);}}}

三、DynamicDataSource动态数据源,这个切换全靠AbstractRoutingDataSource

package com.dhc.config;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;/*** * @FileName DynamicDataSource.java* @Author 肖高翔* @At 2018年6月15日 上午10:41:17* @Desc 动态数据源*/
public class DynamicDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DynamicDataSourceContextHolder.getDataSourceType();}}

四、DynamicDataSourceAspect  利用AOP切换数据源Advice

package com.dhc.config;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;/*** * @FileName DynamicDataSourceAspect.java* @Author 肖高翔* @At 2018年6月15日 上午10:41:47* @Desc AOP切换数据源Advice*/
@Aspect
@Order(-1) // 保证该AOP在@Transactional之前执行
@Component
public class DynamicDataSourceAspect {private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);@Before("@annotation(ds)")public void changeDataSource(JoinPoint point, TargetDataSource ds) throws Throwable {String dsId = ds.name();if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) {System.out.println("数据源【{}】不存在,使用默认数据源 > {} " + ds.name() + " " + point.getSignature());logger.error("数据源[{}]不存在,使用默认数据源 > {}", ds.name(), point.getSignature());} else {System.out.println("Use DataSource : {} > {} " + ds.name() + " " + point.getSignature());logger.debug("Use DataSource : {} > {}", ds.name(), point.getSignature());DynamicDataSourceContextHolder.setDataSourceType(ds.name());}}@After("@annotation(ds)")public void restoreDataSource(JoinPoint point, TargetDataSource ds) {System.out.println("Revert DataSource : {} > {} " + ds.name() + " " + point.getSignature());logger.debug("Revert DataSource : {} > {}", ds.name(), point.getSignature());DynamicDataSourceContextHolder.clearDataSourceType();}}

五、DynamicDataSourceContextHolder保存切换的数据源

package com.dhc.config;import java.util.ArrayList;
import java.util.List;/*** * @FileName DynamicDataSourceContextHolder.java* @Author 肖高翔* @At 2018年6月16日 上午10:06:30* @Desc 保存切换的数据源*/
public class DynamicDataSourceContextHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();public static List<String> dataSourceIds = new ArrayList<>();/*** 设置数据源名* * @Title setDataSourceType* @Author 肖高翔* @param dataSourceType*            void*/public static void setDataSourceType(String dataSourceType) {System.out.println("切换到 { " + dataSourceType + " } 数据源");contextHolder.set(dataSourceType);}/*** 获取数据源名* * @Title getDataSourceType* @Author 肖高翔* @return String*/public static String getDataSourceType() {return contextHolder.get();}/*** 清除数据源名* * @Title clearDataSourceType* @Author 肖高翔 void*/public static void clearDataSourceType() {contextHolder.remove();}/*** 判断指定DataSrouce当前是否存在* * @Title containsDataSource* @Author 肖高翔* @param dataSourceId* @return boolean*/public static boolean containsDataSource(String dataSourceId) {return dataSourceIds.contains(dataSourceId);}
}

六、TargetDataSource在方法上使用该注解,用于指定使用哪个数据源,这是个注释类,看清楚啊!

package com.dhc.config;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** * @FileName TargetDataSource.java* @Author 肖高翔* @At 2018年6月15日 上午10:44:41* @Desc 在方法上使用该注解,用于指定使用哪个数据源*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {String name();
}

七、在Application.java启动程序里面添加@Import({ DynamicDataSourceRegister.class })

package com.dhc.fcoc;import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;import com.dhc.config.DynamicDataSourceRegister;@ImportResource(locations = { "classpath*:/conf/applicationContext.xml", "classpath*:/conf/applicationContext-legacy.xml" })
@Import({ DynamicDataSourceRegister.class })
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, SessionAutoConfiguration.class, RedisAutoConfiguration.class,RedisRepositoriesAutoConfiguration.class, CacheStatisticsAutoConfiguration.class, WebMvcAutoConfiguration.class })
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

我是在项目的AS层(也就是service,我这里叫做AS层),方法头部用了注解方式,然后就实现了数据源的动态切换

package com.dhc.fcoc.job.chl.employee.as;import java.io.IOException;
import java.util.List;import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Service;import com.dhc.config.HttpClientUtils;
import com.dhc.config.TargetDataSource;
import com.dhc.fcoc.common.message.ResultCodeEnum;
import com.dhc.fcoc.job.chl.employee.domain.ChlHrmresource;
import com.dhc.fcoc.job.chl.employee.repo.ChlHrmresourceRepo;
import com.dhc.fcoc.main.param.common.ParamFieldConstant;
import com.dhc.ilead.base.exception.BaseASException;
import com.dhc.ilead.base.exception.BaseRepoException;
import com.dhc.ilead.base.service.BaseService;import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.util.JSONUtils;@Service
public class ChlHrmresourceAS extends BaseService<ChlHrmresource, ChlHrmresourceRepo> {/*** 查询OA系统中的人员信息, 并同步到erp系统中* * @Title batchInsert* @Author 肖高翔* @throws BaseASException*             void* @throws IOException* @throws ClientProtocolException*/@TargetDataSource(name = "db1")  // 在这里使用注解,这个名字就是application.properties里面的names的数据源!自己根据项目自己定义public void batchInsert() throws BaseASException, ClientProtocolException, IOException {try {// 查询人员信息List<ChlHrmresource> chlHrmresourceList = getDefaultRepository().selectAllHrmresource();// 封装成jsonStringBuilder json = new StringBuilder();String strJson = JSONArray.fromObject(chlHrmresourceList).toString();// 这里必须加上日期转换的,否则查询出来的日期会自动转成long类型JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" }));json.append("{\"chlEmployeeDto\":");json.append(strJson);json.append("}");// 调用httpclient封装好的方法即可HttpClientUtils.httpClientMethod(ParamFieldConstant.EMPLOYEE_URL, json.toString());} catch (BaseRepoException e) {throw new BaseASException(ResultCodeEnum.DB_ERROR.getCode(), ResultCodeEnum.DB_ERROR.getMessage() + "," + "异常类:" + this.getClass().getName() + ","+ "异常方法:" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);}}
}

基本的pom.xml没有贴,用常规的配置就可以。

在这过程中发现的问题是:

1,spring boot ,前后端分离,然后服务都在中台服务中,如果直接在中台服务里面去设置这个多数据源切换,那么通过web端访问,他只能够切换成一个数据源,后面的就没用了。

2,可能的猜想是:在中台这端,事务就已经开启了。所以导致行不通。

3,当把切换数据源放在web端,放到事务未开始之前,就能够随意的进行切换,并且测试成功了。

有不明白spring boot中台的可以私信我。我来给你们解释一下这个概念。

更多精彩敬请关注公众号

Java极客思维

微信扫一扫,关注公众号

spring boot微服务架构mybatis多数据源切换相关推荐

  1. Docker容器及Spring Boot微服务应用

    2019独角兽企业重金招聘Python工程师标准>>> Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复 ...

  2. 《Spring Cloud微服务架构实战派》PDF样章

    内容摘要: 本书针对Spring Cloud Greenwich.SR2版本+Spring Boot的2.1.x.RELEASE版本.在编写过程中,不仅考虑到在企业任职所需的技能,还考虑到求职面试时可 ...

  3. 疯狂Spring Cloud微服务架构实战

    网站 更多书籍点击进入>> CiCi岛 下载 电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍 电子书下载(皮皮云盘-点击"普通下载" ...

  4. 《Spring Cloud 微服务架构进阶》读书笔记

    前页 随着 DevOps 和以 Docker 为主的容器技术的发展,云原生应用架构和微服 务变得流行起来. 云原生包含的内容很多,如 DevOps.持续交付.微服务.敏捷等 第一章,微服务架构介绍 架 ...

  5. Spring Boot 微服务编码风格指南和最佳实践

    文奇摄于世界尽头州立公园 通过多年来使用 Spring Boot 微服务,我编制了一份编码风格指南和最佳实践列表.这份清单并不全面,但我希望您能找到一两点可以借鉴的地方,无论您是新手还是经验丰富的 S ...

  6. spring boot 微服务集群 + 注册中心

    spring boot 微服务框架下载地址: https://start.spring.io/ 注册中心 Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进 ...

  7. Spring Boot微服务的黑匣子测试是如此简单

    当我需要进行原型设计,概念验证或在空闲时间使用一些新技术时,开始新项目对于Maven来说总是有点烦人. 不得不说,设置Maven项目并不难,您可以使用Maven原型. 但是原型通常是过时的. 谁想玩旧 ...

  8. Docker基础篇 - (六)Docker 网络Spring Boot微服务打包Docker镜像

    ⑦ Docker 网络 7.1 理解Docker0 清空下前面的docker 镜像.容器 # 删除全部容器 [root@cVzhanshi tomcat-diy]# docker rm -f $(do ...

  9. Spring Boot微服务间文件返回实现

    Feign接口获取文件流问题_Java_wyazyf的博客-CSDN博客 https://blog.csdn.net/wyazyf/article/details/93200033 Spring Bo ...

最新文章

  1. 一道经典面试题讲解 :数组越界而没报错 ,却出现死循环 ?(C语言)
  2. Nature | 有机合成的数字化
  3. 论如何骗过计算机视觉AI网络,UCLA专家为你支几招
  4. html marquee
  5. win2003服务器装spl2008,打安全补丁后无法进入SQL Server Management Studio
  6. QT的QPair类的使用
  7. BABOK - 开篇:业务分析知识体系介绍
  8. 矩阵相乘Java版(第一个java程序)
  9. 训练中Loss为Nan的原因,梯度消失或者爆炸的优化
  10. 最小生成树模板 加 例题分析 (最小生成树类型汇总)
  11. 计算机与生物的关系论文题目,生物论文
  12. 引爆你的集合灵感 [C#, LINQ]
  13. 一文读懂V8垃圾回收机制——新生代Scavenge、老生代Mark-Sweep和Mark-Compact
  14. Spring| Spring中的动态代理配置(aspectj-autoproxy属性介绍)
  15. 【从零开始学习YOLOv3】3. YOLOv3的数据加载机制和增强方法
  16. 测试歌词的软件,测试、修改LRC歌词工具
  17. 大型在线实时应用解决方案
  18. 服务器系统盘能用ssd吗,服务器主机可以用固态硬盘吗
  19. 湖南c语言程序设计,C语言程序设计
  20. 器件选型电源篇-AC/DC电源模块选型及性能测试

热门文章

  1. Python tkinter用户登录界面
  2. 35岁程序员真的要去卖炒饭了吗?
  3. Windows后渗透--维持权限十九式
  4. Win10 无法安装 net3.5, 错误代码:0x80070422
  5. i2p源码笔记-KBucketSet.java
  6. 从此以后,别他妈的来搭理我
  7. 《中国标准文献分类法》
  8. MQTT服务器 Mosquitto的部署和应用
  9. 从零开始学Java哇哇哇-Day1
  10. Python——元祖