1、代码实现
import com.zaxxer.hikari.HikariDataSource;import lombok.Data;import lombok.Getter;import lombok.Setter;import org.aopalliance.intercept.MethodInvocation;import org.springframework.aop.Advisor;import org.springframework.aop.support.AbstractGenericPointcutAdvisor;import org.springframework.aop.support.AopUtils;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.ApplicationContextAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.Ordered;import org.springframework.core.annotation.Order;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import org.springframework.transaction.interceptor.TransactionAttribute;import org.springframework.transaction.interceptor.TransactionInterceptor;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;import javax.sql.DataSource;import java.lang.reflect.Field;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;@Configuration("dynamicDataSourceConfig")@ConditionalOnProperty(name = "dynamic.jdbc", havingValue = "true")@EnableConfigurationProperties(value = DynamicDataSourceConfig.DynamicDataSourceProperties.class)public class DynamicDataSourceConfig { /** * 动态数据源 * @param dynamicDataSourceProperties * @return */ @Primary @Bean("dynamicDataSource") public DataSource dataSource(DynamicDataSourceProperties dynamicDataSourceProperties) { JdbcDataSourceProperties[] jdbcDataSourceProperties = dynamicDataSourceProperties.getDatasources(); if (jdbcDataSourceProperties == null) { throw new IllegalArgumentException("can not find any available dynamic.datasource config"); } Map
2、配置
dynamic.jdbc=truedynamic.jdbc.datasources[0].datasourceId=firstdynamic.jdbc.datasources[0].url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=falsedynamic.jdbc.datasources[0].username=readdynamic.jdbc.datasources[0].password=123456dynamic.jdbc.datasources[0].driver-class-name=com.mysql.cj.jdbc.Driver#read 为读库,write 为写库,不配置为其他用途,比如生产环境配置相应的压力测试库等dynamic.jdbc.datasources[0].accessType=readdynamic.jdbc.datasources[1].datasourceId=seconddynamic.jdbc.datasources[1].url=jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=falsedynamic.jdbc.datasources[1].username=readdynamic.jdbc.datasources[1].password=123456dynamic.jdbc.datasources[1].driver-class-name=com.mysql.cj.jdbc.Driverdynamic.jdbc.datasources[1].accessType=readdynamic.jdbc.datasources[2].datasourceId=thirddynamic.jdbc.datasources[2].url=jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=falsedynamic.jdbc.datasources[2].username=writedynamic.jdbc.datasources[2].password=123456dynamic.jdbc.datasources[2].driver-class-name=com.mysql.cj.jdbc.Driverdynamic.jdbc.datasources[2].accessType=write