美文网首页基于springboot的支付系统实战
springboot基于yaml使用druid线程池并监控

springboot基于yaml使用druid线程池并监控

作者: faymanwang | 来源:发表于2020-12-29 11:49 被阅读0次
    1. 页面效果 http://127.0.0.1:8081/druid/sql.html (ip:端口)
      image.png
    2. 引入依赖,这里版本冲突的情况挺多的。现在最新1.2X了,我这里用的是1.1.10
            <!--继承druid数据源,log4j必须要-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
            <!--要启用监控功能,日志包不能少-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
    1. yml 参数配置
    spring:
      datasource:
        username: 
        password: 
        url: jdbc:mysql://127.0.0.1:3306/pay?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          initial-size: 5 #初始连接数
          max-active: 10 #最大活动连接
          max-wait: 60000 #从池中取连接(没有闲置连接)的最大等待时间,-1表示无限等待
          min-idle: 5 #最小闲置数,小于min-idle连接池会主动创建新的连接
          time-between-eviction-runs-millis: 60000 #清理线程启动的间隔时间,当线程池中没有可用的连接启动清理线程
          min-evictable-idle-time-millis: 300000 #清理线程保持闲置最小时间
          validation-query: SELECT 1  #用于校验连接
          test-on-borrow: false #请求连接时是否校验,比较消耗性能,一般设置false
          test-on-return: false #归还连接时是否校验,比较消耗性能,一般设置false
          test-while-idle: true #清理线程通过validation-query来校验连接是否正常,如果不正常将从连接池中移除
          pool-prepared-statements: true #存储相同逻辑的sql到连接池的缓存中
          filters: stat,wall #监控统计web的statement(sql),以及防sql注入的wall
          # 关闭如上配置,可以采用自定义的filter
          filter:
            stat:
              enabled: true #状态监控-->stat
              db-type: mysql
              log-slow-sql: true  #记录超过指定时间的sql到日志中
              slow-sql-millis: 1000
            wall:
              enabled: true #防火墙-->wall
              db-type: mysql
              config:
                delete-allow: false #禁止删除
                drop-table-allow: false #禁止删除表
          web-stat-filter:
            enabled: true #开启监控uri,默认false
            url-pattern: /* #添加过滤规则
            exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid" #忽略过滤
          stat-view-servlet:
            enabled: true #开启视图管理界面,默认false
            url-pattern: /druid/* #视图管理界面uri
            login-username: admin #账号
            login-password: admin #密码
            allow: 127.0.0.1 #白名单
            deny:  192.168.1.130 #黑名单
    
    1. 此时demo的完整pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.1.RELEASE</version>
        </parent>
        <groupId>com.baoxian</groupId>
        <artifactId>baoxian</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>baoxian</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--测试-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--mybatis-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!--mybatis 和下面版本需要匹配-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!--mapper-->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>2.0.3</version>
            </dependency>
            <!--pagehelper-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <!--继承druid数据源,log4j必须要-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
            <!--要启用监控功能,日志包不能少-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    相关文章

      网友评论

        本文标题:springboot基于yaml使用druid线程池并监控

        本文链接:https://www.haomeiwen.com/subject/cajfoktx.html