SpringBoot下配置Druid
什么是Druid:Druid是阿里发开的一套基于database的监控平台,相对于其他监控来说对于中文的支持更亲民..
前言:最近这段时间发现项目整体运行响应速度较慢,打算对系统进行深层次的优化(尤其是sql方面),所以打算内置Druid在系统中,用来监控系统的运行情况.
传统的web项目中配置Druid在其github上已有明确的文档,此处不在赘述,如有需要可以参考:https://github.com/alibaba/druid
下面我们来谈谈在springboot中如何内置Druid.
1.需要依赖的jar包:
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
特别说明一下:如果使用postgresql的朋友请选择:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
目前我发现的是在这个版本下可用,其他版本可能会出现一些奇怪的报错.
2.配置数据库驱动:
@Configuration
public class DataSourceConfig { @ConfigurationProperties(prefix = "spring.datasource")
@Bean("dataSource")
public DataSource druid(){ return new DruidDataSource();
}
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet());
Map<String, String> initParameters=new HashMap<>();
initParameters.put("loginUsername","***");
initParameters.put("loginPassword","***");
initParameters.put("resetEnable","true");
bean.setInitParameters(initParameters);
bean.setUrlMappings(Arrays.asList("/druid/*"));
return bean;
}
@Bean
public FilterRegistrationBean webFileter(){
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
Map<String, String> initParameters=new HashMap<>();
initParameters.put("exclusions","*.js,*.css,*.png,*.jpg,/druid/*");
bean.setInitParameters(initParameters);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
关于参数的一些说明:
loginUsername: 登录监控系统的用户名
loginPassword: 登录监控控系统的密码
resetEnable: 是否允许重置数据
还可以设置的参数包括:
allow: 允许登录的ip地址(不填默认为允许所有)
deny: 拒绝登录的ip地址(拒绝的优先级比同意的优先级高)
3.配置数据库的相关数据:
spring.datasource.driverClassName=
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.maxActive=80
spring.datasource.queryTimeout=3000
spring.datasource.poolPreparedStatements=true
spring.datasource.filters=stat,wall
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druuid.stat.slowSqlMillis=500
spring.datasource.useGlobalDataSourceStat=true
spring.datasource.validationQuery=select 1 from dual
大体上就只需要这几部就搞定啦!当项目启动之后可以访问*/druid/目录来查看监控页面,这只是一个简单的配置,如果想要实现更加完善或者个性化的功能可以参考官网自主改造~
SpringBoot下配置Druid的更多相关文章
- Springboot中配置druid
pom文件信息: <!--引入druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> ...
- SpringBoot下配置FreeMarker配置远程模版
需求产生原因 要求在同一个接口中,根据不同的参数,返回不同的视图结果 所有的视图中的数据基本一致 要求页面能静态化,优化SEO 例如:A接口返回客户的信息 客户A在调用接口时,返回其个性化定制的页面A ...
- Springboot项目配置druid数据库连接池,并监控统计功能
pom.xml配置依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> & ...
- springboot中配置druid允许一次执行多条sql
原文:https://blog.csdn.net/jiangjun0130/article/details/77868578 1:在配置文件中不需要指定wall防火墙filter. 配置如下: spr ...
- springboot下配置多数据源
摘自: http://blog.csdn.net/wangqingqi20005/article/details/52613055
- Java之SpringBoot自定义配置与整合Druid
Java之SpringBoot自定义配置与整合Druid SpringBoot配置文件 优先级 前面SpringBoot基础有提到,关于SpringBoot配置文件可以是properties或者是ya ...
- 配置 Druid 数据源及密码加密-SpringBoot 2.7 实战基础
在SpringBoot中配置 Druid 数据源及密码加密的方法 前文集成 MyBatis Plus,实现了一组增删改查接口.在启动服务时,从控制台中可以看出 Spring Boot 默认使用 Hik ...
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
随机推荐
- opencv:图像轮廓发现
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- java将小写金额转换为大写的工具类
public class Tool { private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分"; ...
- Codeforces Round #624 (Div. 3) C. Perform the Combo(前缀和)
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...
- 【原】docker基础(一)
1.架构 2.说明 Docker daemon( Docker守护进程):Docker daemon是一个运行在宿主机( DOCKER-HOST)的后台进程.可通过 Docker客户端与之通信. Cl ...
- 【游戏体验】Haunted House(鬼屋历险记)
>>>点此处可试玩无敌版<<< 这款游戏可谓是经典,很多人的童年都有过它的陪伴,不妨重拾当年的快乐 个人测评 游戏性 9/10 音乐 7/10 剧情 4/10 总评 ...
- Mac 配置cron
请参考:https://www.cnblogs.com/EasonJim/p/7819635.html 查看 crontab 是否启动 sudo launchctl list | grep cron ...
- django model 高级进阶
十.model高级用法: 10.1 ORM映射: Object Relational Mapping: orm映射的任务: 将list ,object 的操作转化为sql语句,根据对象生成数据表,将查 ...
- Python学习之面向对象进阶
面向对象进阶当然是要谈谈面向对象的三大特性:封装.继承.多态 @property装饰器 python虽然不建议把属性和方法都设为私有的,但是完全暴露给外界也不好,这样,我们给属性赋值的有效性九无法保证 ...
- 「JSOI2015」最小表示
「JSOI2015」最小表示 传送门 很显然的一个结论:一条边 \(u \to v\) 能够被删去,当且仅当至少存在一条其它的路径从 \(u\) 通向 \(v\) . 所以我们就建出正反两张图,对每个 ...
- 普及C组第二题(8.1)
2000. [2015.8.6普及组模拟赛]Leo搭积木(brick) 题目: Leo是一个快乐的火星人,总是能和地球上的OIers玩得很high. 2012到了,Leo又被召回火星了 ...