spring全家桶學習-整合druid資料庫連線池
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
springboot 2 整合druid
相較於hikaricp 資料來源,druid 主要是提供了豐富的監控以及擴充套件(filter)功能
1:springboot 2 整合 druid ,pom檔案配置
由於springboot2 預設內嵌了 hikaricp ,因此對於整合druid 需要排除掉依賴的hikaricp相關jar(當前操作並非必需)
<!--引入jdbc連線--> <dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> <exclusions> <!--由於使用druid作為連線池,因此最好是排除掉預設連線池 hikaricp--> <exclusion> <artifactId>HikariCP</artifactId> <groupId>com.zaxxer</groupId> </exclusion> </exclusions> </dependency> <!--引入 druid 相關--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.1</version> <!--對於當前版本的使用建議參考druid官方文件--> </dependency>
2:application.properties 配置
# 使用 druid 相關配置,當前以預設整合的H2資料庫為例
spring.datasource.druid.url=jdbc:h2:mem:foo
spring.datasource.druid.username=sa
spring.datasource.druid.password=
druid的另外一個優勢點就是支援對配置檔案中的password 進行加密處理,可以通過配置 密文,在服務內部進行自定義解密操作
關於密碼加密相關操作
https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
# 加密後的密碼 spring.datasource.password=n/z7PyA5cvcXvs8px8FVmBVpaRyNsvJb3X7YfS38DJrIg25EbZaZGvH4aHcnc97Om0islpCAPc3MqsGvsrxVJw== # 資料庫連線時解密配置 spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key} # 開啟config filter spring.datasource.druid.filter.config.enabled=true # 公鑰 public-key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALS8ng1XvgHrdOgm4pxrnUdt3sXtu/E8My9KzX8sXlz+mXRZQCop7NVQLne25pXHtZoDYuMh3bzoGj6v5HvvAQ8CAwEAAQ==
3: 自定義filter
在druid中本身就存在大量的內建filter, 可以檢視 com.alibaba.druid jar檔案中 META-INF 中 關於 druid-filter.properties檔案中的定義
關於druid 1.2.1 jar META-INF/druid-filter.properties 中的屬性配置;
druid.filters.default=com.alibaba.druid.filter.stat.StatFilter
druid.filters.stat=com.alibaba.druid.filter.stat.StatFilter
druid.filters.mergeStat=com.alibaba.druid.filter.stat.MergeStatFilter
druid.filters.counter=com.alibaba.druid.filter.stat.StatFilter
druid.filters.encoding=com.alibaba.druid.filter.encoding.EncodingConvertFilter
druid.filters.log4j=com.alibaba.druid.filter.logging.Log4jFilter
druid.filters.log4j2=com.alibaba.druid.filter.logging.Log4j2Filter
druid.filters.slf4j=com.alibaba.druid.filter.logging.Slf4jLogFilter
druid.filters.commonlogging=com.alibaba.druid.filter.logging.CommonsLogFilter
druid.filters.commonLogging=com.alibaba.druid.filter.logging.CommonsLogFilter
druid.filters.wall=com.alibaba.druid.wall.WallFilter
druid.filters.config=com.alibaba.druid.filter.config.ConfigFilter
druid.filters.haRandomValidator=com.alibaba.druid.pool.ha.selector.RandomDataSourceValidateFilter
可以參考druid.filters.default=com.alibaba.druid.filter.stat.StatFilter 來實現自定義配置;具體操作按照以下流程執行
- 自定義filter類, 繼承com.alibaba.druid.filter.FilterEventAdapter 類,裡面提供了大量擴充套件點, 可以通過選擇性重寫相關方法來實現自定義filter增強
- 在當前專案 resources目錄下 建立 META-INF/druid-filter.properties檔案; 對於檔案中的配置資料為 :druid.filters.自定義filter屬性名稱=自定義filter類全路徑
- 可以通過 spring.datasource.druid.filters=需要開啟的filter名稱
關於filter 相關配置
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter#%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-filter
"talk is cheap,show me the code"
程式碼路徑:https://github.com/mengyan183/spring-family/tree/main/spring-datasource-druid
程式碼中只演示了單資料來源,對於springboot2 版本中 druid配置多資料來源 和 hikaricp配置相同,都是需要自定義bean宣告