springboot2.x 注入druid 監控 非yaml 格式

導入com.alibaba:druid-spring-boot-starter:1.1.9 包css

建立類 import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List;java

import javax.servlet.Servlet; import javax.sql.DataSource;mysql

import com.alibaba.druid.filter.Filter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; 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 com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter;web

/**spring

  • @DESC druid配置類,會被springboot掃描而後將相應的service加到容器中 */ @Configuration @Slf4j public class DruidConfiguration { @Value("${spring.datasource.mydruid.Filters:stat,wall,slf4j}") private String Filters; @Value("${spring.datasource.mydruid.InitialSize:5}") private Integer InitialSize; @Value("${spring.datasource.mydruid.MinIdle:5}") private Integer MinIdle; @Value("${spring.datasource.mydruid.MaxActive:20}") private Integer MaxActive; @Value("${spring.datasource.mydruid.PoolPreparedStatements:true}") private boolean PoolPreparedStatements; @Value("${spring.datasource.mydruid.MaxPoolPreparedStatementPerConnectionSize:20}") private Integer MaxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.mydruid.ValidationQuery:select 'x'}") private String ValidationQuery; @Value("${spring.datasource.mydruid.TestOnBorrow:false}") private boolean TestOnBorrow; @Value("${spring.datasource.mydruid.TestOnReturn:false}") private boolean TestOnReturn; @Value("${spring.datasource.mydruid.WhileIdle:true}") private boolean WhileIdle; @Value("${spring.datasource.mydruid.MaxWait:6000}") private Integer MaxWait; @Value("${spring.datasource.mydruid.TimeBetweenEvictionRunsMillis:60000}") private Integer TimeBetweenEvictionRunsMillis; @Value("${spring.datasource.mydruid.UseGlobalDataSourceStat:true}") private boolean UseGlobalDataSourceStat;sql

    @Value("${spring.datasource.mydruid.loginUsername:admin}") private String loginUsername; @Value("${spring.datasource.mydruid.loginPassword:admin}") private String loginPassword; @Value("${spring.datasource.mydruid.resetEnable:true}") private String resetEnable; @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource();springboot

    //        配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
     druidDataSource.setFilters(Filters);
     //自定義 sql 統計 應爲druid 沒有時間段 能夠本身寫
    // List<Filter> filterList=new ArrayList<>();
    // filterList.add(new TestDruidFilter());
    // druidDataSource.setProxyFilters(filterList);
     // 初始化大小,最小,最大
     druidDataSource.setInitialSize(InitialSize);
     druidDataSource.setMinIdle(MinIdle);

// 最大等待(空閒)鏈接中的數量,設 0 爲沒有限制 druidDataSource.setMaxActive(MaxActive); // 打開PSCache,而且指定每一個鏈接上PSCache的大小 druidDataSource.setPoolPreparedStatements(PoolPreparedStatements); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(MaxPoolPreparedStatementPerConnectionSize); // 校驗sql druidDataSource.setValidationQuery(ValidationQuery); // 指明是否在從池中取出鏈接前進行檢驗,若是檢驗失敗 則從池中去除鏈接並嘗試取出另外一個. 注意: 設置爲true後若是要生效,validationQuery參數必須設置爲非空字符串 druidDataSource.setTestOnBorrow(TestOnBorrow); // 指明是否在歸還到池中前進行檢驗 druidDataSource.setTestOnReturn(TestOnReturn); druidDataSource.setTestWhileIdle(WhileIdle); // 配置獲取鏈接等待超時的時間 最大等待毫秒數, 單位爲 ms, 若是超過此時間將接到異常,設爲-1表示無限制 druidDataSource.setMaxWait(MaxWait); // 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 druidDataSource.setTimeBetweenEvictionRunsMillis(TimeBetweenEvictionRunsMillis); // 合併多個DruidDataSource的監控數據 druidDataSource.setUseGlobalDataSourceStat(UseGlobalDataSourceStat); return druidDataSource; }app

/**
 * 配置druid管理頁面的訪問控制
 * 訪問網址: http://127.0.0.1:8080/druid
 *
 * @return
 */
@Bean
public ServletRegistrationBean<Servlet> druidServlet() {
    log.info("初始化配置--druid");
    ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>();
    servletRegistrationBean.setServlet(new StatViewServlet());  //配置一個攔截器
    servletRegistrationBean.addUrlMappings("/druid/*");    //指定攔截器只攔截druid管理頁面的請求
    HashMap<String, String> initParam = new HashMap<String, String>();
    initParam.put("loginUsername", loginUsername);    //登陸druid管理頁面的用戶名
    initParam.put("loginPassword", loginPassword);    //登陸druid管理頁面的密碼
    initParam.put("resetEnable", resetEnable);       //是否容許重置druid的統計信息
    initParam.put("allow", "");         //ip白名單,若是沒有設置或爲空,則表示容許全部訪問

    servletRegistrationBean.setInitParameters(initParam);
    return servletRegistrationBean;
}

@Bean
public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
    FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
    filterRegistrationBean.setFilter(new WebStatFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
}

} 實現自定義接口 從源碼看的 import com.alibaba.druid.filter.FilterEventAdapter; import com.alibaba.druid.proxy.jdbc.StatementProxy;ide

public class TestDruidFilter extends FilterEventAdapter { @Override protected void statementExecuteAfter(StatementProxy statement, String sql, boolean result) {spring-boot

System.err.println("監控sql----------執行後-------");
    System.err.println("sql-----"+sql);
    System.err.println("statement-----");
    super.statementExecuteAfter(statement, sql, result);
}
@Override
protected void statementExecuteBefore(StatementProxy statement, String sql) {
    System.err.println("監控sql----------執行前-------");
    System.err.println("sql-----"+sql);
    System.err.println("statement-----");
    super.statementExecuteBefore(statement, sql);
}

} 配置文件 spring: application: name: datapull datasource: driver-class-name: com.mysql.cj.jdbc.Driver #mysql8 url: jdbc:mysql://xxxx:8066/xxx?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: xxx password: xxx mydruid: Filters: stat,wall,slf4j InitialSize: 5 MinIdle: 5 MaxActive: 20 PoolPreparedStatements: true MaxPoolPreparedStatementPerConnectionSize: 20 ValidationQuery: select 'x' TestOnBorrow: false TestOnReturn: false WhileIdle: false MaxWait: 6000 TimeBetweenEvictionRunsMillis: 60000 UseGlobalDataSourceStat: true loginUsername: admin loginPassword: admin resetEnable: true druid: db-type: com.alibaba.druid.pool.DruidDataSource

相關文章
相關標籤/搜索