zhaowenxuan
2025-02-21 57370b6e66633c1928593b74b3fafcce14631111
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package com.ltkj.framework.config;
 
import java.io.*;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.DataSource;
 
import com.ltkj.framework.config.properties.DruidProperties;
import com.ltkj.framework.datasource.DynamicDataSource;
import com.ltkj.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import com.ltkj.common.enums.DataSourceType;
import com.ltkj.common.utils.spring.SpringUtils;
 
/**
 * druid 配置多数据源
 *
 * @author ltkj
 */
//@Configuration
@Slf4j
public class DruidConfig {
 
 
    @Value ("${config.properties}")
    private  String url;
 
    @Value ("${config.path}")
    private  String path;
 
 
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.master")
    public DataSource masterDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到 系统正在创建!");
 
                File f = new File(path);
                if(!f.exists()){
                    f.mkdirs();
                }
                File file = new File(url);
                try {
                    FileWriter fileWriter = new FileWriter(file);
                    fileWriter.write("ip = 你的主数据库连接ip地址\n");
                    fileWriter.write("prot = 你的主数据库连接端口\n");
                    fileWriter.write("name = 你的主数据库连接名称\n");
                    fileWriter.write("username = 你的主数据库连接用户名\n");
                    fileWriter.write("password = 你的主数据库连接密码\n");
                    fileWriter.write("\n");
 
 
                    fileWriter.write("hisenabled = 是否开启 his 从库数据库连接 (从数据源开关/默认关闭 fales)\n");
                    fileWriter.write("hisip = 你的 his 数据库连接ip地址\n");
                    fileWriter.write("hisprot = 你的 his 数据库连接端口\n");
                    fileWriter.write("hisname = 你的 his 数据库连接名称\n");
                    fileWriter.write("hisusername = 你的 his 数据库连接用户名\n");
                    fileWriter.write("hispassword = 你的 his 数据库连接密码\n");
                    fileWriter.write("\n");
 
                    fileWriter.write("pacsenabled = 是否开启pacs从库数据库连接 (从数据源开关/默认关闭 fales)\n");
                    fileWriter.write("pacsip = 你的pacs数据库连接ip地址\n");
                    fileWriter.write("pacsprot = 你的pacs数据库连接端口\n");
                    fileWriter.write("pacsname = 你的pacs数据库连接名称\n");
                    fileWriter.write("pacsusername = 你的pacs数据库连接用户名\n");
                    fileWriter.write("pacspassword = 你的pacs数据库连接密码\n");
                    fileWriter.write("\n");
 
 
                    fileWriter.write("lisenabled = 是否开启 lis 从库数据库连接 (从数据源开关/默认关闭 fales)\n");
                    fileWriter.write("lisip = 你的 lis 数据库连接ip地址\n");
                    fileWriter.write("lisprot = 你的 lis 数据库连接端口\n");
                    fileWriter.write("lisname = 你的 lis 数据库连接名称\n");
                    fileWriter.write("lisusername = 你的 lis 数据库连接用户名\n");
                    fileWriter.write("lispassword = 你的 lis 数据库连接密码\n");
 
 
 
 
                        fileWriter.close();
                        log.info("数据库连接文件创建成功!");
                } catch (IOException ioException) {
                    log.info("数据库连接文件创建失败  请联系管理员手动创建!");
                    ioException.printStackTrace();
                }
                e.printStackTrace();
            }
            props.load(fis);
            fis.close();
            // 获取属性值并赋值
            Properties properties = new Properties();
            // 这里是测试写法,具体的value可以通过请求参数传递过来
            properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("ip")+":"+props.getProperty("prot")+"/"+props.getProperty("name")+"" +
                    "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
            properties.setProperty("druid.username",props.getProperty("username"));
            properties.setProperty("druid.password",props.getProperty("password"));
            dataSource.restart(properties);
            log.info("数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
 
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavehis")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavehis", name = "enabled", havingValue = "true")
    public DataSource slaveHisDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到!");
            }
            props.load(fis);
            fis.close();
            // 获取属性值并赋值
            String hisenabled = props.getProperty("hisenabled");
            if (hisenabled.equals("false"))
                return null;
            String hisdbtype = props.getProperty("hisdbtype");
            switch (hisdbtype){
                case "sqlserver":
                    dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword"));
                    break;
                case "mysql":
                    dataSource = creatMysql(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword"));
                    break;
                case "oracle":
                    dataSource = creatOracle(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword"));
                    break;
                default:
                    dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword"));
                    break;
            }
            log.info("his数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
 
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavelis")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavelis", name = "enabled", havingValue = "true")
    public DataSource slaveDataLisSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到!");
            }
            props.load(fis);
            fis.close();
            String lisenabled = props.getProperty("lisenabled");
            if (lisenabled.equals("false"))
                return null;
            String lisdbtype = props.getProperty("lisdbtype");
            switch (lisdbtype){
                case "sqlserver":
                    dataSource = creatSqlServer(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword"));
                    break;
                case "mysql":
                    dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword"));
                    break;
                case "oracle":
                    dataSource = creatOracle(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword"));
                    break;
                default:
                    dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword"));
                    break;
            }
            log.info("Lis数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
 
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavepacs")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true")
    public DataSource slaveDataPacsSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到");
            }
            props.load(fis);
            fis.close();
            // 这里是测试写法,具体的value可以通过请求参数传递过来
            String pacsenabled = props.getProperty("pacsenabled");
            if (pacsenabled.equals("false"))
                return null;
            String pacsdbtype = props.getProperty("pacsdbtype");
            switch (pacsdbtype){
                case "sqlserver":
                    dataSource = creatSqlServer(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword"));
                    break;
                case "mysql":
                    dataSource = creatMysql(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword"));
                    break;
                case "oracle":
                    dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword"));
                    break;
                default:
                    dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword"));
                    break;
            }
            log.info("数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
 
 
 
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavepacs")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true")
    public DataSource slaveDataWsSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到");
            }
            props.load(fis);
            fis.close();
            // 这里是测试写法,具体的value可以通过请求参数传递过来
            String pacsenabled = props.getProperty("wsenabled");
            if (pacsenabled.equals("false"))
                return null;
            String pacsdbtype = props.getProperty("wsdbtype");
            switch (pacsdbtype){
                case "sqlserver":
                    dataSource = creatSqlServer(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"),
                            props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                case "mysql":
                    dataSource = creatMysql(pacsenabled, props.getProperty("wsip"),
                            props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                case "oracle":
                    dataSource = creatOracle(pacsenabled, props.getProperty("wsip"),
                            props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                default:
                    dataSource = creatOracle(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername")
                            ,props.getProperty("wspassword"));
                    break;
            }
            log.info("数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
 
 
    private DruidDataSource creatSqlServer(String enabled, String ip,String port,String db,String user,String password) throws SQLException {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties properties = new Properties();
        properties.setProperty("druid.enabled", enabled);
        properties.setProperty("druid.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
        properties.setProperty("druid.url","jdbc:sqlserver://"+ ip+":"+ port+";DatabaseName="+ db+
                ";&characterEncoding=utf8");
        properties.setProperty("druid.username", user);
        properties.setProperty("druid.password", password);
        dataSource.restart(properties);
        return dataSource;
    }
 
    private DruidDataSource creatMysql(String enabled, String ip,String port,String db,String user,String password) throws SQLException {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties properties = new Properties();
        properties.setProperty("druid.enabled",enabled);
        properties.setProperty("druid.url","jdbc:mysql://"+ip+":"+port+"/"+db+"" +
                "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8");
        properties.setProperty("druid.username",user);
        properties.setProperty("druid.password",password);
        dataSource.restart(properties);
        return dataSource;
    }
 
    private DruidDataSource creatOracle(String enabled, String ip,String port,String db,String user,String password) throws SQLException {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties properties = new Properties();
        properties.setProperty("druid.enabled",enabled);
        properties.setProperty("druid.driverClassName","oracle.jdbc.OracleDriver");
        properties.setProperty("druid.url","jdbc:oracle:thin:@//"+ip+"/"+db);
        properties.setProperty("druid.username",user);
        properties.setProperty("druid.password",password);
        dataSource.restart(properties);
        return dataSource;
    }
 
 
    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        setDataSource(targetDataSources, DataSourceType.SLAVE_HIS.name(), "slaveHisDataSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_LIS.name(), "slaveDataLisSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_PACS.name(), "slaveDataPacsSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_WS.name(), "slaveDataWsSource");
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }
 
    /**
     * 设置数据源
     *
     * @param targetDataSources 备选数据源集合
     * @param sourceName        数据源名称
     * @param beanName          bean名称
     */
    public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) {
        try {
            DataSource dataSource = SpringUtils.getBean(beanName);
            targetDataSources.put(sourceName, dataSource);
        } catch (Exception e) {
        }
    }
 
    /**
     * 去除监控页面底部的广告
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
        final String filePath = "support/http/resources/js/common.js";
        // 创建filter进行过滤
        Filter filter = new Filter() {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException {
            }
 
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException {
                chain.doFilter(request, response);
                // 重置缓冲区,响应头不会被重置
                response.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);
                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                response.getWriter().write(text);
            }
 
            @Override
            public void destroy() {
            }
        };
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);
        return registrationBean;
    }
}