当前位置:网站首页>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语句,可以看见其详情
边栏推荐
- I am in Zhongshan. Where can I open an account? Is online account opening safe?
- LeetCode 498. Diagonal traversal
- Record a time when the server was taken to mine
- SQL function
- mysql 数据库字段查询区分大小写设置
- Industrial and enterprise patent matching data (hundreds of thousands of data) 1998-2014
- install realsense2: The following packages have unmet dependencies: libgtk-3-dev
- c语言语法基础之——函数定义学习
- 逻辑英语结构【重点】
- LeetCode 0710.黑名单中的随机数 - 预处理实现O(1)取值
猜你喜欢

Explained: A Style-Based Generator Architecture for GANs (StyleGAN)

Badge series 4: use of circle Ci

c语言语法基础之——指针( 多维数组、函数、总结 ) 学习

MapReduce & yarn theory

Regular expression learning

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

VI summary of common commands

WGCLOUD的web ssh服务端口是多少

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

Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
随机推荐
LeetCode 498. Diagonal traversal
Teach you to use shell script to check whether the server program is running
Daily-used English phrases
Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID
Summary of common commands of vim
install ompl. sh
js---获取对象数组中key值相同的数据,得到一个新的数组
Use recursion or a while loop to get the name of the parent / child hierarchy
[pulsar learning] pulsar Architecture Principle
LeetCode 接雨水系列 42.(一维) 407.(二维)
自动化测试——pytest本身及第三方模块介绍及使用
Common SQL add / delete / modify query statements
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
mysql 数据库字段查询区分大小写设置
自动化测试——关于unitest与pytest初始化共存问题
异常记录-23
十万行事务锁,开了眼界了。
Industrial and enterprise patent matching data (hundreds of thousands of data) 1998-2014
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
Specific implementation comparison between different programming languages