springboot之整合druid&配置数据源监控

时间:2022-11-30 18:58:22

1-maven repository中搜索druid,引入druid数据源

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.8</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2-application.properties中

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

springboot之整合druid&配置数据源监控

3-测试类中

    @Autowired
    DataSource dataSource;

    @Test
    public void testSql() throws SQLException{
        System.out.println(dataSource.getClass());

        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

springboot之整合druid&配置数据源监控

4-新建application.yml注释掉application.properties

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.3.18:3306/jdbc
    driver-class-name: com.mysql.cj.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
    testOnBrrow: false
    testOnReturn: false
    poolPreparedStatement: true

    #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,slf4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

黄色底色的配置没有生效 

springboot之整合druid&配置数据源监控 5-新建config包DruidConfig类

package com.example.springbootelasticsearch.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DruidConfig {

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

springboot之整合druid&配置数据源监控

6-修改DruidConfig类为

package com.example.springbootelasticsearch.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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 javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

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

    //配置Druid的监控
    //1-配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

        Map<String,String> initParams = new HashMap<>();
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        initParams.put("allow",""); // 为空时,默认允许所有访问
        initParams.put("deny","192.168.3.243");
        bean.setInitParameters(initParams);

        return bean;
    }

    //2-配置一个监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String,String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*"); //排除静态资源和后台/druid/*的所有请求
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));  //filter拦截所有请求

        return bean;
    }

}

可以配置的初始化参数

springboot之整合druid&配置数据源监控 

springboot之整合druid&配置数据源监控

springboot之整合druid&配置数据源监控

springboot之整合druid&配置数据源监控

springboot之整合druid&配置数据源监控

7-运行主类

application.yml中加入,指定项目名

server:
  servlet:
    context-path: /springtest

 访问:
springboot之整合druid&配置数据源监控

输入admin123456

springboot之整合druid&配置数据源监控

8-新建controller包HelloController类

package com.example.springbootelasticsearch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class HelloController {
    
    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @ResponseBody
    @GetMapping("/query")
    public Map<String,Object> map(){
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from department");
        return list.get(0);
    }
}

9-在数据库中新建department表

springboot之整合druid&配置数据源监控 10-访问

springboot之整合druid&配置数据源监控

springboot之整合druid&配置数据源监控

springboot之整合druid&配置数据源监控