目录

1. 添加依赖

2. 配置文件

3. 配置类

4. 启动Druid监控

5. 访问Druid监控页面


在Springboot 中配置连接池来连接数据库是非常普遍的应用。本文主要介绍最常用的MySQL的连接用法,其中主要是使用产品开发中使用的Druid(阿里巴巴开源的)来进行示例演示如何从0到1完成数据库连接池的配置和使用。

在Spring Boot中配置Druid连接池,可以通过以下步骤实现。下面是一个完整的示例,包括pom.xml依赖、application.yml配置和Java代码。

1. 添加依赖

首先,在你的pom.xml文件中添加Druid的依赖:

<dependencies>
    <!-- Spring Boot Starter Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- Druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.8</version>
    </dependency>
    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置文件

application.ymlapplication.properties文件中添加Druid相关的配置。

application.yml

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/yourdatabase
      username: yourusername
      password: yourpassword
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall,log4j
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# Druid监控页面配置
druid:
  stat-view-servlet:
    enabled: true
    url-pattern: /druid/*
    login-username: admin
    login-password: admin
    reset-enable: false
  web-stat-filter:
    enabled: true
    url-pattern: /*
    exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

3. 配置类

创建一个配置类来配置Druid数据源。

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class DruidConfig {

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

4. 启动Druid监控

在你的Spring Boot应用程序中启用Druid监控。

在主类上添加注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan // 启用Druid的监控
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

5. 访问Druid监控页面

启动Spring Boot应用程序后,可以通过http://localhost:8080/druid访问Druid监控页面。默认的用户名和密码在application.yml中配置为admin

通过以上步骤,你已经成功在Spring Boot项目中配置了Druid连接池,并启用了监控功能。Druid不仅提供了强大的连接池功能,还能帮助你监控和优化数据库连接的使用情况。

GitHub 加速计划 / druid / druid
27.83 K
8.56 K
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:1 个月前 )
f77b2f18 - 4 天前
a1536b8c - 7 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐