Parcourir la source

new property: datasource-default-config

sunyj il y a 7 ans
Parent
commit
0f3bbbc8d7

+ 2 - 4
report/src/main/java/com/uas/report/DynamicProperties.java

@@ -3,10 +3,7 @@ package com.uas.report;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.annotation.DynamicValue;
 import com.uas.report.model.ExportType;
-import com.uas.report.util.FileUtils;
-import com.uas.report.util.ObjectUtils;
-import com.uas.report.util.ResourceUtils;
-import com.uas.report.util.StringUtils;
+import com.uas.report.util.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -195,6 +192,7 @@ public class DynamicProperties {
             }
         }
         save();
+        MasterManager.init();
     }
 
     /**

+ 14 - 0
report/src/main/java/com/uas/report/SystemProperties.java

@@ -30,6 +30,12 @@ public class SystemProperties {
 	@Value("${extract-jars}")
 	private String[] extractJars;
 
+    /**
+     * 数据源的默认配置
+     */
+    @Value("${datasource-default-config}")
+	private String dataSourceDefaultConfig;
+
 	public int getTaskPeriod() {
 		return taskPeriod;
 	}
@@ -53,4 +59,12 @@ public class SystemProperties {
 	public void setExtractJars(String[] extractJars) {
 		this.extractJars = extractJars;
 	}
+
+    public String getDataSourceDefaultConfig() {
+        return dataSourceDefaultConfig;
+    }
+
+    public void setDataSourceDefaultConfig(String dataSourceDefaultConfig) {
+        this.dataSourceDefaultConfig = dataSourceDefaultConfig;
+    }
 }

+ 41 - 4
report/src/main/java/com/uas/report/util/MasterManager.java

@@ -3,6 +3,7 @@ package com.uas.report.util;
 import com.alibaba.druid.pool.DruidDataSource;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.DynamicProperties;
+import com.uas.report.SystemProperties;
 import com.uas.report.model.Master;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,24 +39,60 @@ public class MasterManager {
 	private static Logger logger = LoggerFactory.getLogger(MasterManager.class);
 
 	static {
-		initAllDataSources();
+		init();
 	}
 
 	/**
 	 * 初始化所有数据源
 	 */
-	private static void initAllDataSources() {
+	public static void init() {
+	    clear();
 		// 从配置中获取数据源信息
 		DynamicProperties dynamicProperties = ContextUtils.getBean(DynamicProperties.class);
-		JSONObject jsonObject = dynamicProperties.getDataSourceInformation();
+        JSONObject jsonObject = dynamicProperties.getDataSourceInformation();
 		Set<Entry<String, Object>> entrySet = jsonObject.entrySet();
 		for (Entry<String, Object> entry : entrySet) {
-			DruidDataSource dataSource = JSONObject.parseObject(entry.getValue().toString(), DruidDataSource.class);
+            JSONObject object = JSONObject.parseObject(entry.getValue().toString());
+            setDefaultConfig(object);
+            DruidDataSource dataSource = JSONObject.parseObject(entry.getValue().toString(), DruidDataSource.class);
 			dataSources.put(entry.getKey().toUpperCase(), dataSource);
 			masters.add(new Master(entry.getKey()));
 		}
 	}
 
+    /**
+     * 清除旧的数据源
+     */
+	private static void clear(){
+        Set<Entry<String, DruidDataSource>> entrySet = dataSources.entrySet();
+        for(Entry<String, DruidDataSource> entry : entrySet){
+            DruidDataSource dataSource = entry.getValue();
+            try {
+                dataSource.close();
+            } catch (Exception e) {
+                logger.error("数据源关闭失败", e);
+            }
+        }
+        dataSources = new ConcurrentHashMap<>();
+    }
+
+    /**
+     * 加载默认数据源配置
+     */
+    private static void setDefaultConfig(JSONObject dataSource){
+        SystemProperties systemProperties = ContextUtils.getBean(SystemProperties.class);
+        JSONObject dataSourceDefaultConfig = JSONObject.parseObject(systemProperties.getDataSourceDefaultConfig());
+        // 若数据源部分配置未显式指定,则使用默认值
+        Set<Entry<String, Object>> entrySet = dataSourceDefaultConfig.entrySet();
+        for(Entry<String, Object> entry : entrySet){
+            String key = entry.getKey();
+            if(StringUtils.isEmpty(dataSource.getString(key))){
+                dataSource.put(key, entry.getValue());
+            }
+        }
+    }
+
+
 	/**
 	 * 获取账套对应的数据源
 	 *

+ 3 - 1
report/src/main/resources/application.yml

@@ -29,4 +29,6 @@ schedule:
  period: 3600000
  auto-start: true
  
-extract-jars: report-common, jasperreports, fastjson
+extract-jars: report-common, jasperreports, fastjson
+
+datasource-default-config: "{\"initialSize\":1,\"minIdle\":0,\"maxActive\":20,\"maxWait\":60000,\"timeBetweenEvictionRunsMillis\":60000,\"minEvictableIdleTimeMillis\":300000,\"validationQuery\":\"SELECT 1 FROM DUAL\",\"testWhileIdle\":true,\"testOnBorrow\":true,\"testOnReturn\":false,\"removeAbandoned\":true,\"removeAbandonedTimeout\":120,\"logAbandoned\":true,\"timeBetweenLogStatsMillis\":600000,\"poolPreparedStatements\":true,\"maxPoolPreparedStatementPerConnectionSize\":20,\"filters\":\"stat,slf4j\",\"connectionProperties\":\"druid.stat.mergeSql=false;druid.stat.slowSqlMillis=5000\"}"

+ 1 - 1
report/src/main/resources/report.properties

@@ -13,4 +13,4 @@ use-xlsx=false
 # support PDF, XLS, XLS_DATA, XLSX, XLSX_DATA, DOC, TXT in com.uas.report.model.ExportType
 page.preview.show-export-buttons=PDF, XLS, XLS_DATA
 
-datasource={"B2B_dev":{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@192.168.253.6:1521:orcl","username":"uuplatformdemo","password":"selectuuplatform","initialSize":1,"minIdle":0,"maxActive":20,"maxWait":60000,"timeBetweenEvictionRunsMillis":60000,"minEvictableIdleTimeMillis":300000,"validationQuery":"SELECT 1 FROM DUAL","testWhileIdle":true,"testOnBorrow":true,"testOnReturn":false,"removeAbandoned":true,"removeAbandonedTimeout":120,"logAbandoned":true,"timeBetweenLogStatsMillis":600000,"poolPreparedStatements":true,"maxPoolPreparedStatementPerConnectionSize":20,"filters":"stat,slf4j","connectionProperties":"druid.stat.mergeSql=false;druid.stat.slowSqlMillis=5000"}, "UAS":{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@127.0.0.1:1521:orcl","username":"UAS","password":"select!#%*(","initialSize":1,"minIdle":0,"maxActive":20,"maxWait":60000,"timeBetweenEvictionRunsMillis":60000,"minEvictableIdleTimeMillis":300000,"validationQuery":"SELECT 1 FROM DUAL","testWhileIdle":true,"testOnBorrow":true,"testOnReturn":false,"removeAbandoned":true,"removeAbandonedTimeout":120,"logAbandoned":true,"timeBetweenLogStatsMillis":600000,"poolPreparedStatements":true,"maxPoolPreparedStatementPerConnectionSize":20,"filters":"stat,slf4j","connectionProperties":"druid.stat.mergeSql=false;druid.stat.slowSqlMillis=5000"}}
+datasource={"B2B_dev":{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@192.168.253.6:1521:orcl","username":"uuplatformdemo","password":"selectuuplatform"}, "UAS":{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@192.168.253.6:1521:orcl","username":"UAS","password":"select!#%*("}}