pig-mesh / pig

↥ ↥ ↥ 点击关注更新,基于 Spring Cloud 2023 、Spring Boot 3.3、 OAuth2 的 RBAC 权限管理系统
https://wiki.pig4cloud.com
Apache License 2.0
5.95k stars 961 forks source link

避免使用 @xxx@ 表达式配置项目 #914

Open xuxiaowei-com-cn opened 1 year ago

xuxiaowei-com-cn commented 1 year ago
  1. 如果是固定值,可以使用 @xxx@ 表达式,如:maven artifactId 是 pig-gateway,项目的 spring.application.name 也设置为 pig-gateway,则可以配合:spring.application.name=@artifactId@`
  2. 如果不是固定值,应该使用环境变量来配置,原因如下:
    1. 避免代码、打包产物泄漏敏感信息,如:spring.cloud.nacos.username=@nacos.username@spring.cloud.nacos.password=@nacos.password@ 等等
    2. 一分代码、一份产物处处部署(十二要素宣言:https://12factor.net/zh_cn/ ),通过环境变量来配置。如果配置信息需要发生变化,而 pom.xml 中没有,是重新打包后生成产物部署?还是增加一个非常长的参数?还是修改一下环境变量重启解决此问题?
xuxiaowei-com-cn commented 1 year ago

统一代码配置

https://github.com/pig-mesh/pig/blob/95d0091c3e74fb9d0246724619fc2a1bf0bd71cd/pig-gateway/src/main/resources/application.yml

image

xuxiaowei-com-cn commented 1 year ago

十二要素宣言:https://12factor.net/zh_cn/

https://github.com/pig-mesh/pig/blob/95d0091c3e74fb9d0246724619fc2a1bf0bd71cd/pig-gateway/src/main/resources/application.yml

image

xuxiaowei-com-cn commented 1 year ago
  1. 从下列打包可以看出,执行 mvn clean install 后,原来配置文件中是 application-@profiles.active@.yml,打包后的产物是 application-dev.yml
  2. 也就是说,打包后的产物配置固定了,无法在程序启动时通过 参数 -Pxxx 修改配置了
  3. 只能在打包时指定 参数 -Pxxx,也就是说,一个包可能只能在一个环境中使用,如果配置发生了变化,也只能通过重新打包来解决此问题(启动时,全部使用参数进行指定是不现实的做法)

xuxiaowei@DESKTOP-8E6LUNV MINGW64 /d/IdeaProjects-jihulab.com/mirrors-gitee/log4j/pig (jdk17-dev)
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:                                                    
[INFO]                                                                         
[INFO] pig                                                                [pom]
[INFO] pig-register                                                       [jar]
[INFO] pig-common                                                         [pom]
[INFO] pig-common-core                                                    [jar]
[INFO] pig-gateway                                                        [jar]
[INFO] pig-common-feign                                                   [jar]
[INFO] pig-common-mybatis                                                 [jar]
[INFO] pig-upms                                                           [pom]
[INFO] pig-upms-api                                                       [jar]
[INFO] pig-common-security                                                [jar]
[INFO] pig-common-log                                                     [jar]
[INFO] pig-auth                                                           [jar]
[INFO] pig-common-oss                                                     [jar]
[INFO] pig-common-swagger                                                 [jar]
[INFO] pig-common-xss                                                     [jar]
[INFO] pig-upms-biz                                                       [jar]
[INFO] pig-common-bom                                                     [pom]
[INFO] pig-common-datasource                                              [jar]
[INFO] pig-common-job                                                     [jar]
[INFO] pig-common-seata                                                   [jar]
[INFO] pig-visual                                                         [pom]
[INFO] pig-codegen                                                        [jar]
[INFO] pig-monitor                                                        [jar]
[INFO] pig-quartz                                                         [jar]
[INFO]
[INFO] -------------------------< com.pig4cloud:pig >--------------------------
[INFO] Building pig 3.7.1                                                [1/24]

******

[INFO] Reactor Summary for pig 3.7.1:
[INFO]
[INFO] pig ................................................ SUCCESS [  0.442 s]
[INFO] pig-register ....................................... SUCCESS [ 13.649 s]
[INFO] pig-common ......................................... SUCCESS [  0.030 s]
[INFO] pig-common-core .................................... SUCCESS [  3.235 s]
[INFO] pig-gateway ........................................ SUCCESS [  3.748 s]
[INFO] pig-common-feign ................................... SUCCESS [  1.440 s]
[INFO] pig-common-mybatis ................................. SUCCESS [  1.348 s]
[INFO] pig-upms ........................................... SUCCESS [  0.025 s]
[INFO] pig-upms-api ....................................... SUCCESS [  2.419 s]
[INFO] pig-common-security ................................ SUCCESS [  2.406 s]
[INFO] pig-common-log ..................................... SUCCESS [  1.427 s]
[INFO] pig-auth ........................................... SUCCESS [  2.966 s]
[INFO] pig-common-bom ..................................... SUCCESS [  0.022 s]
[INFO] pig-common-datasource .............................. SUCCESS [  0.869 s]
[INFO] pig-common-job ..................................... SUCCESS [  0.906 s]
[INFO] pig-common-seata ................................... SUCCESS [  0.874 s]
[INFO] pig-visual ......................................... SUCCESS [  0.021 s]
[INFO] pig-codegen ........................................ SUCCESS [  3.004 s]
[INFO] pig-monitor ........................................ SUCCESS [  1.229 s]
[INFO] pig-quartz ......................................... SUCCESS [  2.619 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  50.726 s
[INFO] Finished at: 2023-09-01T10:15:41+08:00
[INFO] ------------------------------------------------------------------------

xuxiaowei@DESKTOP-8E6LUNV MINGW64 /d/IdeaProjects-jihulab.com/mirrors-gitee/log4j/pig (jdk17-dev)
$ cat pig-gateway/src/main/resources/application.yml
server:     
  port: 9999

spring:     
  application:
    name: @artifactId@
  cloud:
    nacos:
      username: @nacos.username@
      password: @nacos.password@
      discovery:
        server-addr: ${NACOS_HOST:pig-register}:${NACOS_PORT:8848}
        watch:
          enabled: true
        watch-delay: 1000
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
  config:
    import:
      - optional:nacos:application-@profiles.active@.yml
      - optional:nacos:${spring.application.name}-@profiles.active@.yml

xuxiaowei@DESKTOP-8E6LUNV MINGW64 /d/IdeaProjects-jihulab.com/mirrors-gitee/log4j/pig (jdk17-dev)
$ cat pig-gateway/target/classes/application.yml 
server:
  port: 9999

spring:
  application:
    name: pig-gateway
  cloud:
    nacos:
      username: nacos
      password: nacos
      discovery:
        server-addr: ${NACOS_HOST:pig-register}:${NACOS_PORT:8848}
        watch:
          enabled: true
        watch-delay: 1000
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
  config:
    import:
      - optional:nacos:application-dev.yml
      - optional:nacos:${spring.application.name}-dev.yml

xuxiaowei@DESKTOP-8E6LUNV MINGW64 /d/IdeaProjects-jihulab.com/mirrors-gitee/log4j/pig (jdk17-dev)
$
xuxiaowei-com-cn commented 1 year ago

测试环境已通过,正式环境需要重新打包,如何保证正式环境的打包产物与测试环境相同?

  1. Maven 快照随时能修改、删除:在测试打包后,正式打包前,Maven 快照可能发生变化
  2. npm 包随时能修改、删除:在测试打包后,正式打包前,npm 包发生了变化
  3. docker 镜像随时能修改、删除:在测试打包后,正式打包前,docker 镜像发生了变化
  4. ......
booniez commented 1 year ago

给 nacos 指定一个 namespace,从环境变量中去读取,就可以实现一份代码多端部署了吧。配置都放到了 nacos ,都是在 nacos 操作的。

xuxiaowei-com-cn commented 1 year ago

给 nacos 指定一个 namespace,从环境变量中去读取,就可以实现一份代码多端部署了吧。配置都放到了 nacos ,都是在 nacos 操作的。

理想状态下,所有“打包产物中的配置”,都可以使用环境变量进行修改