druid源代码仓库地址:https://github.com/alibaba/druid
一、通过配置类进行设置
1、pom.xml中添加dependency
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
2、配置数据库连接池(MySQL8)
在application.yml文件中配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/xxx
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
3、配置_StatFilter
@ConfigurationProperties(prefix = "spring.datasource") //获取配置文件中的参数
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
//Druid内置提供一个StatFilter,用于统计监控信息
//也可以在yml文件中配置
druidDataSource.setFilters("stat");
return druidDataSource;
}
4、配置_StatViewServlet
根据配置中的url-pattern来访问内置监控页面,例如:
http://localhost:8080/druid/index.html
@Bean
public ServletRegistrationBean statViewServlet() {
StatViewServlet statViewServlet = new StatViewServlet();
// 内置监控页面的首页是/druid/index.html
ServletRegistrationBean<StatViewServlet> servletServletRegistrationBean = new ServletRegistrationBean<>(statViewServlet,"/druid/*");
//配置监控页面访问密码
servletServletRegistrationBean.addInitParameter("loginUsername","druid");
servletServletRegistrationBean.addInitParameter("loginPassword","druid");
return servletServletRegistrationBean;
}
5、配置WebStatFilter
@Bean
public FilterRegistrationBean webStatFilter() {
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
//排除 *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* 相关文件
webStatFilterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return webStatFilterFilterRegistrationBean;
}
6、配置 wallfilter
在第三步配置_StatFilter中,添加wall
@ConfigurationProperties(prefix = "spring.datasource") //获取配置文件中的参数
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
//Druid内置提供一个StatFilter,用于统计监控信息
//也可以在yml文件中配置
druidDataSource.setFilters("stat,wall");
druidDataSource.setMaxActive(12);
return druidDataSource;
}
所有代码如下:
MyDataSourceConfig类
package com.greenery.admin.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.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.sql.SQLException;
import java.util.Arrays;
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties(prefix = "spring.datasource") //获取配置文件中的参数
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
//Druid内置提供一个StatFilter,用于统计监控信息
//也可以在yml文件中配置
druidDataSource.setFilters("stat,wall");
druidDataSource.setMaxActive(12);
return druidDataSource;
}
/**
* 配置druid的内置监控
*
* Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。
*
* 这个StatViewServlet的用途包括:
* 提供监控信息展示的html页面
* 提供监控信息的JSON API
*/
@Bean
public ServletRegistrationBean statViewServlet() {
StatViewServlet statViewServlet = new StatViewServlet();
// 内置监控页面的首页是/druid/index.html
ServletRegistrationBean<StatViewServlet> servletServletRegistrationBean = new ServletRegistrationBean<>(statViewServlet,"/druid/*");
//配置监控页面访问密码
servletServletRegistrationBean.addInitParameter("loginUsername","druid");
servletServletRegistrationBean.addInitParameter("loginPassword","druid@2023");
return servletServletRegistrationBean;
}
/**
* WebStatFilter用于采集web-jdbc关联监控的数据
*/
@Bean
public FilterRegistrationBean webStatFilter() {
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
//排除 *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* 相关文件
webStatFilterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return webStatFilterFilterRegistrationBean;
}
}
TestController类
@Controller
public class TestController {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/sql")
public String mySql() {
Long aLong = jdbcTemplate.queryForObject("select count(*) from g_user", long.class);
return aLong.toString();
}
}
二、通过yml文件配置
1、pom.xml中加上dependency
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-version}</version>
</dependency>
2、application.yml文件中进行配置
spring:
# 处理druid-1.1.22及以上版本无法监控SpringBean问题
aop:
auto: false
datasource:
url: jdbc:mysql://localhost:3306/xxx
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
filters: stat,wall,slf4j #底层开启功能:sql监控,防火墙,日志
aop-patterns: com.greenery.admin.* #监控SpringBean
stat-view-servlet: #配置监控页
enabled: true
login-username: xxx
login-password: xxx
reset-enable: false
web-stat-filter: #监控web
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
# 详细配置
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
wall:
enabled: true
config:
drop-table-allow: false
标签:spring,数据源,boot,druid,监控,new,druidDataSource,import,public
From: https://www.cnblogs.com/shuimublog/p/17068388.html