SpringBoot入門教程(十五)集成Druid


Druid是阿里巴巴開源平台上一個數據庫連接池實現,它結合了C3P0、DBCP、PROXOOL等DB池的優點,同時加入了日志監控,可以很好的監控DB池連接和SQL的執行情況,可以說是針對監控而生的DB連接池(據說是目前最好的連接池)。今天主要講在SpringBoot2.0中集成Druid。

在springboot2.0中,幾乎不用修改任何代碼,只需要引入pom.xml,更新application.properties即可輕松完成Druid的搭建。

v修改pom.xml

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

v目錄結構

SpringBoot入門教程(十五)集成Druid

v更新application.properties

# Druid連接池配置,官方配置參考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
# spring.datasource的相關配置在application-dev.properties中,所以這里直接注釋了
#spring.datasource.druid.url=jdbc:mysql://10.168.0.24:3306/dmp_db?useUnicode=true&characterEncoding=utf8
#spring.datasource.druid.username=root
#spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=10
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# Druid WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*

# Druid StatViewServlet配置,說明請參考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

# Redis配置,官方參考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#common-application-properties
#spring.redis.database=0
#spring.redis.host=127.0.0.1
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.timeout=5000

v效果圖

SpringBoot入門教程(十五)集成Druid

SpringBoot入門教程(十五)集成Druid

 

v源碼地址

https://github.com/toutouge/javademosecond/tree/master/hellospringboot


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關於作者:專注於基礎平台的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回復。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角推薦一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM