简体   繁体   English

通过 Spring 或通过 Maven/Gradle 等构建工具的配置文件有什么区别

[英]What is the difference between Profiles via Spring or via a build tool like Maven/Gradle

We are currently developing a Spring Boot based application and we have a QA and a Prod environment to deploy.我们目前正在开发基于 Spring 引导的应用程序,我们有一个 QA 和一个 Prod 环境要部署。 I suggested that having different app properties file for different environments and then using -Dspring.profiles.active in a Docker compose file would be a good solution for profiling.我建议为不同的环境使用不同的应用程序属性文件,然后在 Docker 撰写文件中使用 -Dspring.profiles.active 将是一个很好的分析解决方案。

But, my colleague says that having a build tool based profiling would be a better idea.但是,我的同事说,拥有基于构建工具的分析会是一个更好的主意。 Something like this - https://mkyong.com/maven/maven-profiles-example/像这样的东西 - https://mkyong.com/maven/maven-profiles-example/

Please help me understand the advantages and disadvantages between these two ways as I am not finding sufficient reading materials online.请帮助我了解这两种方式之间的优缺点,因为我在网上找不到足够的阅读材料。

To compare Spring and Mvn/Gradle is to compare 2 tools designed for different things.比较 Spring 和 Mvn/Gradle 就是比较为不同事物设计的 2 个工具。

Both however, allow you to feed configuration through different mechanics.但是,两者都允许您通过不同的机制提供配置。 Spring profiles are intended to be used for deploying applications in different environments, so in terms of use-case and intended use, what you described fits in to this. Spring 配置文件旨在用于在不同环境中部署应用程序,因此就用例和预期用途而言,您所描述的内容符合这一点。 (Ref: https://docs.spring.io/spring-boot/docs/1.2.0.M1/reference/html/boot-features-profiles.html ) (参考: https://docs.spring.io/spring-boot/docs/1.2.0.M1/reference/html/boot-features-profiles.ZFC35FDC70D5FC69D259883A822C7

Here's a couple of advantages of using Spring profiles over a build tool's config I thought of:这是使用 Spring 配置文件而不是我想到的构建工具配置的几个优点:

  • Can switch profile without rebuilding the application可以在不重建应用程序的情况下切换配置文件
  • Can debug the application itself rather than looking at the build config, which should be easier可以调试应用程序本身而不是查看构建配置,这应该更容易

I think the best approach to change configuration based on environment is to use an external config server where the service will connect at startup and fetch the configuration that applies to the environment where the app is deploying.我认为根据环境更改配置的最佳方法是使用外部配置服务器,服务将在启动时连接并获取适用于应用程序部署环境的配置。

For example, you can use spring-cloud-config server and create different configuration files for each environment and when the service deploys, use spring profiles to know what is the environment in which the application is being deployed and get the correct configuration:例如,您可以使用 spring-cloud-config 服务器并为每个环境创建不同的配置文件,并且在部署服务时,使用 spring 配置文件来了解正在部署应用程序的环境是什么并获得正确的配置:

https://cloud.spring.io/spring-cloud-config/reference/html/ https://cloud.spring.io/spring-cloud-config/reference/html/

With Mvn/Gradle profiles, the profile is selected at build time.使用 Mvn/Gradle 配置文件,配置文件是在构建时选择的。 So you will need to rebuild your project for Prod after it's accepted on QA.因此,在 QA 接受后,您需要为 Prod 重建项目。

With Spring, the profile is selected at runtime.对于 Spring,在运行时选择配置文件。 You can deploy the same WAR file or Docker image on QA or on Prod, but you only need to change the runtime profile, eg via the command line -Dspring.profiles.active=... or via an environment variable SPRING_PROFILES_ACTIVE=..., and Spring will select either application-qa.properties or application-prod.properties during startup.您可以在 QA 或 Prod 上部署相同的 WAR 文件或 Docker 映像,但您只需更改运行时配置文件,例如通过命令行 -Dspring.profiles.active=... 或通过环境变量 SPRING_PROFILES_ACTIVE=.. ., 和 Spring 在启动期间将 select application-qa.properties 或 application-prod.properties。

Having the same Docker image on Prod or QA is definitely better, I think.我认为在 Prod 或 QA 上拥有相同的 Docker 图像肯定更好。 The downside of using Spring profiles that you include the property sets for both QA and Prod inside the image, and when you want to change either one, you will have to rebuild the whole Docker image.使用 Spring 配置文件的缺点是您在映像中包含 QA 和 Prod 的属性集,当您想要更改其中任何一个时,您必须重建整个 Docker 映像。

A third option is not to use profiles at all, but externalize all properties that are not the same for QA or Prod.第三种选择是根本不使用配置文件,而是将所有与 QA 或 Prod 不同的属性外部化。 For example, set spring.datasource.url, username, password, etc, via a deploy descriptor rather than include it in your docker image.例如,通过部署描述符设置 spring.datasource.url、用户名、密码等,而不是将其包含在 docker 映像中。 This way, you can reuse the same Docker image for any environment, and reconfigure it on the fly.这样,您可以在任何环境中重复使用相同的 Docker 映像,并即时重新配置它。 Additionally, you can keep production passwords secret from developers if you want.此外,如果需要,您可以对开发人员保密生产密码。

What @JArgente suggest goes even a step further: read the properties from an external service at startup time. @JArgente 的建议更进一步:在启动时从外部服务读取属性。 But that may be overkill, if you can do it via Kubernetes environment variables.但这可能是矫枉过正,如果你可以通过 Kubernetes 环境变量来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM