詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫
問題
mp加密與druid和nacos結(jié)合,首次項目啟動成功,后續(xù)訪問無法連接數(shù)據(jù)庫
導(dǎo)致原因
項目首次加載由于會去nacos讀取一遍配置,剛好mp啟動的時候也會去讀取配置好key值,所以啟動的時候不會報錯
由于nacos有自動刷新配置功能,后面自動刷新的時候mp不會再讀取命令行配置key,導(dǎo)致無法解密,從而連接數(shù)據(jù)庫失敗
解決方案
知道原因之后,我們可以修改druid連接數(shù)據(jù)庫的配置,因為druid自帶數(shù)據(jù)庫加解密,參考ConfigFilter類就可以知道,druid會去讀取外部的配置文件,可以通過這種方法解決
注意事項
- 由于mp這個配置的key值只會讀取一次,通過SafetyEncryptProcessor這個類來解密。后續(xù)是存儲在MapPropertySource這里面,源碼得知
- druid的過濾器比mp寫的解密還要先執(zhí)行,這個是重點,因為key只讀取一次,而且過濾器也只會執(zhí)行一次,所以不能從mp下手
- 交換bean的注入順序也無法解決(大概原因是druid過濾器比mp執(zhí)行還要優(yōu)先,此處不知道是否有新的解決方案)
附源碼
package com.hpf.cloud.filter; import com.alibaba.druid.filter.config.ConfigFilter; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.proxy.jdbc.DataSourceProxy; import com.baomidou.mybatisplus.core.toolkit.AES; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.stereotype.Component; import java.sql.SQLException; import java.util.Properties; /** * @description: 兼容druid與mp加解密,重寫druid的解密,替換為mp的解密方式 * @datetime: 2020/11/22 16:12 * @author: huangpengfei */ @NoArgsConstructor @Slf4j @Component public class DruidForMybatisPlusDecryptFilter extends FilterAdapter { /** * 獲取命令行中的key */ @Autowired private ConfigurableEnvironment configurableEnvironment; /** * 重寫獲取密碼解密 * 1. 參考DruidFilter的代碼,取的是file類型的配置文件,這里從啟動參數(shù)中獲取即可 * 2. 如果不配置,且不需要解密則放行 * * @param dataSourceProxy */ @Override public void init(DataSourceProxy dataSourceProxy) { if (!(dataSourceProxy instanceof DruidDataSource)) { log.error("ConfigLoader only support DruidDataSource"); } DruidDataSource dataSource = (DruidDataSource) dataSourceProxy; String mpwKey = null; for (PropertySource<?> ps : configurableEnvironment.getPropertySources()) { if (ps instanceof SimpleCommandLinePropertySource) { SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource) ps; mpwKey = source.getProperty("mpw.key"); break; } } if (null != mpwKey) { // 證明加密 Properties properties = this.setProperties(dataSource, mpwKey); try { // 將信息配置進(jìn)druid DruidDataSourceFactory.config(dataSource, properties); } catch (SQLException e) { e.printStackTrace(); } } log.info("數(shù)據(jù)庫連接成功!"); } /** * 通過命令行的密鑰進(jìn)行解密 * * @param dataSource 數(shù)據(jù)源 * @param mpwKey 解密key * @return */ private Properties setProperties(DruidDataSource dataSource, String mpwKey) { Properties properties = new Properties(); // 先解密 try { String userName = AES.decrypt(dataSource.getUsername().substring(4), mpwKey); properties.setProperty(DruidDataSourceFactory.PROP_USERNAME, userName); dataSource.setUsername(userName); String password = AES.decrypt(dataSource.getPassword().substring(4), mpwKey); properties.setProperty(DruidDataSourceFactory.PROP_PASSWORD, password); dataSource.setPassword(password); String url = AES.decrypt(dataSource.getUrl().substring(4), mpwKey); properties.setProperty(DruidDataSourceFactory.PROP_URL, url); dataSource.setUrl(url); } catch (Exception e) { log.info("druid decrypt failed!"); e.printStackTrace(); } return properties; } }
ps
nacos集成后一直刷新配置,頻率很高,這是因為版本問題,修改客戶端和服務(wù)端的版本即可
到此這篇關(guān)于詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)MybatisPlus druid連接不上內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識,需要的朋友可以參考下2017-05-05Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式
這篇文章主要介紹了Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09java8 stream sort自定義復(fù)雜排序案例
這篇文章主要介紹了java8 stream sort自定義復(fù)雜排序案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java關(guān)鍵字instanceof的兩種用法實例
這篇文章主要介紹了Java關(guān)鍵字instanceof的兩種用法實例,本文給出了instanceof關(guān)鍵字用于判斷一個引用類型變量所指向的對象是否是一個類(或接口、抽象類、父類)及用于數(shù)組比較,需要的朋友可以參考下2015-03-03