在Spring Boot项目中经由过程Druid-Spring-Boot-Starter设置装备摆设Druid数据库毗连池和监控,真的很简单、很优雅。只要对一些需要的属性进行设置,不需要你写一行代码,就可以在项目中开启Druid数据库毗连池和监控功能。
这里我用的数据库是PostgreSQL11、Idea是2018.3社区版、Spring Boot是最新版本2.1.1-RELEASE,若是没有安装它们,可以参考我前面的两篇经验进行安装。
1系统下安装
1系统下安装社区版
一、打开Idea,新建Spring Boot项目。
在建立项目时我勾选了Web、PostgreSQL和JPA三个依靠,项目建立完后,这三个依靠将被主动添加到pom.xml文件中。
二、在pom.xml设置装备摆设文件中添加Druid依靠
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
这里添加的是druid-spring-boot-starter。最新版本1.1.10.
三、src->main->resources->application.properties,打开application.properties属性设置装备摆设文件,进行如下设置:
#数据源设置装备摆设
spring.datasource.druid.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.druid.username = postgres
spring.datasource.druid.password = 12345678
spring.datasource.druid.driver-class-name = org.postgresql.Driver
#毗连池设置装备摆设
spring.datasource.druid.initial-size = 5
spring.datasource.druid.max-active = 20
spring.datasource.druid.min-idle = 5
spring.datasource.druid.max-wait = 60000
spring.datasource.druid.pool-prepared-statements = true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size = 20
spring.datasource.druid.validation-query = SELECT 'x'
spring.datasource.druid.test-on-borrow = false
spring.datasource.druid.test-on-return = false
spring.datasource.druid.test-while-idle = true
spring.datasource.druid.time-between-eviction-runs-millis = 60000
spring.datasource.druid.min-evictable-idle-time-millis = 300000
#监控设置装备摆设
spring.datasource.druid.filters = stat,wall,slf4j
spring.datasource.connectionProperties = druid.stat.mergeSql = true;druid.stat.slowSqlMillis = 5000
#设置DRUID监控登录时的用户名和暗码
spring.datasource.druid.stat-view-servlet.login-username = a
spring.datasource.druid.stat-view-servlet.login-password = a
#JPA设置装备摆设
spring.jpa.properties.hibernate.hbm2ddl.auto = create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true
spring.jpa.show-sql = true
注重:不设置装备摆设JPA的话,编译运行项目标时辰会呈现一点小错误,但对druid数据库毗连池和监控没有什么影响。
四、好了,让我们的项目跑起来!
五、打开浏览器,在地址栏中输入localhsot:8080/druid 回车,呈现登录页面,输入前面我们设置的用户名“a”和暗码“a”即可进入druid的监控页。这里也可以经由过程长途拜候。若是是在虚拟机中运行此项目,你可以用本家儿机上的浏览器,经由过程虚拟机的ip+端标语+拜候路径进行拜候,需要注重的是不要用windows自带的浏览器,windows的安装查抄可能导致你拜候不了监控页面。我用的是谷歌浏览器,在地址栏中输入网址:192.168.153.106:8080/druid来拜候监控页面的。
OK,Druid数据库毗连池和监控当作功设置装备摆设完当作。
0 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!