危化品全生命周期管理后端
heheng
2025-04-18 5b18c5a8c26194474b65b180c6e87e180a03c646
sharding版本
已重命名1个文件
已修改7个文件
已添加3个文件
209 ■■■■■ 文件已修改
hazmat-admin/src/main/resources/application-dev.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-admin/src/main/resources/application-sharding-dev.yml 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-admin/src/main/resources/application.yml 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/pom.xml 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/aspectj/DataSourceAspect.java 74 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSource.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/properties/ShardingProperties.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
      # 从库数据源
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
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:
hazmat-common/src/main/java/com/gkhy/hazmat/common/enums/DataSourceType.java
@@ -11,5 +11,9 @@
    /**
     * 从库
     */
    SLAVE
    SLAVE,
    /**
     * 分库
     */
    SHARDING;
}
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>
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);
    }
}
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);
    }
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;
hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DynamicDataSourceContextHolder.java
文件名从 hazmat-common/src/main/java/com/gkhy/hazmat/common/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;
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;
    }
}
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>