简体   繁体   中英

Spring Boot - Profiles (application.properties) to define baseUrl for HTTP requests

The situation is that I have two api projects, API A does HTTP requests to API B. Both API's are deployed to a development and production environment.

What I want to achieve is the following: Build the project based on a specific profile (dev or prod) so that the code can use a particular baseurl to talk with the correct api on the correct environment.

So if I build API A based on prod flag, I want it to use the specific url to make http requests to API B that is deployed on it's own prod environment.

It looks like you're referring to profiles of maven, however you should probably check out spring profiles. The concept should change :

You're not supposed to build different artifacts for different environments.

Instead create a spring profile in service A:

application-dev.properties:

url.addr=dev-service-host:1234
application-prod.properties:

url.addr=prod-service-b-host:4321

Then run the application with --spring.profiles.active=dev (or prod ) flag.

Spring boot will load the correct definitions automatically because the dev/prod matches the suffix of properties file

You can define Spring-Boot profile as:

spring.profiles.active=prod

You also should have profiled .properties files in resources :

  • in application-dev.properties you should have api.b.url={api_b_url_on_dev_environment}

  • in application-prod.properties you should have api.b.url={api_b_url_on_prod_environment}

Or if you don't want to recompile your application after changing properties you may use outside .properties files.

In order for them to be included during app's deployment do the following:

  • in some config directory add application-dev.properties and application-prod.properties
  • deploy you app with the following properties: --spring.profiles.active=dev and --spring.config.additional-location=config/application.properties

This way the outside profiled properties will be included in deployment process. These .properties files have the highest priority in Spring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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