吴学文
2019-05-16 9592a96bc773354e93b048df7183cc6accfe5229
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.java110.db;
 
import org.springframework.context.annotation.Configuration;
 
import java.io.IOException;
import java.sql.SQLException;
 
/**
 * 数据源配置
 */
@Configuration
public class DataSourceConfig {
 
//    @Autowired
//    private Filter statFilter;
//
//    private static final String SHARDING_YML_PATH = "dataSource.yml";
 
    /**
     * 构建dataSource
     * 这里没有使用ShardingDataSourceFactory
     * 因为要为durid数据源配置监听Filter
     *
     * @return 数据源对象
     * @throws SQLException sql异常
     * @throws IOException  IO 异常
     * @since 1.8
     */
//    @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());
//    }
//
//    /**
//     * 解析yml
//     *
//     * @return yaml 配置文件
//     * @throws IOException                  IO 异常
//     * @throws FileNotFoundException        文件未发现异常
//     * @throws UnsupportedEncodingException 不支持编码异常
//     */
//    private MasterSlaveDataSourceFactory parse() throws IOException, FileNotFoundException, UnsupportedEncodingException {
//        Resource certResource = new ClassPathResource(SHARDING_YML_PATH);
//        try (
//                InputStreamReader inputStreamReader = new InputStreamReader(certResource.getInputStream(), "UTF-8")
//        ) {
//            return YamlMasterSlaveDataSourceFactory;
//        }
//    }
}