druid-spring-boot-starter 填坑
此处说的坑并不是指 druid 或者 druid-spring-boot-starter 有多少坑,而是网上的文章有点 out,按照其集成,你会遇到一堆坑。坑,特指这个!主要有监控界面不显示,登录,以及解决 mysql 重启后的异常问题。
spring boot 集成 druid
首先是如何集成,这个问题现在很简单,毕竟有 druid-spring-boot-starter 这个项目了,你只需要在 pom.xml 里新增:
至于这里的版本号,可以从 这里 查询:https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter 目前最新是 1.1.21 配置 druid 只需要将下面的内容复制到你的 application.properties 文件里,你也可以新增个 application-druid.properties 文件,并写入到 application-druid.properties 文件里,之后在 application.properties 文件里 新增:spring.profiles.include=druid 即可,如果你的 application.properties 文件里已经有 spring.profiles.include 配置项,则用 逗号分割开即可,比如:spring.profiles.include=druid,default druid 的配置如下:
|
|
这里,spring.datasource.druid.test-on-borrow=true 我个人建议是 true,否则 mysql 重启后 不会自动重连,不要被下面两行误导了:
在我个人的测试中,如果 mysql 重启,然后无论是等待 30 多秒 还是 3 分钟多,同样会报:
The last packet successfully received from the server was 137,509 milliseconds ago. The last packet sent successfully to the server was 137,510 milliseconds ago
只有将 spring.datasource.druid.test-on-borrow=false 改为 spring.datasource.druid.test-on-borrow=true 才能解决这个问题,至于设置为 true 会有性能影响,实际微乎其微,可能就 1-2 ms 的样子,甚至更短,但却保证了 100% 的可用。
druid console 404
如果用网上的教程,那么,很多文章都没有提到一点:
这个值,曾经默认为 true,但是现在默认为 false,如果你不设置,那么将无法打开 /druid 页面,显示 404!
druid Sorry, you are not permitted to view this page.
新增一行:
spring.datasource.druid.stat-view-servlet.allow=
这时候所有 ip 都可以访问,但是你可以设置登录密码:
目前,我遇到的坑就这么多,后面如果有,再补充。