詳解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 {
// 將信息配置進druid
DruidDataSourceFactory.config(dataSource, properties);
} catch (SQLException e) {
e.printStackTrace();
}
}
log.info("數(shù)據(jù)庫連接成功!");
}
/**
* 通過命令行的密鑰進行解密
*
* @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)文章
SpringBoot整合數(shù)據(jù)庫訪問層的實戰(zhàn)
本文主要介紹了SpringBoot整合數(shù)據(jù)庫訪問層的實戰(zhàn),主要包含JdbcTemplate和mybatis框架的整合應(yīng)用,具有一定的參考價值,感興趣的可以了解一下2022-03-03
Java Thread之Sleep()使用方法總結(jié)
這篇文章主要介紹了Java Thread之Sleep()使用方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09

