700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > spring boot整合druid以及druid监控

spring boot整合druid以及druid监控

时间:2019-09-01 10:44:38

相关推荐

spring boot整合druid以及druid监控

Druid是Java语言中最好的数据库连接池(我个人知识范围内),并且能够提供强大的监控和扩展功能。

下面来说明如何在 spring Boot 中配置使用druid

1:添加druid和spring boot的依赖

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.1</version></dependency>

2:如果spring-boot-starter-parent你用的版本还是1.3.2那现在要升级了,在后面运行项目的时候就报错了.找不到

java.lang.NoClassDefFoundError: org/springframework/boot/web/servlet/ServletRegistrationBean

改为1.5.2吧.比较新

2:在application.properties中添加最基本的数据源配置文件

#数据库配置文件spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xyy_v2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=truespring.datasource.username=adminspring.datasource.password=adminspring.datasource.driver-class-name=com.mysql.jdbc.Driver#连接池配置信息spring.datasource.initial-size=5 spring.datasource.min-idle=5 spring.datasource.max-active=20 spring.datasource.max-wait=60000 spring.datasource.time-between-eviction-runs-millis=60000 spring.datasource.min-evictable-idle-time-millis=300000 spring.datasource.validation-query=SELECT 1 FROM DUAL spring.datasource.test-while-idle=true spring.datasource.test-on-borrow=false spring.datasource.test-on-return=false spring.datasource.pool-prepared-statements=true spring.datasource.max-pool-prepared-statement-per-connection-size=20 spring.datasource.filters=stat spring.datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3-1:spring boot整合druid的方式也和其它一样,可以分为javabean和配置文件properties的方式

我们先来说说bean的方式吧,直接上代码!

a>配置监控拦截器

package com.xyy.util;import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;import com.alibaba.druid.support.http.WebStatFilter;/**?* druid监控拦截去?* @ClassName: DruidStatFilter ?* @author wangqinghua?* @date 7月24日 上午10:53:40?*/@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",initParams={??? @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源})public class DruidStatFilter extends WebStatFilter {}

b>druid监控视图配置

package com.xyy.util;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import com.alibaba.druid.support.http.StatViewServlet;/*** druid监控视图配置* @ClassName: DruidStatViewServlet * @author wangqinghua* @date 7月24日 上午10:54:27*/@WebServlet(urlPatterns = "/druid/*", initParams={@WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)@WebInitParam(name="loginUsername",value="shanhy"),// 用户名@WebInitParam(name="loginPassword",value="shanhypwd"),// 密码@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能})public class DruidStatViewServlet extends StatViewServlet {private static final long serialVersionUID = 6588499385893299545L;}

3-2:配置文件配置的方式application.properties(具体配置视个人而定)

#druid WebStatFilter监控配置spring.datasource.druid.web-stat-filter.enabled= truespring.datasource.druid.web-stat-filter.url-pattern=/*spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*spring.datasource.druid.web-stat-filter.session-stat-enable=truespring.datasource.druid.web-stat-filter.session-stat-max-count=10spring.datasource.druid.web-stat-filter.principal-session-name=spring.datasource.druid.web-stat-filter.principal-cookie-name=spring.datasource.druid.web-stat-filter.profile-enable=#druid StatViewServlet监控配置spring.datasource.druid.stat-view-servlet.enabled= truespring.datasource.druid.stat-view-servlet.url-pattern= /druid/*spring.datasource.druid.stat-view-servlet.reset-enable=falsespring.datasource.druid.stat-view-servlet.login-username=adminspring.datasource.druid.stat-view-servlet.login-password=adminspring.datasource.druid.stat-view-servlet.allow=192.168.1.110,127.0.0.1spring.datasource.druid.stat-view-servlet.deny=192.168.16.111spring.datasource.druid.aop-patterns=com.xyy.service

上述配置具体含义我就不描述了,英文水平可以或者之前有使用过的,应该很清楚 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0S2zStZ1-1650613623624)(http://static./xheditor/xheditor_emot/default/shy.gif)],我就不装逼了,免得有人打我!

4:点击main主入口,运行成功.浏览器输入:http://localhost:8080/druid,帐号:admin 密码:admin,就会看到如下效果了

好了,大功告成!来点掌声![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-er5cr6UG-1650613623625)(http://static./xheditor/xheditor_emot/default/crazy.gif)]

]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。