From 5b18c5a8c26194474b65b180c6e87e180a03c646 Mon Sep 17 00:00:00 2001
From: heheng <475597332@qq.com>
Date: Fri, 18 Apr 2025 14:08:03 +0800
Subject: [PATCH] sharding版本
---
hazmat-admin/src/main/resources/application-dev.yml | 2
hazmat-framework/pom.xml | 12 ++
hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java | 6 +
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java | 2
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java | 21 ++++
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/properties/ShardingProperties.java | 15 +++
hazmat-admin/src/main/resources/application-sharding-dev.yml | 66 ++++++++++++++++
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/aspectj/DataSourceAspect.java | 74 ++++++++++++++++++
pom.xml | 6 +
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java | 1
hazmat-admin/src/main/resources/application.yml | 4
11 files changed, 201 insertions(+), 8 deletions(-)
diff --git a/hazmat-admin/src/main/resources/application-dev.yml b/hazmat-admin/src/main/resources/application-dev.yml
index 6b2a6c3..231b1b1 100644
--- a/hazmat-admin/src/main/resources/application-dev.yml
+++ b/hazmat-admin/src/main/resources/application-dev.yml
@@ -5,7 +5,7 @@
druid:
# 主库数据源
master:
- url: jdbc:mysql://192.168.2.16:7006/hazmat_manage?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
+ url: jdbc:mysql://192.168.2.29:7006/hazmat_manage?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
username: root
password: 2farwL3yPXfbH2AP
# 从库数据源
diff --git a/hazmat-admin/src/main/resources/application-sharding-dev.yml b/hazmat-admin/src/main/resources/application-sharding-dev.yml
new file mode 100644
index 0000000..b5c5e32
--- /dev/null
+++ b/hazmat-admin/src/main/resources/application-sharding-dev.yml
@@ -0,0 +1,66 @@
+# 数据源
+dataSources:
+ sharding:
+ dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ url: jdbc:mysql://192.168.2.29:7006/hazmat_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+ username: root
+ password: 2farwL3yPXfbH2AP
+ maxTotal: 100
+# ds_1:
+# dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
+# driverClassName: com.mysql.cj.jdbc.Driver
+# url: jdbc:mysql://localhost:3306/ry-order2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+# username: root
+# password: root
+# maxTotal: 100
+rules:
+ - !SHARDING
+ tables:
+ hz_hazmat:
+ actualDataNodes: sharding.hz_hazmat_$->{0..19}
+ tableStrategy:
+ standard:
+ shardingColumn: company_id
+ shardingAlgorithmName: hz_hazmat_inline
+ hz_hazmat_flow:
+ actualDataNodes: sharding.hz_hazmat_flow_$->{0..19}
+ tableStrategy:
+ standard:
+ shardingColumn: company_id
+ shardingAlgorithmName: hz_hazmat_flow_inline
+ hz_product:
+ actualDataNodes: sharding.hz_product_$->{0..19}
+ tableStrategy:
+ standard:
+ shardingColumn: company_id
+ shardingAlgorithmName: hz_product_inline
+ hz_product_flow:
+ actualDataNodes: sharding.hz_product_flow_$->{0..19}
+ tableStrategy:
+ standard:
+ shardingColumn: company_id
+ shardingAlgorithmName: hz_product_flow_inline
+ shardingAlgorithms:
+ hz_hazmat_inline:
+ type: INLINE
+ props:
+ algorithm-expression: hz_hazmat_$->{company_id % 20}
+ hz_hazmat_flow_inline:
+ type: INLINE
+ props:
+ algorithm-expression: hz_hazmat_flow_$->{company_id % 20}
+ hz_product_inline:
+ type: INLINE
+ props:
+ algorithm-expression: hz_product_$->{company_id % 20}
+ hz_product_flow_inline:
+ type: INLINE
+ props:
+ algorithm-expression: hz_product_flow_$->{company_id % 20}
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+props:
+ sql-show: true # 是否打印sql
+
diff --git a/hazmat-admin/src/main/resources/application.yml b/hazmat-admin/src/main/resources/application.yml
index 93d1491..9454be8 100644
--- a/hazmat-admin/src/main/resources/application.yml
+++ b/hazmat-admin/src/main/resources/application.yml
@@ -13,7 +13,9 @@
mvc:
pathmatch:
matching-strategy: ant_path_matcher
-
+ #分库分表
+ shardingsphere:
+ configLocation: application-sharding-dev.yml
server:
port: 8083
servlet:
diff --git a/hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java b/hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java
index 2d1e876..bf2050d 100644
--- a/hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java
+++ b/hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java
@@ -11,5 +11,9 @@
/**
* 从库
*/
- SLAVE
+ SLAVE,
+ /**
+ * 分库
+ */
+ SHARDING;
}
diff --git a/hazmat-framework/pom.xml b/hazmat-framework/pom.xml
index c322721..4e06497 100644
--- a/hazmat-framework/pom.xml
+++ b/hazmat-framework/pom.xml
@@ -28,7 +28,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-
+ <!-- 阿里数据库连接池 -->
+ <dependency>
+ <groupId>com.alibaba</groupId>
+ <artifactId>druid-spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
+ <version>5.2.0</version>
+ </dependency>
<!-- 验证码 -->
<dependency>
<groupId>pro.fessional</groupId>
@@ -40,6 +49,7 @@
</exclusion>
</exclusions>
</dependency>
+
</dependencies>
</project>
\ No newline at end of file
diff --git a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/aspectj/DataSourceAspect.java b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/aspectj/DataSourceAspect.java
new file mode 100644
index 0000000..049c2d0
--- /dev/null
+++ b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/aspectj/DataSourceAspect.java
@@ -0,0 +1,74 @@
+package com.gkhy.hazmat.framework.aspectj;
+
+
+import cn.hutool.core.util.ObjectUtil;
+import com.gkhy.hazmat.common.annotation.DataSource;
+import com.gkhy.hazmat.framework.config.DynamicDataSourceContextHolder;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+import com.gkhy.hazmat.common.utils.StringUtils;
+import java.util.Objects;
+
+/**
+ * 多数据源处理
+ *
+ * @author ruoyi
+ */
+@Aspect
+@Order(1)
+@Component
+public class DataSourceAspect
+{
+ protected Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Pointcut("@annotation(com.gkhy.hazmat.common.annotation.DataSource)"
+ + "|| @within(com.gkhy.hazmat.common.annotation.DataSource)")
+ public void dsPointCut()
+ {
+
+ }
+
+ @Around("dsPointCut()")
+ public Object around(ProceedingJoinPoint point) throws Throwable
+ {
+ DataSource dataSource = getDataSource(point);
+
+ if (ObjectUtil.isNotNull(dataSource))
+ {
+ DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
+ }
+
+ 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);
+ }
+}
diff --git a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java
index ee91599..660b8ec 100644
--- a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java
+++ b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java
@@ -7,16 +7,23 @@
import com.alibaba.druid.util.Utils;
import com.gkhy.hazmat.common.enums.DataSourceType;
import com.gkhy.hazmat.framework.config.properties.DruidProperties;
+import com.gkhy.hazmat.framework.config.properties.ShardingProperties;
+import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
+import org.springframework.core.io.ClassPathResource;
import javax.servlet.*;
import javax.sql.DataSource;
+import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
@@ -38,7 +45,16 @@
DruidDataSource dataSource=DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
-
+ @Bean
+ public DataSource shardingDataSource(ShardingProperties shardingProperties) throws Exception
+ {
+ ClassPathResource classPathResource = new ClassPathResource(shardingProperties.getConfigLocation());
+ InputStream inputStream = classPathResource.getInputStream();
+ File tmpFile = File.createTempFile(shardingProperties.getConfigLocation(), ".tmp");
+ Files.copy(inputStream, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(tmpFile);
+ return dataSource;
+ }
@Bean(name="dynamicDataSource")
@@ -46,7 +62,8 @@
public DynamicDataSource dataSource(DataSource masterDataSource){
Map<Object,Object> targetDataSources=new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(),masterDataSource);
- setDataSource(targetDataSources,DataSourceType.SLAVE.name(),"slaveDataSource");
+// setDataSource(targetDataSources,DataSourceType.SLAVE.name(),"slaveDataSource");
+ setDataSource(targetDataSources, DataSourceType.SHARDING.name(), "shardingDataSource");
return new DynamicDataSource(masterDataSource,targetDataSources);
}
diff --git a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java
index fd48f36..89d2e67 100644
--- a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java
+++ b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java
@@ -1,6 +1,5 @@
package com.gkhy.hazmat.framework.config;
-import com.gkhy.hazmat.common.config.DynamicDataSourceContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
diff --git a/hazmat-common/src/main/java/com/gkhy/hazmat/common/config/DynamicDataSourceContextHolder.java b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java
similarity index 95%
rename from hazmat-common/src/main/java/com/gkhy/hazmat/common/config/DynamicDataSourceContextHolder.java
rename to hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java
index 670b9b2..c1cb9b9 100644
--- a/hazmat-common/src/main/java/com/gkhy/hazmat/common/config/DynamicDataSourceContextHolder.java
+++ b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java
@@ -1,4 +1,4 @@
-package com.gkhy.hazmat.common.config;
+package com.gkhy.hazmat.framework.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/properties/ShardingProperties.java b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/properties/ShardingProperties.java
new file mode 100644
index 0000000..2864ce3
--- /dev/null
+++ b/hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/properties/ShardingProperties.java
@@ -0,0 +1,15 @@
+package com.gkhy.hazmat.framework.config.properties;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class ShardingProperties {
+
+ @Value("${spring.shardingsphere.configLocation}")
+ private String configLocation;
+
+ public String getConfigLocation() {
+ return configLocation;
+ }
+}
diff --git a/pom.xml b/pom.xml
index bb86fd9..c471e05 100644
--- a/pom.xml
+++ b/pom.xml
@@ -210,6 +210,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <!-- 分库分表引擎 -->
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
+ <version>5.2.0</version>
+ </dependency>
</dependencies>
--
Gitblit v1.9.2