SpringBoot:数据访问-整合 Druid 配置数据源监控

本文介绍了如何在SpringBoot项目中集成Druid数据库连接池,包括添加依赖、配置属性、设置监控界面和过滤器。还展示了如何在Maven项目中管理和测试Druid功能,以及整合Mybatis的实践。
摘要由CSDN通过智能技术生成
点击查看数据访问demo:LearnSpringBoot06DataJdbc
点击查看更多的SpringBoot教程

简介

Druid Spring Boot Starter 用于帮助你在Spring Boot项目中轻松集成Druid数据库连接池和监控。

一、添加druid-spring-boot-starter依赖

点击查询最新版

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

二、配置属性

application.yml代码

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.0.102:3307/dbjdbc
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j2
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

# mybatis
mybatis:
  # 指定全局配置文件位置
  config-location: classpath:mybatis/mybatis-config.xml
  # 指定sql映射文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml

#    表创建好了,将下面代码注释掉
#  sql:
#    init:
#      username: root
#      password: 123456
#      mode: always
#      schema-locations:
#        - classpath*:sql/schema.sql
#      data-locations:
#        - classpath*:sql/schema-data.sql

# Spring Boot 之五:Spring 属性配置 https://www.jianshu.com/p/cfeae8c1b8dd

# Springboot 2.5.x初始化执行sql文件配置 https://blog.csdn.net/qq_24258617/article/details/120272350

# 查看源码 DataSourceProperties  DataSourceConfiguration

DruidConfig.java代码

package com.example.learnspringboot06datajdbc.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidDataSource(){
        return new DruidDataSource();
    }

    //配置Druid的监控
    //1、配置一个管理后台的Servlet

    // 运行起来之后 打开 http://localhost:8082/druid/login.html
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> initParams = new HashMap<>();

        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        initParams.put("allow","");//默认就是允许所有访问
        initParams.put("deny","192.168.0.104");//设置黑名单
        bean.setInitParameters(initParams);
        return bean;
    }

    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String,String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);

        bean.setUrlPatterns(Arrays.asList("/*"));

        return  bean;
    }
}

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>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>LearnSpringBoot06DataJdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>LearnSpringBoot06DataJdbc</name>
    <description>LearnSpringBoot06DataJdbc</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <!--
            【问题解决】Springboot3.0.3+找不到MySQL驱动
            https://blog.csdn.net/qq_39320261/article/details/129394183

            https://blog.csdn.net/ZHENFENGSHISAN/article/details/130992615
            -->
            <scope>runtime</scope>
        </dependency>
        <!--   引入自定义的数据源 https://mvnrepository.com/artifact/com.alibaba/druid -->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>druid</artifactId>-->
<!--            <version>1.1.22</version>-->
<!--        </dependency>-->

<!--        https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.18</version>
        </dependency>
    <!--
        SpringBoot 整合 durid数据库连接池
        https://blog.csdn.net/qq_45315910/article/details/94736535

         这里 注释掉,不用再添加依赖了,查看pom文件的依赖关系图,已经依赖 slf4j 了
    -->
<!--        <dependency>-->
<!--            <groupId>org.slf4j</groupId>-->
<!--            <artifactId>slf4j-log4j12</artifactId>-->
<!--        </dependency>-->


        <!--使用 log4j2 记录日志
        原文链接:https://blog.csdn.net/m0_71777195/article/details/126053802

        这里 注释掉,不用再添加依赖了,查看pom文件的依赖关系图,已经依赖 slf4j 了
        -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-log4j2</artifactId>-->
<!--        </dependency>-->



        <!--        https://blog.csdn.net/weixin_37799575/article/details/125102566 -->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>druid-spring-boot-starter</artifactId>-->
<!--            <version>1.1.22</version>-->
<!--        </dependency>-->

        <!-- mybatis https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </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>

三、工程结构图

工程:LearnSpringBoot06DataJdbc在这里插入图片描述

数据库
在这里插入图片描述

四、DruidMonitor页面介绍

项目运行成功后,在浏览器里访问http://localhost:8082/druid/login.html
在这里插入图片描述
用户名:admin,密码:123456
在这里插入图片描述

首页
在这里插入图片描述

数据源页面
在这里插入图片描述

SQL监控页面
在这里插入图片描述

SQL防火墙页面
在这里插入图片描述

Web应用页面
在这里插入图片描述

URL监控页面
在这里插入图片描述

Session监控页面
在这里插入图片描述
Spring监控页面
在这里插入图片描述

五、测试结果

在浏览器访问http://localhost:8082/queryDepartment,即查询所有部门
在这里插入图片描述

所有部门数据已查询完毕,来到DruidMonitor 点击URL监控页面
在这里插入图片描述
从上图可以看出相关数据已经发生变化了,例如请求次数、jdbc执行数都增加了

查看SQL监控页面数据
在这里插入图片描述
从上图可以看出相关数据已经发生变化了,例如执行数都增加了

点击查看数据访问demo:LearnSpringBoot06DataJdbc

**提醒: **
LearnSpringBoot06DataJdbc工程里有部分是整合mybatis相关操作实践的,可以正常使用。

欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值