From 58ada741b40cd6ebd590b777c18d0f77548e043b Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期四, 25 七月 2019 14:16:53 +0800
Subject: [PATCH] 注册组件调整
---
java110-db/src/main/java/com/java110/db/DataSourceConfig.java | 64 +++++++++++++++++++------------
1 files changed, 39 insertions(+), 25 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
index e3bef6d..159df84 100644
--- a/java110-db/src/main/java/com/java110/db/DataSourceConfig.java
+++ b/java110-db/src/main/java/com/java110/db/DataSourceConfig.java
@@ -1,23 +1,18 @@
package com.java110.db;
-import com.alibaba.druid.filter.Filter;
-import com.alibaba.druid.pool.DruidDataSource;
-import com.google.common.collect.Lists;
-import io.shardingsphere.core.yaml.sharding.YamlShardingConfiguration;
-import io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration;
-import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
-import org.yaml.snakeyaml.Yaml;
-import org.yaml.snakeyaml.constructor.Constructor;
+import javax.servlet.Filter;
import javax.sql.DataSource;
+import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.InputStream;
+import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
@@ -27,7 +22,7 @@
@Configuration
public class DataSourceConfig {
- @Autowired
+ //@Autowired
private Filter statFilter;
private static final String SHARDING_YML_PATH = "dataSource.yml";
@@ -44,14 +39,16 @@
*/
@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());
+// 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());
+
+ return YamlShardingDataSourceFactory.createDataSource(getYmlFile());
}
/**
@@ -62,12 +59,29 @@
* @throws FileNotFoundException 鏂囦欢鏈彂鐜板紓甯�
* @throws UnsupportedEncodingException 涓嶆敮鎸佺紪鐮佸紓甯�
*/
- private YamlShardingConfiguration parse() throws IOException, FileNotFoundException, UnsupportedEncodingException {
- Resource certResource = new ClassPathResource(SHARDING_YML_PATH);
- try (
- InputStreamReader inputStreamReader = new InputStreamReader(certResource.getInputStream(), "UTF-8")
- ) {
- return new Yaml(new Constructor(YamlShardingConfiguration.class)).loadAs(inputStreamReader, YamlShardingConfiguration.class);
+ private byte[] getYmlFile() throws IOException {
+ Reader reader = null;
+ InputStream inputStream = null;
+ ByteArrayOutputStream swapStream = null;
+ try {
+ Resource resource = new ClassPathResource(SHARDING_YML_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