数据库|springboot 配置Druid多数据源监控

springboot配置文件配置如下:
# 数据源一
spring.datasource.ky_data.name=ky_data-db
spring.datasource.ky_data.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.ky_data.url= jdbc:mysql://127.0.0.1:3306/ky_data?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&usessl=false
spring.datasource.ky_data.username= root
spring.datasource.ky_data.password= 123456
spring.datasource.ky_data.type= com.alibaba.druid.pool.DruidDataSource
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.ky_data.filters=stat,wall,log4j
spring.datasource.ky_data.validationQuery=SELECT 1 FROM DUAL
spring.datasource.ky_data.logSlowSql=true
# 数据源二
spring.datasource.ky_user.name=user-db
spring.datasource.ky_user.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.ky_user.url= jdbc:mysql://127.0.0.1:3306/ky_user_notice?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&usessl=false
spring.datasource.ky_user.username= root
spring.datasource.ky_user.password= 123456
spring.datasource.ky_user.type= com.alibaba.druid.pool.DruidDataSource
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.ky_user.filters=stat,wall,log4j
spring.datasource.ky_user.validationQuery=SELECT 1 FROM DUAL
spring.datasource.ky_user.logSlowSql=true
# 数据源三
【数据库|springboot 配置Druid多数据源监控】spring.datasource.ky_order.name=order-db
spring.datasource.ky_order.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.ky_order.url= jdbc:mysql://127.0.0.1:3306/ky_order?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&usessl=false
spring.datasource.ky_order.username= root
spring.datasource.ky_order.password= 123456
spring.datasource.ky_order.type= com.alibaba.druid.pool.DruidDataSource
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.ky_order.filters=stat,wall,log4j
spring.datasource.ky_order.validationQuery=SELECT 1 FROM DUAL
spring.datasource.ky_order.logSlowSql=true

# druid监控 登录信息
druid.monitor.loginUsername=admin
druid.monitor.loginPassword=admin

编写KyDataDBConfig类,如下

@Data @Configuration("kyDataDBConfig") @ConfigurationProperties(prefix = "spring.datasource.ky_data") public class KyDataDBConfig {private String name; private String url; private String username; private String password; private String driverClassName; //private int initialSize; //private int minIdle; //private int maxActive; //private int maxWait; //private int timeBetweenEvictionRunsMillis; //private int minEvictableIdleTimeMillis; private String validationQuery; //private boolean testWhileIdle; //private boolean testOnBorrow; //private boolean testOnReturn; //private boolean poolPreparedStatements; //private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String logSlowSql; //private String connectionProperties; //private boolean useGlobalDataSourceStat; }

编写KyOrderDBConfig类,如下
@Data @Configuration("kyOrderDBConfig") @ConfigurationProperties(prefix = "spring.datasource.ky_order") public class KyOrderDBConfig { private String name; private String url; private String username; private String password; private String driverClassName; //private int initialSize; //private int minIdle; //private int maxActive; //private int maxWait; //private int timeBetweenEvictionRunsMillis; //private int minEvictableIdleTimeMillis; private String validationQuery; //private boolean testWhileIdle; //private boolean testOnBorrow; //private boolean testOnReturn; //private boolean poolPreparedStatements; //private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String logSlowSql; //private String connectionProperties; //private boolean useGlobalDataSourceStat; }

编写KyUserDBConfig类,如下:
@Data @Configuration("kyUserDBConfig") @ConfigurationProperties(prefix = "spring.datasource.ky_user") public class KyUserDBConfig { private String name; private String url; private String username; private String password; private String driverClassName; //private int initialSize; //private int minIdle; //private int maxActive; //private int maxWait; //private int timeBetweenEvictionRunsMillis; //private int minEvictableIdleTimeMillis; private String validationQuery; //private boolean testWhileIdle; //private boolean testOnBorrow; //private boolean testOnReturn; //private boolean poolPreparedStatements; //private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String logSlowSql; //private String connectionProperties; //private boolean useGlobalDataSourceStat; }

编写ManyDataSourceConfig类,如下:
@Configuration @EnableTransactionManagement(proxyTargetClass = true) public class ManyDataSourceConfig {private static final Logger logger = LoggerFactory.getLogger(ManyDataSourceConfig.class); public static final String DATA_SOURCE_KY_DATA = "https://www.it610.com/article/kyDataDataSource"; public static final String DATA_SOURCE_KY_USER = "kyUserDataSource"; public static final String DATA_SOURCE_KY_ORDER = "kyOrderDataSource"; @Bean(name = DATA_SOURCE_KY_DATA) @Qualifier(DATA_SOURCE_KY_DATA) @Primary @ConfigurationProperties(prefix = "spring.datasource.ky_data") public DataSource kyDataDataSource(@Qualifier("kyDataDBConfig") KyDataDBConfig db) { //return DataSourceBuilder.create().build(); // 屏蔽采用自动注入方式, 采用druidDataSource方式加载数据库数据源 by zhangjf 20190509 add final DruidDataSource datasource = new DruidDataSource(); datasource.setName(db.getName()); datasource.setUrl(db.getUrl()); datasource.setUsername(db.getUsername()); datasource.setPassword(db.getPassword()); datasource.setDriverClassName(db.getDriverClassName()); //datasource.setInitialSize(db.getInitialSize()); //datasource.setMinIdle(db.getMinIdle()); //datasource.setMaxActive(db.getMaxActive()); //datasource.setMaxWait(db.getMaxWait()); //datasource.setTimeBetweenEvictionRunsMillis(db.getTimeBetweenEvictionRunsMillis()); //datasource.setMinEvictableIdleTimeMillis(db.getMinEvictableIdleTimeMillis()); //datasource.setValidationQuery(db.getValidationQuery()); //datasource.setTestOnReturn(db.isTestOnReturn()); //datasource.setPoolPreparedStatements(db.isPoolPreparedStatements()); //datasource.setMaxPoolPreparedStatementPerConnectionSize(db.getMaxPoolPreparedStatementPerConnectionSize()); //datasource.setUseGlobalDataSourceStat(db.isUseGlobalDataSourceStat()); try { datasource.setFilters(db.getFilters()); } catch (SQLException e) { logger.error("setFilters Exception : ", e); } //datasource.setConnectionProperties(db.getConnectionProperties()); datasource.setValidationQuery(db.getValidationQuery()); return datasource; }@Bean(name = DATA_SOURCE_KY_USER) @Qualifier(DATA_SOURCE_KY_USER) @ConfigurationProperties(prefix = "spring.datasource.ky_user") public DataSource kyUserDataSource(@Qualifier("kyUserDBConfig") KyUserDBConfig db) { //return DataSourceBuilder.create().build(); // 屏蔽采用自动注入方式, 采用druidDataSource方式加载数据库数据源 by zhangjf 20190509 add final DruidDataSource datasource = new DruidDataSource(); datasource.setName(db.getName()); datasource.setUrl(db.getUrl()); datasource.setUsername(db.getUsername()); datasource.setPassword(db.getPassword()); datasource.setDriverClassName(db.getDriverClassName()); //datasource.setInitialSize(db.getInitialSize()); //datasource.setMinIdle(db.getMinIdle()); //datasource.setMaxActive(db.getMaxActive()); //datasource.setMaxWait(db.getMaxWait()); //datasource.setTimeBetweenEvictionRunsMillis(db.getTimeBetweenEvictionRunsMillis()); //datasource.setMinEvictableIdleTimeMillis(db.getMinEvictableIdleTimeMillis()); //datasource.setValidationQuery(db.getValidationQuery()); //datasource.setTestOnReturn(db.isTestOnReturn()); //datasource.setPoolPreparedStatements(db.isPoolPreparedStatements()); //datasource.setMaxPoolPreparedStatementPerConnectionSize(db.getMaxPoolPreparedStatementPerConnectionSize()); //datasource.setUseGlobalDataSourceStat(db.isUseGlobalDataSourceStat()); try { datasource.setFilters(db.getFilters()); } catch (SQLException e) { logger.info("setFilters Exception : ", e); } //datasource.setConnectionProperties(db.getConnectionProperties()); datasource.setValidationQuery(db.getValidationQuery()); return datasource; }@Bean(name = DATA_SOURCE_KY_ORDER) @Qualifier(DATA_SOURCE_KY_ORDER) @ConfigurationProperties(prefix = "spring.datasource.ky_order") public DataSource kyOrderDataSource(@Qualifier("kyOrderDBConfig") KyOrderDBConfig db) { //return DataSourceBuilder.create().build(); // 屏蔽采用自动注入方式, 采用druidDataSource方式加载数据库数据源 by zhangjf 20190509 add final DruidDataSource datasource = new DruidDataSource(); datasource.setName(db.getName()); datasource.setUrl(db.getUrl()); datasource.setUsername(db.getUsername()); datasource.setPassword(db.getPassword()); datasource.setDriverClassName(db.getDriverClassName()); //datasource.setInitialSize(db.getInitialSize()); //datasource.setMinIdle(db.getMinIdle()); //datasource.setMaxActive(db.getMaxActive()); //datasource.setMaxWait(db.getMaxWait()); //datasource.setTimeBetweenEvictionRunsMillis(db.getTimeBetweenEvictionRunsMillis()); //datasource.setMinEvictableIdleTimeMillis(db.getMinEvictableIdleTimeMillis()); //datasource.setValidationQuery(db.getValidationQuery()); //datasource.setTestOnReturn(db.isTestOnReturn()); //datasource.setPoolPreparedStatements(db.isPoolPreparedStatements()); //datasource.setMaxPoolPreparedStatementPerConnectionSize(db.getMaxPoolPreparedStatementPerConnectionSize()); //datasource.setUseGlobalDataSourceStat(db.isUseGlobalDataSourceStat()); try { datasource.setFilters(db.getFilters()); } catch (SQLException e) { logger.error("setFilters Exception : ", e); } //datasource.setConnectionProperties(db.getConnectionProperties()); datasource.setValidationQuery(db.getValidationQuery()); return datasource; }}

编写DataDruidConfig类,如下:

@Configuration public class DataDruidConfig {@Value("${druid.monitor.loginUsername}") private String loginUsername; @Value("${druid.monitor.loginPassword}") private String loginPassword; @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); filterRegistrationBean.addInitParameter("profileEnable", "true"); return filterRegistrationBean; }@Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); reg.addUrlMappings("/druid/*"); reg.setServlet(new StatViewServlet()); Map initParameters = new HashMap<>(); initParameters.put("resetEnable", "false"); //禁用HTML页面上的“Rest All”功能 //initParameters.put("allow", "10.8.9.115"); //ip白名单(没有配置或者为空,则允许所有访问) initParameters.put("loginUsername", StringUtils.isNotBlank(loginUsername) ? loginUsername : "admin"); ; //监控页面登录用户名 initParameters.put("loginPassword", StringUtils.isNotBlank(loginPassword) ? loginPassword : "admin"); //监控页面登录用户密码 //initParameters.put("deny", "127.0.0.1"); //ip黑名单 //如果某个ip同时存在,deny优先于allow reg.setInitParameters(initParameters); return reg; } }

项目启动,访问项目地址:http://localhost:8080/druid/login.html
就可以访问如下界面:
数据库|springboot 配置Druid多数据源监控
文章图片

数据库|springboot 配置Druid多数据源监控
文章图片

    推荐阅读