Technical note

Druid双数据源监控

参考文档

Nacos配置

spring:
  datasource:
    loginTimeout: 20
    druid:
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        allow: ""
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: yourpass
    primary:
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://10.193.2.8:3306/efpx_dubhe_quality?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
      username: root
      password: yourpass
      driver-class-name: com.mysql.jdbc.Driver
      initialSize: 5
      maxActive: 20
      minIdle: 5
      maxWait: 10000
      poolPreparedStatements: false
      maxPoolPreparedStatementPerConnectionSize: -1
      validationQuery: SELECT 'x'
      testOnBorrow: false
      testOnReturn: false
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 30000
      filters: stat,wall,log4j2
    warehouse:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://10.193.2.8:4000/warehouse?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
      username: root
      password: yourpass
      initialSize: 5
      maxActive: 20
      minIdle: 5
      maxWait: 10000
      poolPreparedStatements: false
      maxPoolPreparedStatementPerConnectionSize: -1
      validationQuery: SELECT 'x'
      testOnBorrow: false
      testOnReturn: false
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 30000
      filters: stat,wall,log4j2

Bean配置

package com.sdecloud.efpx.quality.biz.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * JdbcTemplate配置类
 *
 * @author bluexiii
 */
@Configuration
public class JdbcTemplateConfiguration {
	@Primary
	@Bean(name = "dataSource")
	@ConfigurationProperties("spring.datasource.primary")
	public DataSource dataSource() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean(name = "warehouseDataSource")
	@ConfigurationProperties("spring.datasource.warehouse")
	public DataSource warehouseDataSource() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean("warehouseJdbcTemplate")
	public JdbcTemplate warehouseJdbcTemplate(@Qualifier("warehouseDataSource") DataSource warehouseDataSource) {
		return new JdbcTemplate(warehouseDataSource);
	}
}