詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫(kù)
問(wèn)題
mp加密與druid和nacos結(jié)合,首次項(xiàng)目啟動(dòng)成功,后續(xù)訪問(wèn)無(wú)法連接數(shù)據(jù)庫(kù)
導(dǎo)致原因
項(xiàng)目首次加載由于會(huì)去nacos讀取一遍配置,剛好mp啟動(dòng)的時(shí)候也會(huì)去讀取配置好key值,所以啟動(dòng)的時(shí)候不會(huì)報(bào)錯(cuò)
由于nacos有自動(dòng)刷新配置功能,后面自動(dòng)刷新的時(shí)候mp不會(huì)再讀取命令行配置key,導(dǎo)致無(wú)法解密,從而連接數(shù)據(jù)庫(kù)失敗
解決方案
知道原因之后,我們可以修改druid連接數(shù)據(jù)庫(kù)的配置,因?yàn)閐ruid自帶數(shù)據(jù)庫(kù)加解密,參考ConfigFilter類就可以知道,druid會(huì)去讀取外部的配置文件,可以通過(guò)這種方法解決
注意事項(xiàng)
- 由于mp這個(gè)配置的key值只會(huì)讀取一次,通過(guò)SafetyEncryptProcessor這個(gè)類來(lái)解密。后續(xù)是存儲(chǔ)在MapPropertySource這里面,源碼得知
- druid的過(guò)濾器比mp寫的解密還要先執(zhí)行,這個(gè)是重點(diǎn),因?yàn)閗ey只讀取一次,而且過(guò)濾器也只會(huì)執(zhí)行一次,所以不能從mp下手
- 交換bean的注入順序也無(wú)法解決(大概原因是druid過(guò)濾器比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類型的配置文件,這里從啟動(dòng)參數(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ù)庫(kù)連接成功!");
}
/**
* 通過(guò)命令行的密鑰進(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集成后一直刷新配置,頻率很高,這是因?yàn)榘姹締?wèn)題,修改客戶端和服務(wù)端的版本即可
到此這篇關(guān)于詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)MybatisPlus druid連接不上內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識(shí)
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識(shí),需要的朋友可以參考下2017-05-05
Java調(diào)用WebService接口作測(cè)試
這篇文章主要介紹了Java調(diào)用WebService接口作測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Springboot2.0配置JPA多數(shù)據(jù)源連接兩個(gè)mysql數(shù)據(jù)庫(kù)方式
這篇文章主要介紹了Springboot2.0配置JPA多數(shù)據(jù)源連接兩個(gè)mysql數(shù)據(jù)庫(kù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java8 stream sort自定義復(fù)雜排序案例
這篇文章主要介紹了java8 stream sort自定義復(fù)雜排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java關(guān)鍵字instanceof的兩種用法實(shí)例
這篇文章主要介紹了Java關(guān)鍵字instanceof的兩種用法實(shí)例,本文給出了instanceof關(guān)鍵字用于判斷一個(gè)引用類型變量所指向的對(duì)象是否是一個(gè)類(或接口、抽象類、父類)及用于數(shù)組比較,需要的朋友可以參考下2015-03-03
Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能
在前后端分離的Web開(kāi)發(fā)模式中,接口(API)扮演著至關(guān)重要的角色,它是前后端交互的橋梁,創(chuàng)建加法計(jì)算器和用戶登錄功能時(shí),介紹了接口測(cè)試和問(wèn)題解決的一般流程,如使用Postman測(cè)試接口、查看日志、處理緩存問(wèn)題等,確保開(kāi)發(fā)過(guò)程中的高效協(xié)作和問(wèn)題快速定位2024-10-10

