Spring Boot2.x 的Druid连接池配置[附带监控]

Posted lywj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot2.x 的Druid连接池配置[附带监控]相关的知识,希望对你有一定的参考价值。

父依赖【Spring Boot 2.1.x版本】

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

 

主要依赖【3个】

<!--Druid 连接池与Spring Boot结合的依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<!--Spring Boot Web 依赖,Druid中的某些组件还是有依赖Web组件的,毕竟监控是需要页面显示的-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql 依赖【8.0+版本】,当然spring boot也可以连接其他关系型数据库,但是此代码不做其他示例-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

 

Spring Boot配置文件配置

#datasource base config
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.url=jdbc:mysql://数据库IP:端口/数据库名称?useUnicode=true&useSSL=false
#annoated driverClassName,have [cj]
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#druid pool standard config
spring.datasource.druid.max-active=30
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-wait=12000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=30000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-open-prepared-statements=30

spring.datasource.druid.validation-query=select 1 from dual
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false

#druid extends config
#druid sql firewall monitor
spring.datasource.druid.filter.wall.enabled=true

#druid sql monitor
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=10000
spring.datasource.druid.filter.stat.merge-sql=true

#druid uri monitor
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*

#druid session monitor
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.profile-enable=true

#druid spring monitor
spring.datasource.druid.aop-patterns=com.xcky.*

#druid login user config
spring.datasource.druid.stat-view-servlet.login-username=root
spring.datasource.druid.stat-view-servlet.login-password=root

 

基本测试前提:

加上一些基本操作数据库操作的controller,
然后访问项目: http://ip:端口/druid
然后输入上面配置的用户和密码root即可访问

附1:本项目原本的pom依赖【含简单测试例子】

<?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 http://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.1.1.RELEASE</version>
    </parent>
    <groupId>com.xcky</groupId>
    <artifactId>shop-druid</artifactId>
    <version>0.1</version>
    <name>shop-druid</name>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

附2:本项目示范代码及代码示例

示例:http://lb-chen.cn:9005/druid 用户和密码都为root

如果监控台没有任何数据,请访问如下地址再刷新控制台即可

以上是关于Spring Boot2.x 的Druid连接池配置[附带监控]的主要内容,如果未能解决你的问题,请参考以下文章

最全Spring Boot2.x系列Config配置集成篇-1参数配置

spring boot2.x发送邮件

Spring Boot2.X 配置文件自动解密读取

Spring Boot2.X中findOne的用法

spring boot2.x和activiti如何配置

Spring Boot2.x + Quartz 定时任务模块