✨ 添加新特性。pigx 动态切换数据源,为后期提供Online开发、数据同步以及读写分离做准备

This commit is contained in:
Lucky committed 2019-05-21 23:57:40 +08:00
1 parent 6eaa933fde
commit 896dbdeb40
12 files changed
+221 -15

No files matched your search

+5
View File
@@ -47,6 +47,11 @@
<artifactId>pigx-common-gateway</artifactId>
<version>${pigx.version}</version>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-datasource</artifactId>
<version>${pigx.version}</version>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-job</artifactId>
+106
View File
@@ -0,0 +1,106 @@
<?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>pigx-common</artifactId>
<groupId>com.pig4cloud</groupId>
<version>3.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-datasource</artifactId>
<packaging>jar</packaging>
<description>pigx 动态切换数据源,为后期提供Online在线开发、读写分离、数据同步做好准备 </description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--common-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-core</artifactId>
</dependency>
<!--数据操作-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-data</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--common-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-core</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-swagger</artifactId>
</dependency>
<!--安全模块-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-log</artifactId>
</dependency>
<!--代码生成模板引擎-->
<dependency>
<artifactId>velocity</artifactId>
<groupId>org.apache.velocity</groupId>
<version>${velocity.version}</version>
</dependency>
<!--web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package com.pig4cloud.pigx.common.datasource;
import com.pig4cloud.pigx.common.datasource.config.DruidDataSourceProperties;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @author Lucky
* @date 2019-05-18
* <p>
* DbAcess 自动配置类
*/
@AllArgsConstructor
@EnableConfigurationProperties({DruidDataSourceProperties.class})
public class DataSourceAutoConfiguration {
}
@@ -0,0 +1,20 @@
package com.pig4cloud.pigx.common.datasource.annotation;
import com.pig4cloud.pigx.common.datasource.config.DynamicDataSourceConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author Lucky
* @date 2019-05-18
* <p>
* 开启动态数据源
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({DynamicDataSourceConfig.class})
public @interface EnableDynamicDataSource {
}
@@ -1,4 +1,4 @@
package com.pig4cloud.pigx.codegen.config;
package com.pig4cloud.pigx.common.datasource.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -1,6 +1,6 @@
package com.pig4cloud.pigx.codegen.config;
package com.pig4cloud.pigx.common.datasource.config;
import com.pig4cloud.pigx.codegen.util.DynamicDataSourceContextHolder;
import com.pig4cloud.pigx.common.datasource.support.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
@@ -1,8 +1,13 @@
package com.pig4cloud.pigx.codegen.config;
package com.pig4cloud.pigx.common.datasource.config;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.PostConstruct;
import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.Driver;
import com.pig4cloud.pigx.codegen.util.DataSourceConstants;
import com.pig4cloud.pigx.common.datasource.support.DataSourceConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
@@ -14,12 +19,6 @@ import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author lengleng
* @date 2019-03-31
@@ -0,0 +1,43 @@
package com.pig4cloud.pigx.common.datasource.config;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.pig4cloud.pigx.common.datasource.support.DynamicDataSourceContextHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
/**
* @author lengleng
* @date 2019-05-18
* <p>
* 动态数据源拦截器
*/
@Slf4j
@Component
@AllArgsConstructor
public class DynamicDatasourceInterceptor implements HandlerInterceptor {
private final RedisTemplate redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
HashOperations<String, String, String> vo = redisTemplate.opsForHash();
// String dbName = vo.get("db_route_key", 1);
DynamicDataSourceContextHolder.setDataSourceType(1);
log.info("数据源切换:{}", 1);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) {
DynamicDataSourceContextHolder.clearDataSourceType();
}
}
@@ -1,4 +1,4 @@
package com.pig4cloud.pigx.codegen.util;
package com.pig4cloud.pigx.common.datasource.support;
/**
* @author lengleng
@@ -1,25 +1,39 @@
package com.pig4cloud.pigx.codegen.util;
package com.pig4cloud.pigx.common.datasource.support;
import lombok.experimental.UtilityClass;
/**
* @author lengleng
* @date 2019-03-31
* @date 2019-05-18
* <p>
* 保存上线文中数据库路由
* 根据当前线程来选择具体的数据源
*/
@UtilityClass
public class DynamicDataSourceContextHolder {
private final ThreadLocal<Integer> CONTEXT_HOLDER = new ThreadLocal<>();
/**
* 提供给AOP去设置当前的线程的数据源的信息
*
* @param dataSourceType
*/
public void setDataSourceType(Integer dataSourceType) {
CONTEXT_HOLDER.set(dataSourceType);
}
/**
* 提供给AbstractRoutingDataSource的实现类,通过key选择数据源
*
* @return
*/
public Integer getDataSourceType() {
return CONTEXT_HOLDER.get();
}
/**
* 使用默认的数据源
*
*/
public void clearDataSourceType() {
CONTEXT_HOLDER.remove();
}
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.datasource.DataSourceAutoConfiguration
+1
View File
@@ -44,5 +44,6 @@
<module>pigx-common-swagger</module>
<module>pigx-common-transaction</module>
<module>pigx-common-autopoi</module>
<module>pigx-common-datasource</module>
</modules>
</project>