进阶SpringBoot之 Druid 数据源

ops/2024/9/24 6:13:25/

Druid 是开源平台上一个数据库连接池实现,具备日志监控功能

Druid 可以很好的监控 DB 连接池和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池

Maven 仓库

pom.xml 文件导入 Durid 的 jar 包

        <!-- Druid依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.23</version></dependency>

application.yml 文件:

spring.datasource.type: com.alibaba.druid.pool.DruidDataSource

指定 DruidDataSource 连接池

spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource

测试类:

java">import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;@SpringBootTest
class JdbcApplicationTests {@AutowiredDataSource dataSource;@Testvoid contextLoads() throws SQLException {//查看默认数据源System.out.println(dataSource.getClass());//获得数据库连接Connection connection = dataSource.getConnection();System.out.println(connection);//关闭connection.close();}
}

运行,查看结果:数据源是 Druid

application.yml 文件:

druid 数据源配置

stat:监控统计、log4j:日志记录、wall:防御 sql 注入

spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource#SpringBoot默认不注入这些属性,需要自己绑定#druid数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#filters配置监控统计拦截#stat:监控统计  log4j:日志记录  wall:防御sql注入filters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

pom.xml 导入 Log4j 依赖:

        <!-- Log4j依赖 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

DruidConfig 配置类:

@ConfigurationProperties(prefix = "spring.datasource")  绑定 yaml

后台监控 ServletRegistrationBean

SpringBoot 内置了 Servlet 容器,所以没有 web.xml,用 ServletRegistrationBean 代替

java">package com.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.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 javax.sql.DataSource;
import java.util.HashMap;@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource") //绑定@Beanpublic DataSource druidDataSource(){return new DruidDataSource();}//后台监控ServletRegistrationBean//SpringBoot内置了Servlet容器,所以没有web.xml,用ServletRegistrationBean代替@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");HashMap<String,String> initParameters = new HashMap<>();//增加配置initParameters.put("loginUsername","admin");initParameters.put("loginPassword","123456");bean.setInitParameters(initParameters); //设置初始化参数return bean;}//filter过滤器@Beanpublic FilterRegistrationBean webStatFilter(){FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new WebStatFilter());HashMap<String,String> initParameters = new HashMap<>();//exclusions排除,不进行统计initParameters.put("exclusions","*.js,*.css,/druid/*");bean.setInitParameters(initParameters);return bean;}
}

http://www.ppmy.cn/ops/99040.html

相关文章

【超实用!】一文搞懂Transformer原理!‍✨

Hey小伙伴们&#xff01;&#x1f44b; 今天要和大家分享一个超酷的技术点&#xff1a;Transformer模型的原理&#xff01;如果你对深度学习和自然语言处理感兴趣&#xff0c;那么这篇文章绝对不容错过&#xff01;&#x1f469;‍&#x1f4bb;✨ &#x1f4da; Transformer是…

flutter 中 ssl 双向证书校验

SSL 证书&#xff1a; 在处理 https 请求的时候&#xff0c;通常可以使用 中间人攻击的方式 获取 https 请求以及响应参数。应为通常我们是 SSL 单向认证&#xff0c;服务器并没有验证我们的客户端的证书。为了防止这种中间人攻击的情况。我么可以通过 ssl 双向认证的方式。即…

设计模式-结构性模式-桥接模式

1.桥接模式定义 桥接模式就是将抽象部分与他的实现部分分离&#xff0c;使他们都可以独立的变化&#xff1b; 桥接模式用一种巧妙地方式处理多层继承存在的问题&#xff0c;用抽象关联来取代传统的多层继承&#xff0c;将类之间的静态继承关系转变为动态的组合关系&#xff0c;…

RocketMQ 如何保证消息不丢失?

RocketMQ 的消息想要确保不丢失&#xff0c;需要生产者、消费者以及 Broker 的共同努力&#xff0c;缺一不可。 生产者&#xff08;Producer&#xff09; 1、发送方式&#xff1a;选择同步发送 同步发送&#xff1a;发送消息后&#xff0c;需要阻塞等待 Broker 确认收到消息…

Docker-制作镜像

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、操作系统的组成&#xff08;一&#xff09;bootfs&#xff08;二&#xff09;rootfs&#xff08;三&#xff09;Liunx操作系统的启动过程&#xff08;1&…

Zustand:让React状态管理更简单、更高效

Zustand 这个单词在德语里是状态的意思&#xff08;发音&#xff1a;促stand&#xff09; 1. 下载zustand npm i zustand 或者 yarn add zustand2.创建一个store import { create } from zustandconst useBearStore create((set) > ({bears: 0,increasePopulation: …

wpf UniformGrid 动态加载数据

在WPF中&#xff0c;如果你想要在UniformGrid内部为每个Model对象放置一个Panel&#xff08;比如StackPanel或Grid&#xff09;&#xff0c;并且这些Panel是通过数据绑定动态生成的&#xff0c;你需要结合使用ItemsControl、DataTemplate以及UniformGrid。但是&#xff0c;由于…

hyperf 协程作用和相关的方法

什么是协程 协程是一种轻量级的线程&#xff0c;由用户代码来调度和管理&#xff0c;而不是由操作系统内核来进行调度&#xff0c;也就是在用户态进行 判断当前是否处于协程环境内 在一些情况下我们希望判断一些当前是否运行于协程环境内&#xff0c; 对于一些兼容协程环境与…