From 675ccd51874f9c005285eab3e4829e451848b4b6 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期三, 07 六月 2023 11:11:29 +0800
Subject: [PATCH] Merge branch 'master' of http://git.homecommunity.cn/supervip/MicroCommunity

---
 java110-db/src/main/java/com/java110/db/DataSourceConfig.java |   69 ++++++++++++++++++++++++++--------
 1 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/java110-db/src/main/java/com/java110/db/DataSourceConfig.java b/java110-db/src/main/java/com/java110/db/DataSourceConfig.java
old mode 100644
new mode 100755
index f8df53a..8f9dcf9
--- a/java110-db/src/main/java/com/java110/db/DataSourceConfig.java
+++ b/java110-db/src/main/java/com/java110/db/DataSourceConfig.java
@@ -1,19 +1,17 @@
 package com.java110.db;
 
-import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlMasterSlaveDataSourceFactory;
+import com.java110.utils.util.StringUtil;
 import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 
 import javax.servlet.Filter;
 import javax.sql.DataSource;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.sql.SQLException;
 
 /**
@@ -25,6 +23,8 @@
     //@Autowired
     private Filter statFilter;
 
+    @Autowired
+    private Environment env;
     private static final String SHARDING_YML_PATH = "dataSource.yml";
 
     /**
@@ -39,16 +39,30 @@
      */
     @Bean
     public DataSource dataSource() throws SQLException, IOException {
-//        YamlShardingConfiguration config = parse();
-//        YamlShardingRuleConfiguration rule = config.getShardingRule();
-//        for (String key : config.getDataSources().keySet()) {
-//            DruidDataSource d = (DruidDataSource) config.getDataSources().get(key);
-//            d.setProxyFilters(Lists.newArrayList(statFilter));
-//        }
-//        return ShardingDataSourceFactory.createDataSource(config.getDataSources(),
-//                rule.getShardingRuleConfiguration(), config.getConfigMap(), config.getProps());
+        String path = SHARDING_YML_PATH;
 
-        return YamlShardingDataSourceFactory.createDataSource(getYmlFile());
+        String[] actives = env.getActiveProfiles();
+        if (actives != null && actives.length > 0 && !"dev".equals(actives[0])) {
+            path = "dataSource-" + actives[0] + ".yml";
+        }
+
+        String configString = new String(getYmlFile(path), "UTF-8");
+        configString = configString.replaceAll("\\$\\{mysqlpwd\\}", env.getProperty("mysqlpwd"));
+
+        String mysqlPort = StringUtil.isEmpty(env.getProperty("mysqlport")) ? "3306" : env.getProperty("mysqlport");
+        configString = configString.replaceAll("\\$\\{mysqlport\\}", mysqlPort);
+
+        String dbttname = StringUtil.isEmpty(env.getProperty("dbttname")) ? "TT" : env.getProperty("dbttname");
+        String dbttuser = StringUtil.isEmpty(env.getProperty("dbttuser")) ? "TT" : env.getProperty("dbttuser");
+        String dbhcname = StringUtil.isEmpty(env.getProperty("dbhcname")) ? "hc_community" : env.getProperty("dbhcname");
+        String dbhcuser = StringUtil.isEmpty(env.getProperty("dbhcuser")) ? "hc_community" : env.getProperty("dbhcuser");
+
+        configString = configString.replaceAll("\\$\\{dbttname\\}", dbttname)
+                .replaceAll("\\$\\{dbttuser\\}", dbttuser)
+                .replaceAll("\\$\\{dbhcname\\}", dbhcname)
+                .replaceAll("\\$\\{dbhcuser\\}", dbhcuser);
+
+        return YamlShardingDataSourceFactory.createDataSource(configString.getBytes("UTF-8"));
     }
 
     /**
@@ -59,8 +73,29 @@
      * @throws FileNotFoundException        鏂囦欢鏈彂鐜板紓甯�
      * @throws UnsupportedEncodingException 涓嶆敮鎸佺紪鐮佸紓甯�
      */
-    private File getYmlFile() throws IOException {
-        Resource certResource = new ClassPathResource(SHARDING_YML_PATH);
-        return certResource.getFile();
+    private byte[] getYmlFile(String path) throws IOException {
+        Reader reader = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream swapStream = null;
+        try {
+            Resource resource = new ClassPathResource(path);
+
+            inputStream = resource.getInputStream();
+            swapStream = new ByteArrayOutputStream();
+            byte[] buff = new byte[100];
+            int rc = 0;
+            while ((rc = inputStream.read(buff, 0, 100)) > 0) {
+                swapStream.write(buff, 0, rc);
+            }
+            byte[] in2b = swapStream.toByteArray();
+            return in2b;
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            if (swapStream != null) {
+                swapStream.close();
+            }
+        }
     }
 }

--
Gitblit v1.8.0