5

springboot 项目可以动态获取数据库配置吗?

 1 year ago
source link: https://www.v2ex.com/t/918435
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

springboot 项目可以动态获取数据库配置吗?

  godleon · aokleon · 13 小时 9 分钟前 · 2318 次点击

例 springboot 项目,里面会有 mysql 配置如下:

 spring:
  datasource:
    #数据源基本配置
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:9999/database_v2
    

想问问怎么能把这个配置在上线的时候隐藏掉?或者动态获取、自己能控制最好

第 1 条附言  ·  12 小时 21 分钟前

谢谢大家的建议,我统一补充一下

1.配置中心完全满足,但这是个单体项目,可能里面最多也就不到 10 个接口,如果用配置中心,我还要在项目里加依赖,还要在外部启动注册中心,甚是庞大,目前还是不考虑了

2.环境变量是是不太满足的,我说一个需求场景吧, 现在我有个 springboot 项目 p1 ,部署到云服务器 c1, 这时候我要在我本地不进入到 c1 服务器的情况下,能修改 p1 的 mysql 数据库配置

3.我目前为了保证 db 的安全,我部署到云服务器的时候,是挂载的外面的配置文件,然后用启动命令去读配置,然后再删掉,就感觉太麻烦了

4. #7 楼说的跟我想的差不多,就是用代码配置数据库链接,通过接口调用拿到配置信息,但是这是最没有办法的办法
,因为说白了,你用 Http,别人也调你的接口不还是一样吗,最重要的是不能让进到这个服务器的人看到数据库配置

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK