多个属性文件用于同一基于请求的Spring Boot应用程序

huangapple go评论75阅读模式
英文:

Multiple property files for same Spring Boot Application based on request

问题

我有一个使用Cucumber和Selenium进行UI测试自动化的Spring Boot应用程序。
预期该应用程序将测试多个环境。
首先,我已经创建了带有所需属性(如URL、凭据等)的JSON文件,并在触发测试时加载它(将文件路径作为属性传递并使用它)(mvn test -DconfigFile=config/env1_config.json)。

我注意到在运行测试时使用配置文件是一个选项-Dspring.profiles.active=client1,但由于我将在Jenkins中为测试多个环境配置多个流水线,以使用具有不同配置文件的相同项目,它将克隆整个项目并在与流水线对应的工作区中运行测试。为了避免保留项目的多个副本,我计划使用Rest API触发Selenium测试,并在Git中保存配置文件。

是否可以创建多个应用程序属性文件,其中包含自定义属性,将它们放置在Git中,并在Spring Boot应用程序(位于Rest API实现内部)中根据属性或RequestParam使用所需的属性文件,是否可以使用Spring Cloud Config或类似的东西实现这一点?

英文:

I have a Spring Boot application for UI test automation using Cucumber and Selenium.
The application is expected to test multiple environments.
To begin with I have created json files with required properties like URLs, credentials etc. and load it(pass the file path as a property and use it) while triggering the test (mvn test -DconfigFile=config/env1_config.json).

I see that using profiles while running test is an option-Dspring.profiles.active=client1 but as i will configure multiple pipelines in Jenkins for testing multiple environments to use same project with different configuration files, it will clone the entire project and run tests in workspace corresponding to the pipeline. To avoid keeping multiple copies of the project, i am planning to use Rest API to trigger Selenium tests and have configuration files in Git.

Is it possible to create multiple application properties files with custom properties, place them in Git and use required property file in a Spring Boot application(inside a Rest API impl) based on a property or RequestParam using Spring Cloud Config or something?

答案1

得分: 0

你可以使用Spring Boot配置文件,并将其作为Maven参数传递。

只需将-Dspring.profiles.active=test1作为命令行参数传递即可。

你可以在这里阅读更多信息。

英文:

you can use spring boot profiles and pass it as maven argument

just pass -Dspring.profiles.active=test1 as command line argument

you can read more here

答案2

得分: 0

你可以通过设置 spring.profiles.active 环境变量来实现。比如,如果你按照约定为每个环境使用属性文件,如 application-dev.properties、application-qa.properties,你可以将 spring.profiles.active 定义为 dev 和 qa。

你可以参考下面的链接获取更多关于这方面的信息。

https://stackabuse.com/spring-boot-configuring-properties/

英文:

You can do it by setting spring.profiles.active environment variable. Like if you are using property file for every environment by convention application-dev.properties, application-qa.properties. You can define you spring.profiles.active as dev and qa.

You can refer to the below link for more in site on same.

https://stackabuse.com/spring-boot-configuring-properties/

答案3

得分: 0

你可以使用Spring Cloud的服务器和客户端。

创建一个项目,其中包含依赖Spring Cloud Server,并在其中创建配置文件(appilcation.properties)。要在其他微服务中使用该配置文件,只需在其他项目中添加Spring Cloud Client依赖,并在application.properties中添加以下行:

spring.cloud.config.uri:[你的Spring Cloud服务器项目的URL]

参考链接:

  1. Spring Cloud服务器:
    https://www.youtube.com/watch?v=gb1i4WyWNK4&list=PLqq-6Pq4lTTaoaVoQVfRJPqvNTCjcTvJB&index=11
  2. Spring Cloud客户端:
    https://www.youtube.com/watch?v=E2HkL766VHs&list=PLqq-6Pq4lTTaoaVoQVfRJPqvNTCjcTvJB&index=12
英文:

You can use Spring cloud server and client.

Make A project With Dependency Spring cloud server and make config file(appilcation.properties) there. For use that config file in other microservice
just add spring cloud cilent dependency in other projects and add this line in application.properties

spring.cloud.config.uri:[your spring cloud server project url]

refrence:-

  1. spring cloud server:-
    https://www.youtube.com/watch?v=gb1i4WyWNK4&list=PLqq-6Pq4lTTaoaVoQVfRJPqvNTCjcTvJB&index=11
  2. spring cloud client:- https://www.youtube.com/watch?v=E2HkL766VHs&list=PLqq-6Pq4lTTaoaVoQVfRJPqvNTCjcTvJB&index=12

答案4

得分: 0

分享我最终采用的方法,因为它可能会在将来对某人有帮助。

由于我想创建一个 Rest API 并且根据 API 请求从不同的配置文件中使用属性,
我创建了一个 Spring Cloud Config Server 应用,它连接到 git 中的应用程序属性存储库,然后在我的 Rest API 实现的服务层中调用了 Spring Cloud Config Server 提供的 Rest API(主机:端口/应用/配置文件)。

英文:

Sharing the approach I ended up with as it might help someone someday.

As i wanted to create a Rest API and use properties from different config files in API implementation, based on the api request,
I created a Spring Cloud Config Server application that connects to application properties repo in git and I consumed Rest APIs exposed by Spring Cloud Config Server (host:port/app/profile) in the service layer of my Rest API implementation.

huangapple
  • 本文由 发表于 2020年8月24日 15:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定