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

1,先看个目录结构图

spring boot微服务架构mybatis多数据源切换_第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 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文件里面把默认的数据库配置给添加进去,根据自己项目的实际情况来添加。




	
	    
	        
	            sqlserver
	           
	        
	    
	
	
	
	    
	
	
	
	
		
		
		
		
		
		
		
		
	
	

	
		
		
		
		
		
		
			
				TRUE
			
		
	
	
	
	
		
		
	    
	
		
	    
	
		 
	    
	
	    
	    
	    
	    
	
		
	    
	    
	
		 
	    
		
		    
		        
		    
		    
	

	
		
		
	    
	    
	    
	
		
	    
	     
	    
	   
	

	
	    
	    
		
	
	
		
		
		
	
	
	
	
	

	
	
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	





二、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 customDataSources = new HashMap<>();

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		Map targetDataSources = new HashMap();
		// 将主数据源添加到更多数据源中
		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 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 dsMap) {
		try {
			Object type = dsMap.get("type");
			if (type == null)
				type = DATASOURCE_TYPE_DEFAULT;// 默认DataSource

			Class dataSourceType;
			dataSourceType = (Class) 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 rpr = new RelaxedPropertyResolver(env, "spring.datasource").getSubProperties(".");
			Map 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 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 contextHolder = new ThreadLocal();
	public static List 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 {

	/**
	 * 查询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 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中台的可以私信我。我来给你们解释一下这个概念。

你可能感兴趣的:(个人总结,JAVA开发,mybatis,springmvc,springboot)