forked from lenve/javaboy-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.6.6</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>org.javaboy</groupId> | ||
<artifactId>dynamic_datasource_demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>dynamic_datasource_demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.2.9</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
dynamic_datasource_demo/src/main/java/org/javaboy/demo/DynamicDatasourceDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.javaboy.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DynamicDatasourceDemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DynamicDatasourceDemoApplication.class, args); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
dynamic_datasource_demo/src/main/java/org/javaboy/demo/annotation/DataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.javaboy.demo.annotation; | ||
|
||
import org.javaboy.demo.provider.DynamicDataSourceProvider; | ||
import org.springframework.core.annotation.AliasFor; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface DataSource { | ||
String dataSourceName() default DynamicDataSourceProvider.DEFAULT_DATASOURCE; | ||
|
||
@AliasFor("dataSourceName") | ||
String value() default DynamicDataSourceProvider.DEFAULT_DATASOURCE; | ||
} |
63 changes: 63 additions & 0 deletions
63
dynamic_datasource_demo/src/main/java/org/javaboy/demo/aspect/DataSourceAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.javaboy.demo.aspect; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.javaboy.demo.annotation.DataSource; | ||
import org.javaboy.demo.dynamic.DynamicDataSourceContextHolder; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Aspect | ||
@Order(1) | ||
@Component | ||
public class DataSourceAspect { | ||
@Pointcut("@annotation(org.javaboy.demo.annotation.DataSource)" | ||
+ "|| @within(org.javaboy.demo.annotation.DataSource)") | ||
public void dsPc() { | ||
|
||
} | ||
|
||
@Around("dsPc()") | ||
public Object around(ProceedingJoinPoint point) throws Throwable { | ||
DataSource dataSource = getDataSource(point); | ||
|
||
if (Objects.nonNull(dataSource)) { | ||
DynamicDataSourceContextHolder.setDataSourceType(dataSource.dataSourceName()); | ||
} | ||
|
||
try { | ||
return point.proceed(); | ||
} finally { | ||
// 销毁数据源 在执行方法之后 | ||
DynamicDataSourceContextHolder.clearDataSourceType(); | ||
} | ||
} | ||
|
||
/** | ||
* 获取需要切换的数据源 | ||
*/ | ||
public DataSource getDataSource(ProceedingJoinPoint point) { | ||
MethodSignature signature = (MethodSignature) point.getSignature(); | ||
DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class); | ||
if (Objects.nonNull(dataSource)) { | ||
return dataSource; | ||
} | ||
return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
dynamic_datasource_demo/src/main/java/org/javaboy/demo/config/DruidAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.javaboy.demo.config; | ||
|
||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; | ||
import com.alibaba.druid.util.Utils; | ||
import org.javaboy.demo.dynamic.DynamicDataSource; | ||
import org.javaboy.demo.provider.DynamicDataSourceProvider; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.servlet.*; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Configuration | ||
public class DruidAutoConfiguration { | ||
@Autowired | ||
DynamicDataSourceProvider dynamicDataSourceProvider; | ||
|
||
@Bean | ||
DynamicDataSource dynamicDataSource() { | ||
return new DynamicDataSource(dynamicDataSourceProvider); | ||
} | ||
|
||
/** | ||
* 去除数据源监控页面的广告 | ||
* | ||
* @param properties | ||
* @return | ||
*/ | ||
@Bean | ||
@ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true") | ||
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) { | ||
// 获取web监控页面的参数 | ||
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); | ||
// 提取common.js的配置路径 | ||
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; | ||
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); | ||
// 创建filter进行过滤 | ||
Filter filter = new Filter() { | ||
@Override | ||
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
String text = Utils.readFromResource("support/http/resources/js/common.js"); | ||
text = text.replace("this.buildFooter();", ""); | ||
response.getWriter().write(text); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
}; | ||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); | ||
registrationBean.setFilter(filter); | ||
registrationBean.addUrlPatterns(commonJsPattern); | ||
return registrationBean; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
dynamic_datasource_demo/src/main/java/org/javaboy/demo/dynamic/DynamicDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.javaboy.demo.dynamic; | ||
|
||
import org.javaboy.demo.provider.DynamicDataSourceProvider; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
public class DynamicDataSource extends AbstractRoutingDataSource { | ||
|
||
DynamicDataSourceProvider dynamicDataSourceProvider; | ||
|
||
public DynamicDataSource(DynamicDataSourceProvider dynamicDataSourceProvider) { | ||
this.dynamicDataSourceProvider = dynamicDataSourceProvider; | ||
Map<Object, Object> targetDataSources = new HashMap<>(dynamicDataSourceProvider.loadDataSources()); | ||
super.setTargetDataSources(targetDataSources); | ||
super.setDefaultTargetDataSource(dynamicDataSourceProvider.loadDataSources().get(DynamicDataSourceProvider.DEFAULT_DATASOURCE)); | ||
super.afterPropertiesSet(); | ||
} | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
String dataSourceType = DynamicDataSourceContextHolder.getDataSourceType(); | ||
return dataSourceType; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...atasource_demo/src/main/java/org/javaboy/demo/dynamic/DynamicDataSourceContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.javaboy.demo.dynamic; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
public class DynamicDataSourceContextHolder { | ||
public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class); | ||
|
||
/** | ||
* 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本, | ||
* 所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。 | ||
*/ | ||
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>(); | ||
|
||
/** | ||
* 设置数据源的变量 | ||
*/ | ||
public static void setDataSourceType(String dsType) { | ||
log.info("切换到{}数据源", dsType); | ||
CONTEXT_HOLDER.set(dsType); | ||
} | ||
|
||
/** | ||
* 获得数据源的变量 | ||
*/ | ||
public static String getDataSourceType() { | ||
return CONTEXT_HOLDER.get(); | ||
} | ||
|
||
/** | ||
* 清空数据源变量 | ||
*/ | ||
public static void clearDataSourceType() { | ||
CONTEXT_HOLDER.remove(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
dynamic_datasource_demo/src/main/java/org/javaboy/demo/mapper/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.javaboy.demo.mapper; | ||
|
||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Select; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Mapper | ||
public interface UserMapper { | ||
@Select("select count(*) from user") | ||
Integer count(); | ||
} |
Oops, something went wrong.