Spring Boot Druid 使用教程

本文介绍在Spring Boot项目中使用阿里巴巴开源的Druid数据库连接池及监控功能。先讲述添加依赖,接着说明Druid配置,涵盖数据库连接、连接池、StatViewServlet和WebStatFilter等配置,最后进行运行测试,可在监控页面查看相关数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阿里巴巴开源的 Druid 是 Java 语言的数据库连接池,提供了强大的监控和扩展功能。

本文讲述如何在 Spring Boot 项目中使用 Druid 数据库连接池。

教程基于文章 《Spring Boot MyBatis 学习教程》 配套的源代码进行扩展,添加 Druid 数据库连接池和监控的功能。如果读者对于 Spring Boot 如何使用 MyBatis 还不了解,可以先完成文章 《Spring Boot MyBatis 学习教程》 的阅读和学习。

添加依赖

pom.xml 文件添加当前最新版本的 Druid 依赖:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.2.4</version>
</dependency>

形成新的 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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>me.leehao</groupId>
    <artifactId>mybatisdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatisdemo</name>
    <description>Demo project for Mybatis</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Druid 配置

application.yml 配置如下:

server:
  port: 6080

spring:
  datasource:
    druid:
      username: root
      password: 12345678
      url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 8
      max-active: 16
      min-idle: 1
      max-wait: 60000

      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"

      stat-view-servlet:
        enabled: true
        login-username: admin
        login-password: admin
        reset-enable: false
        url-pattern: /druid/*

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: me.leehao.mybatisdemo.entity

logging:
  level:
    me:
      leehao:
        mybatisdemo:
          mapper : debug

其中,数据库连接基本配置:

  • username:数据库用户名称
  • password:数据库密码
  • url:JDBC 连接字符串
  • driver-class-name:数据库驱动类名称

连接池配置:

  • initial-size:连接池建立时创建的初始化连接数
  • max-active:连接池中最大的活跃连接数
  • min-idle:连接池中最小的连接数
  • max-wait:获取连接等待超时的时间

Druid 内置一个 StatViewServlet 用于展示 Druid 的统计信息,对 StatViewServlet 的配置,主要是配置 stat-view-servlet,包括:

  • enabled:是否启用 StatViewServlet (监控页面),默认为 false
  • login-username:监控页面登录的用户名称
  • login-password:监控页面登录的用户密码
  • reset-enable:是否允许清空统计数据
  • url-pattern:根据配置中的 url-pattern 来访问内置监控页面,例如 /druid/*,则内置监控页面的首页是 /druid/index.html

Druid 提供 WebStatFilter 用于采集 web-jdbc 关联监控的数据。对 WebStatFilter 进行配置,主要是配置 web-stat-filter,包括:

  • enabled:是否启用 WebStatFilter,默认值 false
  • url-pattern:需要监控的 url
  • exclusions:需要排除一些不必要的 url,比如 *.js,/jslib/*

运行测试

浏览器打开地址:http://localhost:6080/druid/index.html,其中,端口 6080 为上面 application.yml 配置的 server.port。使用 admin 用户和 admin 密码登录 Druid 的监控页面。

随机调用几次 http://localhost:6080/user/1 接口,可以看到监控页面有相关的监控展示:

其他监控数据这里不再阐述,可以参考官方文档。

参考资料

  • https://github.com/alibaba/druid
  • https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter/
  • https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE
  • https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter
  • https://github.com/alibaba/druid/wiki/Druid%E8%BF%9E%E6%8E%A5%E6%B1%A0%E4%BB%8B%E7%BB%8D
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值