1
This commit is contained in:
@ -0,0 +1,129 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Spring" name="Spring">
|
||||
<configuration />
|
||||
</facet>
|
||||
<facet type="web" name="Web">
|
||||
<configuration>
|
||||
<webroots />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="common-base-starter" />
|
||||
<orderEntry type="library" name="Maven: org.apache.velocity:velocity-engine-core:2.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger2:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.swagger:swagger-annotations:1.5.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.swagger:swagger-models:1.5.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-spi:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-core:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.7.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-schema:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger-common:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-spring-web:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.11" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.4" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.0.9.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.plugin:spring-plugin-metadata:1.2.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mapstruct:mapstruct:1.2.0.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger-ui:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.caspar-chen:swagger-ui-layer:1.1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.13.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.13.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" />
|
||||
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.37" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.glassfish:jakarta.el:3.0.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.37" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.8.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.8.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.8.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: cn.hutool:hutool-all:5.7.20" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.belerweb:pinyin4j:2.5.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.83" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.12.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.guava:guava:29.0-jre" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:2.11.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.4" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.alibaba:transmittable-thread-local:2.11.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.3.2.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.8.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper:5.3.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:4.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:2.0.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-boot-starter:3.4.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus:3.4.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.4.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.4.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-extension:3.4.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-core:3.4.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-annotation:3.4.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml:4.1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-schemas:4.1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.xmlbeans:xmlbeans:3.1.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-compress:1.19" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.virtuald:curvesapi:1.06" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi:4.1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.13" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.4" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.zaxxer:SparseBitSet:1.2" level="project" />
|
||||
<orderEntry type="module" module-name="common-redis-starter" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.3.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.3.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.3.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.3.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.2.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.2.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.lettuce:lettuce-core:5.3.0.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.49.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.3.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.6" level="project" />
|
||||
</component>
|
||||
</module>
|
33
common-dynamic-datasource-starter/pom.xml
Normal file
33
common-dynamic-datasource-starter/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mosty-common</artifactId>
|
||||
<groupId>com.mosty</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-dynamic-datasource-starter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mosty</groupId>
|
||||
<artifactId>common-base-starter</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.3.6</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,19 @@
|
||||
package com.common.dynamic.datasource;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 切换数据注解 可以用于类或者方法级别 方法级别优先级 > 类级别
|
||||
*
|
||||
* @author kevin
|
||||
* @date 2022/4/29 8:59 上午
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||
public @interface DataSource {
|
||||
|
||||
String value() default "master";
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.common.dynamic.datasource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class DynamicDataSourceAspect {
|
||||
|
||||
@Before("@annotation(ds)")
|
||||
public void changeDataSource(JoinPoint point, DataSource ds) throws Throwable {
|
||||
String dsId = ds.value();
|
||||
if (DynamicDataSourceContextHolder.dataSourceIds.contains(dsId)) {
|
||||
log.debug("Use DataSource :{} >", dsId, point.getSignature());
|
||||
} else {
|
||||
log.info("数据源[{}]不存在,使用默认数据源 >{}", dsId, point.getSignature());
|
||||
DynamicDataSourceContextHolder.setDataSourceRouterKey(dsId);
|
||||
}
|
||||
}
|
||||
|
||||
@After("@annotation(ds)")
|
||||
public void restoreDataSource(JoinPoint point, DataSource ds) {
|
||||
log.debug("Revert DataSource : " + ds.value() + " > " + point.getSignature());
|
||||
DynamicDataSourceContextHolder.removeDataSourceRouterKey();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.common.dynamic.datasource;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态数据源处理器
|
||||
* @author kevin
|
||||
* @date 2022/4/29 9:02 上午
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class DynamicDataSourceContextHolder {
|
||||
|
||||
/**
|
||||
* 存储已经注册的数据源的key
|
||||
*/
|
||||
public static List<String> dataSourceIds = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 使用{@link TransmittableThreadLocal} 进行存储,使用线程池等后还能获取到
|
||||
*/
|
||||
private static final ThreadLocal<String> HOLDER = TransmittableThreadLocal.withInitial(() -> null);
|
||||
|
||||
public static String getDataSourceRouterKey () {
|
||||
return HOLDER.get();
|
||||
}
|
||||
|
||||
public static void setDataSourceRouterKey (String dataSourceRouterKey) {
|
||||
log.info("切换至{}数据源", dataSourceRouterKey);
|
||||
HOLDER.set(dataSourceRouterKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据源之前一定要先移除
|
||||
*/
|
||||
public static void removeDataSourceRouterKey () {
|
||||
HOLDER.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定DataSrouce当前是否存在
|
||||
*
|
||||
* @param dataSourceId
|
||||
* @return
|
||||
*/
|
||||
public static boolean containsDataSource(String dataSourceId){
|
||||
return dataSourceIds.contains(dataSourceId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
package com.common.dynamic.datasource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyNameAliases;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 动态数据源注册
|
||||
* 实现 ImportBeanDefinitionRegistrar 实现数据源注册
|
||||
* 实现 EnvironmentAware 用于读取application.yml配置
|
||||
*/
|
||||
@Slf4j
|
||||
public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
|
||||
|
||||
/**
|
||||
* 配置上下文(也可以理解为配置文件的获取工具)
|
||||
*/
|
||||
private Environment evn;
|
||||
|
||||
/**
|
||||
* 别名
|
||||
*/
|
||||
private final static ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
|
||||
|
||||
/**
|
||||
* 由于部分数据源配置不同,所以在此处添加别名,避免切换数据源出现某些参数无法注入的情况
|
||||
*/
|
||||
static {
|
||||
aliases.addAliases("url", new String[]{"jdbc-url"});
|
||||
aliases.addAliases("username", new String[]{"user"});
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储我们注册的数据源
|
||||
*/
|
||||
private Map<String, DataSource> customDataSources = new HashMap<String, DataSource>();
|
||||
|
||||
/**
|
||||
* 参数绑定工具 springboot2.0新推出
|
||||
*/
|
||||
private Binder binder;
|
||||
|
||||
/**
|
||||
* ImportBeanDefinitionRegistrar接口的实现方法,通过该方法可以按照自己的方式注册bean
|
||||
*
|
||||
* @param annotationMetadata
|
||||
* @param beanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
|
||||
// 获取所有数据源配置
|
||||
Map config, defauleDataSourceProperties;
|
||||
defauleDataSourceProperties = binder.bind("spring.datasource.master", Map.class).get();
|
||||
// 获取数据源类型
|
||||
String typeStr = evn.getProperty("spring.datasource.master.type");
|
||||
// 获取数据源类型
|
||||
Class<? extends DataSource> clazz = getDataSourceType(typeStr);
|
||||
// 绑定默认数据源参数 也就是主数据源
|
||||
DataSource consumerDatasource, defaultDatasource = bind(clazz, defauleDataSourceProperties);
|
||||
DynamicDataSourceContextHolder.dataSourceIds.add("master");
|
||||
log.info("注册默认数据源成功");
|
||||
// 获取其他数据源配置
|
||||
List<Map> configs = binder.bind("spring.datasource.cluster", Bindable.listOf(Map.class)).get();
|
||||
// 遍历从数据源
|
||||
for (int i = 0; i < configs.size(); i++) {
|
||||
config = configs.get(i);
|
||||
clazz = getDataSourceType((String) config.get("type"));
|
||||
defauleDataSourceProperties = config;
|
||||
// 绑定参数
|
||||
consumerDatasource = bind(clazz, defauleDataSourceProperties);
|
||||
// 获取数据源的key,以便通过该key可以定位到数据源
|
||||
String key = config.get("key").toString();
|
||||
customDataSources.put(key, consumerDatasource);
|
||||
// 数据源上下文,用于管理数据源与记录已经注册的数据源key
|
||||
DynamicDataSourceContextHolder.dataSourceIds.add(key);
|
||||
log.info("注册数据源{}成功", key);
|
||||
}
|
||||
// bean定义类
|
||||
GenericBeanDefinition define = new GenericBeanDefinition();
|
||||
// 设置bean的类型,此处DynamicRoutingDataSource是继承AbstractRoutingDataSource的实现类
|
||||
define.setBeanClass(DynamicRoutingDataSource.class);
|
||||
// 需要注入的参数
|
||||
MutablePropertyValues mpv = define.getPropertyValues();
|
||||
// 添加默认数据源,避免key不存在的情况没有数据源可用
|
||||
mpv.add("defaultTargetDataSource", defaultDatasource);
|
||||
// 添加其他数据源
|
||||
mpv.add("targetDataSources", customDataSources);
|
||||
// 将该bean注册为datasource,不使用springboot自动生成的datasource
|
||||
beanDefinitionRegistry.registerBeanDefinition("datasource", define);
|
||||
log.info("注册数据源成功,一共注册{}个数据源", customDataSources.keySet().size() + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过字符串获取数据源class对象
|
||||
*
|
||||
* @param typeStr
|
||||
* @return
|
||||
*/
|
||||
private Class<? extends DataSource> getDataSourceType(String typeStr) {
|
||||
Class<? extends DataSource> type = null;
|
||||
try {
|
||||
if (StringUtils.hasLength(typeStr)) {
|
||||
// 字符串不为空则通过反射获取class对象
|
||||
type = (Class<? extends DataSource>) Class.forName(typeStr);
|
||||
} else {
|
||||
// 默认为hikariCP数据源,与springboot默认数据源保持一致
|
||||
// type = HikariDataSource.class;
|
||||
}
|
||||
return type;
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("can not resolve class with type: " + typeStr); //无法通过反射获取class对象的情况则抛出异常,该情况一般是写错了,所以此次抛出一个runtimeexception
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定参数,以下三个方法都是参考DataSourceBuilder的bind方法实现的,目的是尽量保证我们自己添加的数据源构造过程与springboot保持一致
|
||||
*
|
||||
* @param result
|
||||
* @param properties
|
||||
*/
|
||||
private void bind(DataSource result, Map properties) {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
|
||||
Binder binder = new Binder(new ConfigurationPropertySource[]{source.withAliases(aliases)});
|
||||
// 将参数绑定到对象
|
||||
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
|
||||
}
|
||||
|
||||
private <T extends DataSource> T bind(Class<T> clazz, Map properties) {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
|
||||
Binder binder = new Binder(new ConfigurationPropertySource[]{source.withAliases(aliases)});
|
||||
// 通过类型绑定参数并获得实例对象
|
||||
return binder.bind(ConfigurationPropertyName.EMPTY, Bindable.of(clazz)).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param clazz
|
||||
* @param sourcePath 参数路径,对应配置文件中的值,如: spring.datasource
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
private <T extends DataSource> T bind(Class<T> clazz, String sourcePath) {
|
||||
Map properties = binder.bind(sourcePath, Map.class).get();
|
||||
return bind(clazz, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* EnvironmentAware接口的实现方法,通过aware的方式注入,此处是environment对象
|
||||
*
|
||||
* @param environment
|
||||
*/
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
log.info("开始注册数据源");
|
||||
this.evn = environment;
|
||||
// 绑定配置器
|
||||
binder = Binder.get(evn);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.common.dynamic.datasource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
/**
|
||||
* @Auther: yukong
|
||||
* @Date: 2018/8/15 10:47
|
||||
* @Description: 动态数据源路由配置
|
||||
*/
|
||||
@Slf4j
|
||||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
||||
|
||||
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
String dataSourceName = DynamicDataSourceContextHolder.getDataSourceRouterKey();
|
||||
log.info("当前数据源是:{}", dataSourceName);
|
||||
return DynamicDataSourceContextHolder.getDataSourceRouterKey();
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Thu Apr 18 12:45:24 CST 2024
|
||||
version=1.0.0-SNAPSHOT
|
||||
groupId=com.mosty
|
||||
artifactId=common-dynamic-datasource-starter
|
@ -0,0 +1,5 @@
|
||||
com\common\dynamic\datasource\DataSource.class
|
||||
com\common\dynamic\datasource\DynamicRoutingDataSource.class
|
||||
com\common\dynamic\datasource\DynamicDataSourceAspect.class
|
||||
com\common\dynamic\datasource\DynamicDataSourceContextHolder.class
|
||||
com\common\dynamic\datasource\DynamicDataSourceRegister.class
|
@ -0,0 +1,5 @@
|
||||
C:\tools\dy外网\mosty-common\common-dynamic-datasource-starter\src\main\java\com\common\dynamic\datasource\DataSource.java
|
||||
C:\tools\dy外网\mosty-common\common-dynamic-datasource-starter\src\main\java\com\common\dynamic\datasource\DynamicDataSourceAspect.java
|
||||
C:\tools\dy外网\mosty-common\common-dynamic-datasource-starter\src\main\java\com\common\dynamic\datasource\DynamicDataSourceRegister.java
|
||||
C:\tools\dy外网\mosty-common\common-dynamic-datasource-starter\src\main\java\com\common\dynamic\datasource\DynamicDataSourceContextHolder.java
|
||||
C:\tools\dy外网\mosty-common\common-dynamic-datasource-starter\src\main\java\com\common\dynamic\datasource\DynamicRoutingDataSource.java
|
Reference in New Issue
Block a user