Skip to content

Commit 8113db6

Browse files
chengxy-ndsuf203737
authored andcommitted
springboot 注解实现动态切换数据源
1 parent 7061c8d commit 8113db6

File tree

12 files changed

+347
-28
lines changed

12 files changed

+347
-28
lines changed

springboot101/模版引擎/springboot-thymeleaf/src/test/java/com/springboot101/BaseShare.java

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
springboot注解实现动态数据源切换
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.7.6</version>
10+
</parent>
11+
<groupId>com.dynamic</groupId>
12+
<artifactId> springboot-config-order</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
<mybatis.plus.version>3.5.3.1</mybatis.plus.version>
18+
<mysql.connector.version>8.0.32</mysql.connector.version>
19+
<druid.version>1.2.6</druid.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
</dependency>
27+
<!-- springboot核心包 -->
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
<!-- mysql驱动包 -->
33+
<dependency>
34+
<groupId>com.mysql</groupId>
35+
<artifactId>mysql-connector-j</artifactId>
36+
<version>${mysql.connector.version}</version>
37+
</dependency>
38+
<!-- lombok工具包 -->
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<optional>true</optional>
43+
</dependency>
44+
<!-- MyBatis Plus -->
45+
<dependency>
46+
<groupId>com.baomidou</groupId>
47+
<artifactId>mybatis-plus-boot-starter</artifactId>
48+
<version>${mybatis.plus.version}</version>
49+
</dependency>
50+
<!-- druid -->
51+
<dependency>
52+
<groupId>com.alibaba</groupId>
53+
<artifactId>druid-spring-boot-starter</artifactId>
54+
<version>${druid.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.apache.commons</groupId>
58+
<artifactId>commons-lang3</artifactId>
59+
<version>3.7</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.aspectj</groupId>
63+
<artifactId>aspectjweaver</artifactId>
64+
</dependency>
65+
</dependencies>
66+
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-maven-plugin</artifactId>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.dynamic;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6+
7+
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
8+
public class DynamicDatasourceApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(DynamicDatasourceApplication.class, args);
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.dynamic.aspect;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* 定于数据源切换注解
7+
*
8+
* @author 公众号:程序员小富
9+
* @date 2023/11/27 11:02
10+
*/
11+
@Target({ElementType.METHOD, ElementType.TYPE})
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Documented
14+
@Inherited
15+
public @interface DS {
16+
// 默认数据源master
17+
String value() default "master";
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.dynamic.aspect;
2+
3+
import com.dynamic.config.DataSourceContextHolder;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.aspectj.lang.annotation.Around;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
import org.aspectj.lang.reflect.MethodSignature;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.lang.reflect.Method;
13+
import java.util.Objects;
14+
15+
/**
16+
* 实现@DS注解的AOP切面
17+
*
18+
* @author 公众号:程序员小富
19+
* @date 2023/11/27 11:02
20+
*/
21+
@Aspect
22+
@Component
23+
@Slf4j
24+
public class DSAspect {
25+
26+
@Pointcut("@annotation(com.dynamic.aspect.DS)")
27+
public void dynamicDataSource() {
28+
}
29+
30+
@Around("dynamicDataSource()")
31+
public Object datasourceAround(ProceedingJoinPoint point) throws Throwable {
32+
MethodSignature signature = (MethodSignature) point.getSignature();
33+
Method method = signature.getMethod();
34+
DS ds = method.getAnnotation(DS.class);
35+
if (Objects.nonNull(ds)) {
36+
DataSourceContextHolder.setDataSource(ds.value());
37+
}
38+
try {
39+
return point.proceed();
40+
} finally {
41+
DataSourceContextHolder.removeDataSource();
42+
}
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.dynamic.config;
2+
3+
/**
4+
* ThreadLocal存放数据源变量
5+
*
6+
* @author 公众号:程序员小富
7+
* @date 2023/11/27 11:02
8+
*/
9+
public class DataSourceContextHolder {
10+
11+
private static final ThreadLocal<String> DATASOURCE_HOLDER = new ThreadLocal<>();
12+
13+
/**
14+
* 获取当前线程的数据源
15+
*
16+
* @return 数据源名称
17+
*/
18+
public static String getDataSource() {
19+
return DATASOURCE_HOLDER.get();
20+
}
21+
22+
/**
23+
* 设置数据源
24+
*
25+
* @param dataSourceName 数据源名称
26+
*/
27+
public static void setDataSource(String dataSourceName) {
28+
DATASOURCE_HOLDER.set(dataSourceName);
29+
}
30+
31+
/**
32+
* 删除当前数据源
33+
*/
34+
public static void removeDataSource() {
35+
DATASOURCE_HOLDER.remove();
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.dynamic.config;
2+
3+
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
9+
import javax.sql.DataSource;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
/**
14+
* 注册多个数据源
15+
*
16+
* @author 公众号:程序员小富
17+
* @date 2023/11/27 11:02
18+
*/
19+
@Configuration
20+
public class DateSourceConfig {
21+
22+
@Bean
23+
@ConfigurationProperties("spring.datasource.druid.master")
24+
public DataSource dynamicDatasourceMaster() {
25+
return DruidDataSourceBuilder.create().build();
26+
}
27+
28+
@Bean
29+
@ConfigurationProperties("spring.datasource.druid.slave")
30+
public DataSource dynamicDatasourceSlave() {
31+
return DruidDataSourceBuilder.create().build();
32+
}
33+
34+
@Bean(name = "dynamicDataSource")
35+
@Primary
36+
public DynamicDataSource createDynamicDataSource() {
37+
Map<Object, Object> dataSourceMap = new HashMap<>();
38+
// 设置默认的数据源为Master
39+
DataSource defaultDataSource = dynamicDatasourceMaster();
40+
dataSourceMap.put("master", defaultDataSource);
41+
dataSourceMap.put("slave", dynamicDatasourceSlave());
42+
return new DynamicDataSource(defaultDataSource, dataSourceMap);
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.dynamic.config;
2+
3+
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
4+
5+
import javax.sql.DataSource;
6+
import java.util.Map;
7+
8+
/**
9+
* 继承 AbstractRoutingDataSource
10+
* 重置当前的数据库路由,实现切换成想要执行的目标数据库
11+
*
12+
* @author 公众号:程序员小富
13+
* @date 2023/11/27 11:02
14+
*/
15+
public class DynamicDataSource extends AbstractRoutingDataSource {
16+
17+
public DynamicDataSource(DataSource defaultDataSource, Map<Object, Object> targetDataSources) {
18+
super.setDefaultTargetDataSource(defaultDataSource);
19+
super.setTargetDataSources(targetDataSources);
20+
}
21+
22+
/**
23+
* 这一步是关键
24+
* @return
25+
*/
26+
@Override
27+
protected Object determineCurrentLookupKey() {
28+
return DataSourceContextHolder.getDataSource();
29+
}
30+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.dynamic.controller;
2+
3+
import com.dynamic.aspect.DS;
4+
import com.dynamic.config.DataSourceContextHolder;
5+
import com.dynamic.dao.DynamicDatasourceDataMapper;
6+
import com.dynamic.entity.DynamicDatasourceData;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import javax.annotation.Resource;
12+
13+
/**
14+
* 动态数据源切换
15+
*
16+
* @author 公众号:程序员小富
17+
* @date 2023/11/27 11:02
18+
*/
19+
@RestController
20+
public class DynamicSwitchController {
21+
22+
@Resource
23+
private DynamicDatasourceDataMapper dynamicDatasourceDataMapper;
24+
25+
@GetMapping("/switchDataSource/{datasourceName}")
26+
public String switchDataSource(@PathVariable("datasourceName") String datasourceName) {
27+
DataSourceContextHolder.setDataSource(datasourceName);
28+
DynamicDatasourceData dynamicDatasourceData = dynamicDatasourceDataMapper.selectOne(null);
29+
DataSourceContextHolder.removeDataSource();
30+
return dynamicDatasourceData.getSourceName();
31+
}
32+
33+
@DS(value = "master")
34+
@GetMapping("/dbMaster")
35+
public String dbMaster() {
36+
DynamicDatasourceData dynamicDatasourceData = dynamicDatasourceDataMapper.selectOne(null);
37+
return dynamicDatasourceData.getSourceName();
38+
}
39+
40+
@DS(value = "slave")
41+
@GetMapping("/dbSlave")
42+
public String dbSlave() {
43+
DynamicDatasourceData dynamicDatasourceData = dynamicDatasourceDataMapper.selectOne(null);
44+
return dynamicDatasourceData.getSourceName();
45+
}
46+
47+
48+
/**
49+
* 验证一下事物控制
50+
*/
51+
// @Transactional(rollbackFor = Exception.class)
52+
@DS(value = "slave")
53+
@GetMapping("/dbTestTransactional")
54+
public void dbTestTransactional() {
55+
56+
DynamicDatasourceData datasourceData = new DynamicDatasourceData();
57+
datasourceData.setSourceName("test");
58+
dynamicDatasourceDataMapper.insert(datasourceData);
59+
60+
DynamicDatasourceData datasourceData1 = new DynamicDatasourceData();
61+
datasourceData1.setSourceName("testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest");
62+
dynamicDatasourceDataMapper.insert(datasourceData1);
63+
}
64+
}

0 commit comments

Comments
 (0)