欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot微服务架构mybatis多数据源切换

程序员文章站 2022-05-26 09:10:54
...

1,先看个目录结构图

spring boot微服务架构mybatis多数据源切换

可以看到,我把要设置的配置文件都放在了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 1

customer.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=5

customer.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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://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<>();

	@Override
	public 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);
		}

		// 创建DynamicDataSource
		GenericBeanDefinition 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");
	}

	/**
	 * 加载多数据源配置
	 */
	@Override
	public 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;// 默认DataSource

			Class<? 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 {

	@Override
	protected 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();

			// 封装成json
			StringBuilder 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中台的可以私信我。我来给你们解释一下这个概念。