栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解MybatisPlus集成nacos导致druid连接不上数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解MybatisPlus集成nacos导致druid连接不上数据库

问题

mp加密与druid和nacos结合,首次项目启动成功,后续访问无法连接数据库

导致原因

项目首次加载由于会去nacos读取一遍配置,刚好mp启动的时候也会去读取配置好key值,所以启动的时候不会报错
由于nacos有自动刷新配置功能,后面自动刷新的时候mp不会再读取命令行配置key,导致无法解密,从而连接数据库失败

解决方案

知道原因之后,我们可以修改druid连接数据库的配置,因为druid自带数据库加解密,参考ConfigFilter类就可以知道,druid会去读取外部的配置文件,可以通过这种方法解决

注意事项

  • 由于mp这个配置的key值只会读取一次,通过SafetyEncryptProcessor这个类来解密。后续是存储在MapPropertySource这里面,源码得知
  • druid的过滤器比mp写的解密还要先执行,这个是重点,因为key只读取一次,而且过滤器也只会执行一次,所以不能从mp下手
  • 交换bean的注入顺序也无法解决(大概原因是druid过滤器比mp执行还要优先,此处不知道是否有新的解决方案)

附源码

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;


@NoArgsConstructor
@Slf4j
@Component
public class DruidForMybatisPlusDecryptFilter extends FilterAdapter {

  
  @Autowired
  private ConfigurableEnvironment configurableEnvironment;

  
  @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("数据库连接成功!");
  }

  
  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集成后一直刷新配置,频率很高,这是因为版本问题,修改客户端和服务端的版本即可

到此这篇关于详解MybatisPlus集成nacos导致druid连接不上数据库的文章就介绍到这了,更多相关MybatisPlus druid连接不上内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号