SpringBoot2.x 整合Druid详细步骤

druid-spring-boot-starter模式

这种方式比较简单,不需要自己写配置类。

在导入依赖以及配置好配置文件yml即可。

pom.xml



    com.alibaba
    druid-spring-boot-starter
    1.1.10

application.yml

spring:
  datasource:
    url: jdbc:mysql://106.13.160.204:3306/jdbc_test
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 8
      min-idle: 1
      max-active: 20
      max-wait: 60000
      time-between-eviction-runsMillis: 60000
      min-evictable-idle-timeMillis: 300000
      validation-query: select 'x' FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      use-global-data-source-stat: true

手动配置配置

pom.xml

导入的依赖druid-spring-boot-starter模式不同


    com.alibaba
    druid
    1.1.10

application.xml

自己测试的时候发现一个小坑,手动配置的yml格式和druid-spring-boot-starter模式略有不同,没有druid:

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

    initial-size: 8
    min-idle: 1
    max-active: 20
    max-wait: 60000
    time-between-eviction-runsMillis: 60000
    min-evictable-idle-timeMillis: 300000
    validation-query: select 'x' FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    pool-prepared-statements: true
    max-open-prepared-statements: 20
    max-pool-prepared-statement-per-connection-size: 20
    filters: stat
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true

配置类的写法SpringBoot1.5和2.x有点小区别

//导入druid数据源
@Configuration
public class DruidConfig {

    //把yml中的配置生效
    @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/*");
		/*Springboot 1.5 的写法*/
        //Map initParams = new HashMap<>();  
        //initParams.put("loginUsername","admin");
        //initParams.put("loginPassword","123456");
        //initParams.put("allow","");
        //initParams.put("deny","192.168.15.21");
		//bean.setInitParameters(initParams);
		
        /*Springboot 2.1.9不需要再建HashMap*/
        bean.addInitParameter("loginUsername","admin");
        bean.addInitParameter("loginPassword","123456");
        bean.addInitParameter("allow",""); //默认就是允许所有访问
        bean.addInitParameter("deny","192.168.15.21");
        return bean;
    }


    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
		/*Springboot 1.5 的写法*/
        //Map initParams = new HashMap<>();
        //initParams.put("exclusions","*.js,*.css,/druid/*");
        //bean.setInitParameters(initParams);

        bean.addInitParameter("exclusions","*.js,*.css,/druid/*");
        bean.setUrlPatterns(Arrays.asList("/*"));

        return  bean;
    }
}

最后访问网址-> http://localhost:8080/druid/login.html

SpringBoot2.x 整合Druid详细步骤_第1张图片

你可能感兴趣的:(JAVA)