当前位置:网站首页>druid数据源实现后台监控
druid数据源实现后台监控
2022-06-26 09:34:00 【马可爱家的马可爱】
1、在pom.xml中整合德鲁伊数据源
<!--德鲁伊数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
2、在application.yml中配置连接数据库
spring:
datasource:
username: root
password: 123456
#假如时区报错了,就增加一个时区配置即可
url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定数据源的类型,spring默认的数据源是hikari,指定我们自己引入的德鲁伊数据源
type: com.alibaba.druid.pool.DruidDataSource
filters: stat,wall,log4j
# 日志监控功能,配置监控统计的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
server:
port: 8001
3、配置druid数据源最强大的功能-后台监控功能
(1)、定义config文件夹下的DruidConfig.java文件
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource") /*与application.yaml绑定*/
@Bean
public DataSource druidDataSource() {
return new DruidDataSource();
}
@Bean
/*后台监控功能 web.xml*/
// 因为springboot内置了servlet容器,所以没有web.xml
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); //获取后台监控
//后台需要有人登陆查看,配置账号、密码
Map<String, String> map = new HashMap<>();
map.put("loginUsername","ml"); //登陆的key是固定的
map.put("loginPassword","123456");
/*允许谁可以访问,若参数为空,则表明任何人都可以访问*/
map.put("allow","");
/*禁止谁访问*/
map.put("mm","192.168.12.3"); //禁止ip地址访问配置
bean.setInitParameters(map); //设置初始化参数
return bean;
}
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
/*可以过滤掉哪些请求*/
Map<String, String> map = new HashMap<>();
//这些东西不进行统计
map.put("exclusions","*.js,*.css,/druid/*");
return filterRegistrationBean;
}
}
4、访问http://localhost:8001/druid
(1)、首页如下
(2)、访问页面如下
(3)、使用http://localhost:8001/add实现下面后台中的代码
@GetMapping("/add")
public String add() {
String sql = "insert into student (sid,sname,sclass,tid) values('12344555','小明','1602班','12356987')";
jdbcTemplate.update(sql);
return "add ok!";
}
然后在点击“sql监控”可以看见刚才执行的sql语句,实现sql监控功能
点击执行的sql语句,可以看见其详情
边栏推荐
- 2021-11-29 quintic polynomial of trajectory planning
- Specific meaning of go bootstrap
- c语言语法基础之——指针(字符、一维数组) 学习
- 深度学习(初识tensorflow2.版本)之三好学生成绩问题(1)
- Solve Django's if Version (1, 3, 3): raise improverlyconfigured ('mysqlclient 1.3.3 or new is required
- 自动化测试——pytest框架介绍及示例
- MySQL单表500万条数据增、删、改、查速度测试
- Curriculum learning (CL)
- Comparison of similar PMS in QPM
- Flutter's brain map notes are easy to find and search!
猜你喜欢
How to correctly open the USB debugging and complete log functions of Huawei mobile phones?

Specific implementation comparison between different programming languages

install opencv-contrib-dev to use aruco code

Badge series 4: use of circle Ci

online trajectory generation

Record a time when the server was taken to mine

Redis novice introduction

This new change of go 1.16 needs to be adapted: the changes of go get and go install

Regular expression learning

测试须知——常见接口协议解析
随机推荐
halcon 光度立体
Thinkphp5 manual error reporting
SQL query duplicate record
Code statistics tools cloc and SCC
LeetCode 958. 二叉树的完全性校验
Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID
Jz2440--- using uboot burning program
How does flutter transfer parameters to the next page when switching pages?
pcl install
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
Jupyter Notebook遇到的问题
软件测试---如何选择合适的正交表
PHP does not allow images to be uploaded together with data (no longer uploading images before uploading data)
[open5gs] open5gs installation configuration
Specific implementation comparison between different programming languages
【CVPR 2021】DatasetGAN: Efficient Labeled Data Factory with Minimal Human Effort
LeetCode 0710.黑名单中的随机数 - 预处理实现O(1)取值
LeetCode 498. Diagonal traversal
Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
深度学习(初识tensorflow2.版本)之三好学生成绩问题(1)