6

阿里druid-spring-boot-starter 配置,个人整理以及遇到的问题(防止之后找不到) -...

 2 years ago
source link: https://www.cnblogs.com/haoyangyouli/p/16652444.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

简介,什么是Druid

Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和SQL解析器组成。该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证、统计SQL信息、SQL性能收集、SQL注入检查、SQL翻译等,程序员可以通过定制来实现自己需要的功能。

1、Druid官方github地址(这个可以多看看)

https://github.com/alibaba/druid

2、需要导入一些依赖

Druid数据池依赖

<!--        数据库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>
<!--        数据库日志实现-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--SpringBoot 的aop 模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

3、Druid配置信息

现版本其实有大量的默认值都已经配置好了,可以自己去看看源码

(配置信息多,用yaml)

(对于Druid配置,只需要选用自己需要的就行了,我这里是写的一个总结,后面自己有需要其他还会加)

spring:
#数据库配置项
# JDBC 配置(驱动类自动从url的mysql识别,数据源类型自动识别)(yaml相对来说配置信息更加好观看)
  datasource:
    druid:
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://localhost:3306/crm
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: 2003

#    连接池配置信息(通常来说,只需要修改initialSize、minIdle、maxActive
#     如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。removeabandoned不建议在生产环境中打开如果用SQL Server,建议追加配置)
  #     初始化大小
      initial-size: 5
      min-idle: 5
      max-active: 20
  #     配置获取连接等待超时的时间
      max-wait: 60000
  #     配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
  #     配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
  #     申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性
      test-while-idle: true
  #     获取连接时执行检测,建议关闭,影响性能
      test-on-borrow: false
  #     是否开启PSCache,PSCahce对支持游标的数据库性能提示巨大,oracle建议开启,mysql下建议关闭
      pool-prepared-statements: false
  #     开启poolPreparedStatments后生效
      max-pool-prepared-statement-per-connection-size: 20

      #     配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall’用于防火墙·,log4j2:日志
      filters: stat,wall,log4j2

      # Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
      # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
      aop-patterns: 'com.haoyang.webadmin.*'

        #     配置监控属性: 在druid-starter的: com.alibaba.druid.spring.boot.autoconfigure.stat包下进行的逻辑配置
      web-stat-filter: # WebStatFilter配置,
        enabled: true #默认为false,表示不使用WebStatFilter配置,就是属性名去短线
        url-pattern: /* #拦截该项目下的一切请求
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' #对这些请求放行
        session-stat-enable: true
        principal-session-name: session_name
        principal-cookie-name: cookie_name
  #        profile-enable:
        #        session-stat-max-count:

      stat-view-servlet: # StatViewServlet配置
        enabled: true  #默认为false,表示不使用StatViewServlet配置,就是属性名去短线
        url-pattern: /druid/*  #配置DruidStatViewServlet的访问地址。后台监控页面的访问地址
        reset-enable: false #禁用HTML页面上的“重置”功能,会把所有监控的数据全部清空,一般不使用
        login-username: admin #监控页面登录的用户名
        login-password: 2003 #监控页面登录的密码
#       StatViewSerlvet展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数
#       deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问
#       配置的格式
#        <IP>
#        或者<IP>/<SUB_NET_MASK_size>其中128.242.127.1/24
#        24表示,前面24位是子网掩码,比对的时候,前面24位相同就匹配,不支持IPV6。
        allow: 127.0.0.1,192.168.2.111  #IP白名单(没有配置或者为空,则允许所有访问)。允许谁访问druid后台,默认允许全部用户访问。
        deny:  #IP黑名单 (存在共同时,deny优先于allow)。不允许谁访问druid后台

      filter:
        stat:
          enabled: true         # 开启DruidDataSource状态监控
          db-type: mysql        # 数据库的类型
          log-slow-sql: true    # 开启慢SQL记录功能
          slow-sql-millis: 2000 # 默认3000毫秒,这里超过2s,就是慢,记录到日志
        wall:
          enabled: true
          config:
            drop-table-allow: false

以前学习并没有仔细观看,现在回来看一遍发现了一些问题(后续继续添加,目前学的不深)

1、配置问题

一开始把所有的数据库连接池配置信息全部写进spring.datasource.druid下,这样子

spring:
  datasource:
    druid:
      filters: stat,wall,log4j2
        #     初始化大小
      initial-szie: 5
      min-idle: 5
      max-active: 20

然后写了一个测试类

测试类

@SpringBootTest
class ApplicationTests {
    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        DruidDataSource druidDataSource = (DruidDataSource) dataSource;
        System.out.println(druidDataSource.getMaxActive());
        System.out.println(druidDataSource.getInitialSize());
        System.out.println(druidDataSource.getMinIdle());
        //关闭连接
        connection.close();
    }
}

​ 然后经过了一些列断点,源码查看之后,发现spring.datasource.druid配置并没有生效,发现是支持监控配置,但连接池配置并没有写进去

这里看了一下官方的SpringBoot配置示例:

https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

最后发现我自己写了一个自定义注入类,然后注入的并不是spring.datasource.druid下的,而是spring.datasource下的,所以会报错,如果并没有写这个类也没事,SpringBoot会自动注入

​/**
 *       将自定义的Druid数据源添加到容器中,不再让Spring Boot 自动创建
 *       这样做的目的是: 绑定全局配置文件中的druid 数据源属性注入到 com.alibaba.druid.pool.DruidDataSource
 *       从而让它们生效
 *
 *       @ConfigurationProperties(prefix = "spring.datasource"):
 *         作用就是将 全局配置文件中前缀为Spring.dataSource的属性值注入到com.alibaba.druid.pool.DruidDataSource的同名参数中
 */

@Configuration
public class DruidConfig {
    /**
     *     将所有前缀为spring.datasource下的配置项都加载DataSource中
     */

//@ConfigurationProperties("spring.datasource"),这样注入,无法将druid配置文件下的连接池配置自动注入,所以我加一个.druid,这样就是将spring.datasource.druid的属性值注入到com.alibaba.druid.pool.DruidDataSource的同名参数中,而不是spring.datasource

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

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK