Nacos简介

问题场景

软件上生产环境,应安全检测要求,nacos只是防火墙隔离,防护力度不够,其中一条要求是对配置文件application.properties中的明文数据库连接信息进行加密,加密前内容:

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8
db.user=root
db.password=123456
Nacos 源码下载

1 克隆Nacos源码(需要切到版本分支)

git clone https://github.com/alibaba/nacos.git

2 下载Source code (zip)包(此链接为2.1.0版本)

https://github.com/alibaba/nacos/archive/refs/tags/2.1.0.zip

IDEA 打开项目,大致结构

在这里插入图片描述

JDK、Maven等配置操作略过
配置启动项

路径

nacos-2.1.0\console\src\main\java\com\alibaba\nacos\Nacos.java

单机启动参数:

-Dnacos.standalone=true 

在这里插入图片描述

配置外接数据库信息

配置文件路径:nacos\console\src\main\resources\application.properties
在这里插入图片描述

编译打包

在这里插入图片描述

本地启动

在这里插入图片描述

web访问

http://localhost:8848/nacos

在这里插入图片描述

数据库配置相关源码位置

nacos\config\src\main\java\com\alibaba\nacos\config\server\service\datasource\ExternalDataSourceProperties.java

在这里插入图片描述

按需设计加解密算法

需要将数据库密文密码解密后,再执行setPassword

在这里插入图片描述

定义解密decrypt()方法, 可根据自身情况,设计相应的算法,如AES;
为了方便查看,将代码拆开打印。且改动代码后,需要重新编译!!!
application.properties中已设置成密文

在这里插入图片描述

启动Nacos, 查看控制台输出

在这里插入图片描述

密文为application.properties配置的内容
明文为Nacos连接的真实的数据库密码内容
服务启动状态successfully,即代表成功,可以访问web页面验证
web页面已加载出外接数据库中的配置信息

在这里插入图片描述

源码打包

命令: mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

在这里插入图片描述

打好后,包路径:nacos\distribution\target

在这里插入图片描述

环境部署,业务验证

部署和加密前差异在于application.properties的配置要改成密文

完成总结(持续更新)

1、打包报错

[ERROR] Failed to execute goal org.apache.rat:apache-rat-plugin:0.12:check (default) on project nacos-config: Too many files with unapproved license: 18 See RAT report in: D:\nacos-2.0.4\config\target\rat.txt -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :nacos-config

解决方案:
本人未处理License添加,选择跳过认证
方法:nacos\pom.xml 中,注释apache-rat-plugin控件组,再重新执行打包命令
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐