DruidConfig.java
4.05 KB
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
package com.huaheng.framework.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.huaheng.framework.aspectj.lang.constant.DataSourceName;
import com.huaheng.framework.datasource.DynamicDataSource;
/**
* druid 配置多数据源
*
* @author huaheng
*/
@Configuration
public class DruidConfig
{
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource()
{
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "open", havingValue = "true")
public DataSource slaveDataSource()
{
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave2")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave2", name = "open", havingValue = "true")
public DataSource slaveDataSource2()
{
return DruidDataSourceBuilder.create().build();
}
// @Bean(name = "wcs")
// @ConfigurationProperties("spring.datasource.druid.wcs")
// public DataSource wcsDataSource()
// {
// return DruidDataSourceBuilder.create().build();
// }
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource,
DataSource slaveDataSource,
DataSource slaveDataSource2)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceName.MASTER, masterDataSource);
targetDataSources.put(DataSourceName.SLAVE, slaveDataSource);
targetDataSources.put(DataSourceName.SLAVE2, slaveDataSource2);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/monitor/druid/*");
/** 初始化参数配置,initParams**/
//白名单
bean.addInitParameter("allow", "127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
bean.addInitParameter("deny", "192.168.1.73");
//是否能够重置数据.
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 注册FilterRegistrationBean
* @return
*/
// @Bean
// public FilterRegistrationBean druidStatFilter() {
// // FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
// FilterRegistrationBean<TestFilter> bean = new FilterRegistrationBean<TestFilter>();
// bean.setFilter(new TestFilter());//注册自定义过滤器
// bean.setName("TestFilter");//过滤器名称
// bean.setOrder(Ordered.LOWEST_PRECEDENCE);//优先级,最顶级
// //添加过滤规则.
// bean.addUrlPatterns("/*");
// //添加不需要忽略的格式信息.
// // bean.addInitParameter("excludedUris","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
// bean.addInitParameter("excludedUris",".js,.gif,.jpg,.png,.css,.ico,.css,/login,/getWarehouseByUserCode,/index,/main");
//
// return bean;
// }
}