【JAVA秒会技术之随意切换数据库】Spring如何高效的配置多套数据源

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS Agent(兼容OpenClaw),2核4GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介:  Spring如何高效的配置多套数据源     真正的开发中,难免要使用多个数据库,进行不同的切换。无论是为了实现“读写分离”也好,还是为了使用不同的数据库(“MySQL”或“Oracle”或“SQLServer”)。传统的方法,是配置多套Spring配置文件与Mysql配置文件,不仅配置起来较为混乱,而且切换及对事物的管理,也很麻烦。下面,博主就介绍一种方法,帮助大家解决“Spring

 Spring如何高效的配置多套数据源

    真正的开发中,难免要使用多个数据库,进行不同的切换。无论是为了实现读写分离”也好,还是为了使用不同的数据库“MySQL”或“Oracle”或“SQLServer”)。传统的方法,是配置多套Spring配置文件与Mysql配置文件,不仅配置起来较为混乱,而且切换及对事物的管理,也很麻烦。下面,博主就介绍一种方法,帮助大家解决“Spring如何高效的配置多套数据源”!

(一)Spring核心配置文件

1.Spring-conf配置文件

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 使用spring注解 -->
	<context:annotation-config />
	<!-- 扫描注解 -->
	<context:component-scan base-package="com.***.****">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<!-- 配置文件读取 -->
	<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:*.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="UTF-8" />
	</bean>
	<!-- 通过@Value注解读取.properties配置内容 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="properties" ref="configProperties" />
	</bean>
	<!--=================== 多数据配置开始 =======================-->
	<!-- 数据源1-- druid数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<!-- 数据库基本信息配置 -->
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="filters" value="${jdbc.filters}" />
		<!-- 最大并发连接数 -->
		<property name="maxActive" value="${jdbc.maxActive}" />
		<!-- 初始化连接数量 -->
		<property name="initialSize" value="${jdbc.initialSize}" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="${jdbc.maxWait}" />
		<!-- 最小空闲连接数 -->
		<property name="minIdle" value="${jdbc.minIdle}" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${jdbc.validationQuery}" />
		<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
		<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
		<property name="testOnReturn" value="${jdbc.testOnReturn}" />
		<property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" />
		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="${jdbc.logAbandoned}" />
	</bean>
	<!-- 数据源2-- druid数据库连接池 -->
	<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<!-- 数据库基本信息配置 -->
		<property name="url" value="${jdbc2.url}" />
		<property name="username" value="${jdbc2.username}" />
		<property name="password" value="${jdbc2.password}" />
		<property name="driverClassName" value="${jdbc2.driverClassName}" />
		<property name="filters" value="${jdbc2.filters}" />
		<!-- 最大并发连接数 -->
		<property name="maxActive" value="${jdbc2.maxActive}" />
		<!-- 初始化连接数量 -->
		<property name="initialSize" value="${jdbc2.initialSize}" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="${jdbc2.maxWait}" />
		<!-- 最小空闲连接数 -->
		<property name="minIdle" value="${jdbc2.minIdle}" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="${jdbc2.timeBetweenEvictionRunsMillis}" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="${jdbc2.minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${jdbc2.validationQuery}" />
		<property name="testWhileIdle" value="${jdbc2.testWhileIdle}" />
		<property name="testOnBorrow" value="${jdbc2.testOnBorrow}" />
		<property name="testOnReturn" value="${jdbc2.testOnReturn}" />
		<property name="maxOpenPreparedStatements" value="${jdbc2.maxOpenPreparedStatements}" />
		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="${jdbc2.removeAbandoned}" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="${jdbc2.removeAbandonedTimeout}" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="${jdbc2.logAbandoned}" />
	</bean>
	<!-- Spring多数据源-配置 -->
	<bean id="multipleDataSource" class="com.dshl.commons.utlis.MultipleDataSource">
		<property name="targetDataSources">
			<map>
	            <!-- 配置目标数据源 -->
				<entry value-ref="dataSource" key="dataSource" />
				<entry value-ref="dataSource2" key="dataSource2" />
			</map>
		</property>
        <!-- 配置默认使用的据源 -->
		<property name="defaultTargetDataSource" ref="dataSource" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注意:SqlSessionFactory的ref一定要指向multipleDataSource -->
		<property name="dataSource" ref="multipleDataSource" />
		<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
		<!-- mapper扫描 -->
		<property name="mapperLocations">
			<list>
				<value>classpath:/mybatis/mapper/*.xml</value>
			</list>
		</property>
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.*****.dao" />
		<property name="annotationClass" value="org.springframework.stereotype.Repository" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- 多数据源-配置-结束 -->

	<!-- 配置 aspectj -->
	<aop:aspectj-autoproxy />
</beans>

2.配置jdbc.properties

#------------------------JDBC-------------------------------
jdbc.url:jdbc:sqlserver://ip地址1:端口;database=数据库;integratedSecurity=false
jdbc.driverClassName:com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.username:用户名
jdbc.password:密码
jdbc.filters:stat
jdbc.maxActive:10
jdbc.initialSize:2
jdbc.maxWait:60000
jdbc.minIdle:2
jdbc.timeBetweenEvictionRunsMillis:60000
jdbc.minEvictableIdleTimeMillis:300000
jdbc.validationQuery:SELECT 'x' FROM DUAL
jdbc.testWhileIdle:true
jdbc.testOnBorrow:false
jdbc.testOnReturn:false
jdbc.maxOpenPreparedStatements:20
jdbc.removeAbandoned:true
jdbc.removeAbandonedTimeout:180
jdbc.logAbandoned:true
 
#---------------------------JDBC-2----------------------------
jdbc2.url:jdbc:mysql://ip地址1:端口/数据库?autoReconnect=true
jdbc2.driverClassName:com.mysql.jdbc.Driver
jdbc2.username:用户名
jdbc2.password:密码
jdbc2.filters:stat
jdbc2.maxActive:10
jdbc2.initialSize:2
jdbc2.maxWait:60000
jdbc2.minIdle:2
jdbc2.timeBetweenEvictionRunsMillis:60000
jdbc2.minEvictableIdleTimeMillis:300000
jdbc2.validationQuery:SELECT 'x' FROM DUAL
jdbc2.testWhileIdle:true
jdbc2.testOnBorrow:false
jdbc2.testOnReturn:false
jdbc2.maxOpenPreparedStatements:20
jdbc2.removeAbandoned:true
jdbc2.removeAbandonedTimeout:180
jdbc2.logAbandoned:true


(二)配置通知与切面

1.配置通知

package com.netease.numen.core.annotation;
import java.lang.annotation.*;
/**
 * @author liyan
 */
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DatabaseConfiguration {
	/**
	 * annotation description
	 * @return {@link java.lang.String}
	 */
	String description() default "";

	/**
	 * annotation value ,default value "dataSource"
	 * @return {@link java.lang.String}
	 */
	String value() default "";
}

2.配置切面

package com.netease.numen.core.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netease.numen.core.annotation.DatabaseConfiguration;
import com.netease.numen.core.util.MultipleDataSource;
/**
 * 数据库配置切面
 * @author liyan
 */
@Aspect
public class DatabaseConfigurationAspect {

	/**
	 * default dataSource
	 */
	public static final String DEFAULT_DATASOURCE = "dataSource";

	/**
	 * 日志
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseConfigurationAspect.class);

	@Pointcut("@annotation(com.netease.numen.core.annotation.DatabaseConfiguration)")
	public void DBAspect() {
	}

	/**
	 * 前置通知
	 * @param joinPoint 切点
	 */
	@Before("DBAspect()")
	public void doBefore(JoinPoint joinPoint) {
		try {
			MultipleDataSource.setDataSourceKey(getTargetDataSource(joinPoint));
			LOGGER.info("Methods Described:{}", getDescription(joinPoint));
			LOGGER.info("Replace DataSource:{}", getTargetDataSource(joinPoint));
		} catch (Exception e) {
			LOGGER.warn("DataSource Switch Exception:{}", e);
			MultipleDataSource.setDataSourceKey(DEFAULT_DATASOURCE);
		}
	}

	/**
	 * 异常通知
	 * @param joinPoint  切点
	 * @param e  异常
	 */
	@AfterThrowing(pointcut = "DBAspect()", throwing = "e")
	public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
		try {
			MultipleDataSource.setDataSourceKey(DEFAULT_DATASOURCE);
		} catch (Exception ex) {
			LOGGER.warn("DataSource Switch Exception:{}", e);
		}
	}

	/**
	 * 方法后通知
	 * @param joinPoint  切点
	 */
	@After("DBAspect()")
	public void doAfter(JoinPoint joinPoint) {
		try {
			MultipleDataSource.setDataSourceKey(DEFAULT_DATASOURCE);
			LOGGER.info("Restore Default DataSource:{}", DEFAULT_DATASOURCE);
		} catch (Exception e) {
			LOGGER.warn("Restore Default DataSource Exception:{}", e);
		}
	}

	/**
	 * 获取数据源描述
	 * @param joinPoint 切点
	 * @return DB-Key(数据库)
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static String getDescription(JoinPoint joinPoint) throws Exception {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String description = "";
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if (clazzs.length == arguments.length) {
					description = method.getAnnotation(DatabaseConfiguration.class).description();
					if (description == null || "".equals(description))
						description = "Database switch";
					break;
				}
			}
		}
		return description;
	}

	/**
	 * 获取数据源
	 * @param joinPoint 切点
	 * @return DB-Key(数据库)
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static String getTargetDataSource(JoinPoint joinPoint) throws Exception {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String value = "";
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if (clazzs.length == arguments.length) {
					value = method.getAnnotation(DatabaseConfiguration.class).value();
					if (value == null || "".equals(value))
						value = DEFAULT_DATASOURCE;
					break;
				}
			}
		}
		return value;
	}
}

(三)编写切换数据源工具类

package com.netease.numen.core.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * 多数据源配置
 * 
 * 说明:定义动态数据源,实现通过集成Spring提供的AbstractRoutingDataSource,只需要
 * 实现determineCurrentLookupKey方法即可 
 * 由于DynamicDataSource是单例的,线程不安全的,所以采用ThreadLocal保证线程安全,由
 * DynamicDataSourceHolder完成。
 * 
 * @author Liyan
 */
public class MultipleDataSource extends AbstractRoutingDataSource {

	private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

	public static void setDataSourceKey(String dataSource) {
		dataSourceKey.set(dataSource);
	}
	@Override
	protected Object determineCurrentLookupKey() {
		// TODO Auto-generated method stub
		return dataSourceKey.get();
	}
}

(四)如何使用

这就很简单了,只要在serviceImpl中,要切换数据源前,调用工具类:

	public String isExist(String jobNumber) throws DataAccessException {
		try {
			//切换数据源,对中间库操作
			MultipleDataSource.setDataSourceKey("dataSource4");
			Map<String, Object> param = new HashMap<String, Object>(0);
			param.put("jobNumber", jobNumber);
			return mapper.isExist(param);
		} catch (DataAccessException e) {
			throw e;
		} finally{
			//切回数据源
			MultipleDataSource.setDataSourceKey("dataSource");
		}
	}









 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
9月前
|
监控 Cloud Native Java
Quarkus 云原生Java框架技术详解与实践指南
本文档全面介绍 Quarkus 框架的核心概念、架构特性和实践应用。作为新一代的云原生 Java 框架,Quarkus 旨在为 OpenJDK HotSpot 和 GraalVM 量身定制,显著提升 Java 在容器化环境中的运行效率。本文将深入探讨其响应式编程模型、原生编译能力、扩展机制以及与微服务架构的深度集成,帮助开发者构建高效、轻量的云原生应用。
917 44
|
9月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
9月前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
786 1
|
9月前
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
9月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2863 0
|
9月前
|
SQL Java 数据库连接
Spring Data JPA 技术深度解析与应用指南
本文档全面介绍 Spring Data JPA 的核心概念、技术原理和实际应用。作为 Spring 生态系统中数据访问层的关键组件,Spring Data JPA 极大简化了 Java 持久层开发。本文将深入探讨其架构设计、核心接口、查询派生机制、事务管理以及与 Spring 框架的集成方式,并通过实际示例展示如何高效地使用这一技术。本文档约1500字,适合有一定 Spring 和 JPA 基础的开发者阅读。
827 0
|
8月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
9月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1599 5
|
9月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
533 1
|
9月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
472 0
探索Spring Boot的@Conditional注解的上下文配置