1.导入坐标
<?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>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hao</groupId>
<artifactId>spring-boot-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jdbc</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<mysql.version>8.0.19</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.在application.ymal中配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
password:
3.编写配置类
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")//与配置文件进行绑定
@Bean
public DruidDataSource druidDataSource(){
return new DruidDataSource();
}
//配置监控页功能
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
}
Spring中内置的有数据库连接池,如果使用Druid连接池,需要将该组件导入到容器中
4.运行访问http://localhost:8080/druid
结果:
接着通过查询数据库查看SQL监控
编写Controller类
/**
* @author:抱着鱼睡觉的喵喵
* @date:2020/12/23
* @description:
*/
@RestController
public class SqlController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/sql")
public String control(){
Long query= jdbcTemplate.queryForObject("select count(*) from admin", Long.class);
return "query";
}
}
*更改配置类,开启监控功能
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DruidDataSource druidDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
//加入监控功能
dataSource.setFilters("stat");
return dataSource;
}
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
}
1.访问http://localhost:8080/druid,点开SQL监控
2.打开另外一个页面访问http://localhost:8080/sql
3.刷新监控页出现如下
开启Web应用监控
在配置类中导入开启Web应用的组件
/**
* @author:抱着鱼睡觉的喵喵
* @date:2020/12/23
* @description:
*/
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DruidDataSource druidDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
//加入监控功能
dataSource.setFilters("stat");
return dataSource;
}
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
/**
* WebStatFilter 用于采集web-jdbc关联监控的数据
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter(){
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> filterFilter=new FilterRegistrationBean<>(webStatFilter);
filterFilter.setUrlPatterns(Arrays.asList("/*"));//拦截所有资源
//以下资源不拦截*.js,*.gif,*.jpg,*.css等
filterFilter.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.css,*.ico,/druid/*");
return filterFilter;
}
}
2.运行测试
访问http://localhost:8080/sql
查看Web应用监控数据
开启防火墙
在filter中添加wall
启动访问http://localhost:8080/druid,点击SQL防火墙
访问http://localhost:8080/sql
再次刷新监控页面
开启druid登录功能
在配置类中增加
运行访问http://localhost:8080/druid
可以在配置文件进行配置
============================================================================================================================================================================================================================================
下面使用ymal配置文件进行配置
导入坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
spring:
datasource:
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
password: hao20001010
druid:
filters: stat,wall
aop-patterns: com.hao.boot.*
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
再次运行
其他具体操作访问官方文档