From 5b18c5a8c26194474b65b180c6e87e180a03c646 Mon Sep 17 00:00:00 2001
From: heheng <475597332@qq.com>
Date: 星期五, 18 四月 2025 14:08:03 +0800
Subject: [PATCH] sharding版本

---
 hazmat-framework/src/main/java/com/gkhy/hazmat/framework/config/DruidConfig.java |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)

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);
     }
 

--
Gitblit v1.9.2