当前位置:网站首页>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语句,可以看见其详情
边栏推荐
- Comparison of similar PMS in QPM
- LeetCode 0710.黑名单中的随机数 - 预处理实现O(1)取值
- GAN Inversion: A Survey
- Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
- Collection object replication
- 我在中山,到哪里开户比较好?在线开户安全么?
- thinkphp6.0的第三方扩展包,支持上传阿里云,七牛云
- #云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service
- LeetCode 基本计算器 224. 227. follow up 394
- Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
猜你喜欢

Several connection query methods of SQL (internal connection, external connection, full connection and joint query)

#云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service

Flutter's brain map notes are easy to find and search!

logback
![[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network](/img/a9/1361df052f0474e5c50b948a92c42a.jpg)
[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network

What you need to know to test -- URL, weak network, interface, automation

Single sign on logic

WGCLOUD的web ssh服务端口是多少

Specific implementation comparison between different programming languages

Badge series 8: generate a personalized Badge
随机推荐
Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
点击遮罩层关闭弹窗
Does the go compiled executable have dynamic library links?
进入页面输入框自动获取焦点
logback
Flutter's brain map notes are easy to find and search!
Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
2021-11-29 轨迹规划五次多项式
Click the mask layer to close the pop-up window
install ompl. sh
Install new version cmake & swig & tinyspline
MySQL单表500万条数据增、删、改、查速度测试
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
自动化测试——关于unitest与pytest初始化共存问题
LeetCode 958. Completeness checking of binary tree
Badge collection 6:api\_ Use of level
Logical English structure [key points]
Code statistics tools cloc and SCC
LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改