spring boot 整合druid数据源,设置druid为数据连接池

Druid

Druid是数据库连接池,它能够提供强大的监控和扩展功能。spring Boot默认的数据连接池是hikariCP

方法一:自定义方式整合druid数据源

导入Druid依赖,pom.xml

<!--       druid数据源,数据连接池,需要在配置中声明调用druid数据源才能生效,否则无效 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.17</version>
        </dependency>

配置application.yaml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xiaomissm
    username: root
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver

编写配置类,设置数据源为Druid

@Configuration
public class mydatasourceConfig {


    /**
     *     如果没有配置数据源,也就是数据连接池,会有默认的自动配置的数据源
     *     默认的自动配置是判断容器中没有 DataSource类型的组件才会配,用@ConditionalOnMissingBean({DataSource.class})判断
     *     现在定义为Druid数据源,默认的则不生效
     * @return
     */
    @ConfigurationProperties("spring.datasource") //复用配置文件的数据源配置
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setUrl();
//        druidDataSource.setUsername();
//        druidDataSource.setPassword();
        return druidDataSource;
    }
}

其它配置:配置监控页面、防火墙、web应用等

@Configuration
public class mydatasourceConfig {


    /**
     *     如果没有配置数据源,也就是数据连接池,会有默认的自动配置的数据源
     *     默认的自动配置是判断容器中没有 DataSource类型的组件才会配,用@ConditionalOnMissingBean({DataSource.class})判断
     *     现在定义为Druid数据源,默认的则不生效
     * @return
     */
    @ConfigurationProperties("spring.datasource") //复用配置文件的数据源配置
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
//        "stat"打开Druid的监控统计功能,"stat,wall"加入监控和防火墙功能功能
        druidDataSource.setFilters("stat,wall");

        return druidDataSource;
    }

//    配置druid的监控页功能
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        //监控页账号密码:
        registrationBean.addInitParameter("loginUsername","admin");
        registrationBean.addInitParameter("loginPassword","123456");

        return registrationBean;
    }



    /**
     * WebStatFilter 用于采集web-jdbc关联监控的数据。
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();

        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");

        return filterRegistrationBean;
    }
}

以上配置方式,Druid页面中的Spring监控功能没有设置生效

方法二:druid数据源starter整合方式

方法一全部注释后,开始配置druid数据源starter整合方式
导入druid-spring-boot-starter依赖,pom.xml

<!--        数据访问-druid数据源starter整合方式,无需在配置中声明调用druid数据源才能生效-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

分析自动配置:

扩展配置项 spring.datasource.druid
自动配置类DruidDataSourceAutoConfigure
DruidSpringAopConfiguration.class, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns
DruidStatViewServletConfiguration.class, 监控页的配置。spring.datasource.druid.stat-view-servlet默认开启。
DruidWebStatFilterConfiguration.class,web监控配置。spring.datasource.druid.web-stat-filter默认开启。
DruidFilterConfiguration.class所有Druid的filter的配置:

private static final String FILTER_STAT_PREFIX = "spring.datasource.druid.filter.stat";
private static final String FILTER_CONFIG_PREFIX = "spring.datasource.druid.filter.config";
private static final String FILTER_ENCODING_PREFIX = "spring.datasource.druid.filter.encoding";
private static final String FILTER_SLF4J_PREFIX = "spring.datasource.druid.filter.slf4j";
private static final String FILTER_LOG4J_PREFIX = "spring.datasource.druid.filter.log4j";
private static final String FILTER_LOG4J2_PREFIX = "spring.datasource.druid.filter.log4j2";
private static final String FILTER_COMMONS_LOG_PREFIX = "spring.datasource.druid.filter.commons-log";
private static final String FILTER_WALL_PREFIX = "spring.datasource.druid.filter.wall";

配置application.yaml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xiaomissm
    username: root
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver

    druid:
      aop-patterns: com.lin.boot.*  #监控SpringBean
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet: # 配置监控页功能
        enabled: true    #开启
        login-username: admin
        login-password: admin
        resetEnable: false  #关闭重置启用

      web-stat-filter: # 监控web
        enabled: true  #开启
        urlPattern: /*   #监控的路径
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'   #不监控的路径


      filter:
        stat: # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false

druid连接池访问地址(监控平台):http://localhost:8080/druid/login.html
spring boot 整合druid数据源,设置druid为数据连接池_第1张图片
登录成功,进入页面
spring boot 整合druid数据源,设置druid为数据连接池_第2张图片

原文:点击查看

你可能感兴趣的:(Spring,Boot,spring,boot,java,spring)