欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot - 整合Druid数据源并配置监控

程序员文章站 2024-03-12 22:55:14
...

这里SpringBoot版本为1.5.9.RELEASE.

【1】pom文件添加依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

【2】yml文件对数据源进行配置

yml文件示例如下:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: true
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

如下图所示:

SpringBoot - 整合Druid数据源并配置监控

从initialSize属性开始,属性颜色呈现为黄色,这是不正常的。SpringBoot中,数据源配置默认对应DataSourceProperties类,很显然我们为Druid数据源配置的属性与默认数据源配置文件类不符。

解决方法:容器中注册自定义数据源!


【3】注册Druid数据源

MyConfig 类如下:

@Configuration
public class MyConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }
}

测试如下图,拿到了配置文件中的数据源配置:

SpringBoot - 整合Druid数据源并配置监控


【4】配置Druid的监控

之所以使用Druid数据源,除了其性能可靠外,还在于其完善的数据监控机制。

其主要是由StatViewServlet和WebStatFilter(用于配置Web和Druid数据源之间的管理关联监控统计)实现。

配置类如下:

@Configuration
public class MyConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new StatViewServlet());
        registrationBean.setUrlMappings(Arrays.asList("/druid/*"));
        //设置初始化参数
        Map<String,String> initMap = new HashMap<>();
        initMap.put("loginUsername","admin");
        initMap.put("loginPassword","123456");
        initMap.put("allow","");
        initMap.put("deny","192.168.2.110");
        registrationBean.setInitParameters(initMap);
        return registrationBean;
    }
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new WebStatFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/*"));
        //设置初始化参数
        Map<String,String> initMap = new HashMap<>();
        initMap.put("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico");
        registrationBean.setInitParameters(initMap);
        return registrationBean;
    }
}

测试如下:

SpringBoot - 整合Druid数据源并配置监控
SpringBoot - 整合Druid数据源并配置监控


其对应xml配置如下:

<!-- druid监控配置 -->
  <servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    <init-param>
      <!-- 用户名 -->
      <param-name>loginUsername</param-name>
      <param-value>admin</param-value>
    </init-param>
    <init-param>
      <!-- 密码 -->
      <param-name>loginPassword</param-name>
      <param-value>123456</param-value>
    </init-param>
    <!-- 拒绝 -->
      <param-name>deny</param-name>
      <param-value>192.168.2.110</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>DruidWebStatFilter</filter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
    <init-param>
      <param-name>exclusions</param-name>
      <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>DruidWebStatFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
相关标签: SpringBoot Druid