Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xuxiaowei-com-cn opened this issue Sep 1, 2023 · 6 comments
Open

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

xuxiaowei-com-cn opened this issue Sep 1, 2023 · 6 comments

Comments

@xuxiaowei-com-cn
Copy link
Contributor

xuxiaowei-com-cn commented Sep 1, 2023

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

@xuxiaowei-com-cn
Copy link
Contributor Author

xuxiaowei-com-cn commented Sep 1, 2023

@xuxiaowei-com-cn
Copy link
Contributor Author

  1. 从下列打包可以看出,执行 mvn clean install 后,原来配置文件中是 [email protected]@.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:[email protected]@.yml
      - optional:nacos:${spring.application.name}[email protected]@.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
Copy link
Contributor Author

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

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

@booniez
Copy link

booniez commented Sep 5, 2023

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

@xuxiaowei-com-cn
Copy link
Contributor Author

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants