Skip to content
Leo的技术分享
Go back

Spring Boot Druid 使用教程

阿里巴巴开源的 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

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

连接池配置:

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

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

运行测试

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

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

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

参考资料


Share this post on:

Previous Post
Spring Cloud 使用 Nacos 作配置中心
Next Post
Spring Boot MyBatis 学习教程