|
1. 引言
在现代企业级应用开发中,数据库连接池和SQL监控是非常重要的组成部分。Spring Boot作为一个简化了Spring应用开发的框架,广泛应用于各类项目中。Druid是阿里巴巴开源的数据库连接池,提供了强大的监控和管理功能。本文将详细介绍如何在Spring Boot项目中整合Druid,实现SQL监控和数据库密码加密。
2. 环境准备
在开始之前,确保已安装以下环境和工具:
(1) JDK 8或更高版本
(2) Maven 3.2+或Gradle
(3) 一个Spring Boot项目
3. 添加Druid依赖
首先,需要在项目的`pom.xml`中添加Druid的依赖。
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.22</version>
- </dependency>
复制代码
4. 配置Druid数据源
在`application.yml`或`application.properties`中配置Druid数据源。
- application.yml:
- spring:
- datasource:
- druid:
- url: jdbc:mysql://localhost:3306/yourdb
- username: yourusername
- password: yourpassword
- driver-class-name: com.mysql.cj.jdbc.Driver
- initial-size: 5
- max-active: 20
- min-idle: 5
- max-wait: 60000
- filters: stat,wall
- application.properties:
- spring.datasource.druid.url=jdbc:mysql://localhost:3306/yourdb
- spring.datasource.druid.username=yourusername
- spring.datasource.druid.password=yourpassword
- spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.druid.initial-size=5
- spring.datasource.druid.max-active=20
- spring.datasource.druid.min-idle=5
- spring.datasource.druid.max-wait=60000
- spring.datasource.druid.filters=stat,wall
复制代码
5. 配置Druid监控
Druid提供了一个强大的Web监控界面,可以方便地查看SQL执行情况和数据库连接池状态。需要在Spring Boot项目中配置一个`DruidStatViewServlet`。
在项目的`com.example.config`包中创建一个新的配置类`DruidConfig`:
- import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
- import com.alibaba.druid.support.http.WebStatFilter;
- import com.alibaba.druid.support.http.StatViewServlet;
- 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;
- @Configuration
- public class DruidConfig {
- @Bean
- @ConfigurationProperties("spring.datasource.druid")
- public DataSource druidDataSource() {
- return DruidDataSourceBuilder.create().build();
- }
- @Bean
- public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
- ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
- servletRegistrationBean.addInitParameter("loginUsername", "admin");
- servletRegistrationBean.addInitParameter("loginPassword", "admin");
- return servletRegistrationBean;
- }
- @Bean
- public FilterRegistrationBean<WebStatFilter> webStatFilter() {
- FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
- filterRegistrationBean.addUrlPatterns("/*");
- filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
- return filterRegistrationBean;
- }
- }
复制代码
6. 配置数据库密码加密
为了确保数据库密码的安全性,可以使用Druid的加密功能。首先,生成加密后的密码。在项目根目录下运行以下命令:
- java -cp druid-1.1.22.jar com.alibaba.druid.filter.config.ConfigTools yourpassword
复制代码
将生成的加密密码和公钥、私钥保存起来。然后,在`application.yml`或`application.properties`中配置加密后的密码。
- application.yml:
- spring:
- datasource:
- druid:
- url: jdbc:mysql://localhost:3306/yourdb
- username: yourusername
- password: ENC(yourEncryptedPassword)
- driver-class-name: com.mysql.cj.jdbc.Driver
- initial-size: 5
- max-active: 20
- min-idle: 5
- max-wait: 60000
- filters: stat,wall
- connection-properties: config.decrypt=true;config.decrypt.key=yourPublicKey
- application.properties:
- spring.datasource.druid.url=jdbc:mysql://localhost:3306/yourdb
- spring.datasource.druid.username=yourusername
- spring.datasource.druid.password=ENC(yourEncryptedPassword)
- spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.druid.initial-size=5
- spring.datasource.druid.max-active=20
- spring.datasource.druid.min-idle=5
- spring.datasource.druid.max-wait=60000
- spring.datasource.druid.filters=stat,wall
- spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=yourPublicKey
复制代码
7. 结论
通过本文的介绍,我们了解了如何在Spring Boot项目中整合Druid,实现SQL监控和数据库密码加密。Druid的强大功能不仅提升了数据库连接池的性能,还提供了便捷的监控和管理工具。希望本文能够为您的项目提供有价值的参考。
|
|