spring boot - application yml for each developer - spring-boot

In my work the fields in application.yml are dynamics and each developer have another (dynamic) configuration. So, we have the general yml section, that relevant for all of us (and have updates from time to time) and the “private profile”
Like this:
spring:
profiles: default
configuration:
...
...
spring:
profiles: development1
configuration: ...
spring:
profiles: development2
configuration: ...
We have “small” problems with this (mistakes, conflicts, etc.) and we’re look for solution.
I think about separate yml file for each developer, such as:
spring:
profiles: development1
configuration:
yml-file: app_dev1.yml
spring:
profiles: development2
configuration:
yml-file: app_dev2.yml
but didn’t find how to do this…
Will be glad to know if it can be done? (or you have another way to separate the yml between us).
We do not want to set an application yaml with annotation (git issues again..), something like Environment Variables...

Spring boot by default loads application.yaml but in addition if you run with --spring.profiles.active=dev1 it will load file application-dev1.yaml
So you could create yaml file per developer + have some "common" configuration in application.yaml
With such an approach you won't need lines like:
spring:
profiles: dev1
... in any of the yaml files

Related

SpringBoot yml how can I run two profiles at the same time

In my application yml, I have two profiles which I need to both run
spring:
profiles:
active:
awss3, #env#
application:
name: CONFIG-SERVICE
I need awss3 to be run, in what ever the env is. Is this possible ?
In my opinion, your requirements fits Profile Groups. This approach is preferable as it allows you to visually inform the developer which profiles are required for which environments.
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles.groups
Example:
spring:
profiles:
group:
production:
- "aws3"
- "prodmq"
Then you can start the application using:
--spring.profiles.active=production
to active the production, aws3 and prodmq profiles in one hit.
To clarify: The keyword production under group: is what you will use when you run your application as spring.profiles.active=production
Alternatively like you can simply add whatever profiles you want from the command line.
java -jar -Dspring.profiles.active=awss3,prodmq demo-0.0.1-SNAPSHOT.jar
yes, using properties:
// Use your dynamic profile
spring.profiles.active=#env#
// Include your default profile
spring.profiles.include=awss3
yaml:
spring:
profiles:
include: awss3
active:
#env#

How can I activate Profile in Spring Boot YAML?

I have below application YAML of my Spring Boot (2.4.5) application with two profiles.
spring:
profiles: dev
spring:
profiles: prod
Now when I pass the arguments -Dspring.profiles.active=dev, it actually doesn't pickup the profile. Is there any changes in profile has been made recently.
When I activate the profile, like below it works
spring:
profiles:
active: dev
But as per the env profile can be different. I don't want to hard code the profile
You should use the following instead in your application.yml:
spring
config:
activate:
on-profile: dev
---
spring
config:
activate:
on-profile: prod

Yml config files "Inheritance" with Spring boot

I couldn't find a straight answer online.
Do Spring Boot's yml files "inherit" from each other? I mean if I have:
application.yml which has
server:
port: 80
host: foo
and application-profile1.yml which has only
server:
port: 90
So if I start my Spring Boot with profile1 as active profile, will I also have server.host property set to foo?
Yes, application.yml file has higher precedence over any application-{profile}.yml file. Properties from profile specific yml file will override values from the default application.yml file and properties that do not exist in profile specific yml file will be loaded from the default one. It applies to .properties files as well as to bootstrap.yml or bootstrap.properties.
Spring Boot documentation mentions it in 72.7 Change configuration depending on the environment paragraph:
In this example the default port is 9000, but if the Spring profile ‘development’ is active then the port is 9001, and if ‘production’ is active then it is 0.
The YAML documents are merged in the order they are encountered (so later values override earlier ones).
To do the same thing with properties files you can use application-${profile}.properties to specify profile-specific values.
Here is my solution.
Assume application.yml:
spring:
profiles: default-server-config
server:
port: 9801
servlet:
context-path: '/ctp'
If I want use default-server-config profile, and use port 8080 in my application-dev.yml
application-dev.yml:
spring:
profiles:
include:
- default-server-config
- dev-config
---
spring:
profiles: dev-config
server:
port: 8080
Then -Dspring.profiles.active=dev

Spring cloud config in local dev mode

Is it possible to control if the application requires spring-config-server based on profile.
I want to pick properties from resource/... in say local-dev profile and use cloud-config only with a profile where there would be a config-server running.
You can disable using config-server for a specific profile. Please try to define the properties in bootstrap.yml like below. Please note, we should configure these settings only in bootstrap.yml. Setting these properties in application.yml will not work. In this case, local profile will be running only with local profiles in /resources folder. In dev profile, properties from config-server will override properties from resources/ folder.
spring:
profiles: local
cloud:
config:
enabled: false
---
spring:
profiles: dev
cloud:
config:
uri: http://<your config server>

spring-cloud-config-server - File System Backend searchLocation with placeholder

The search locations can contain placeholders for {application},
{profile} and {label}
I am trying to segregate my properties into distinct directories like /app1/dev, /app2/dev, /app2/prod
I have set the searchLocations property in application.yml of my configuration server as given below.
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
#searchLocations: file:./config/{application},file:./config/{application}/{profile} # Trial 01
searchLocations: file:./config/,file:./config/{application},file:./config/{application}/{profile} # Trial 02
I have kept the properties inside directories like zuul/prod, zuul/test
BUT the config server is unable to serve the configurations.
For the request http://localhost:8082/zuul/prod it gives
{"name":"zuul","profiles":["prod"],"label":"master","propertySources":[]}
Kindly clarify how to achieve this segregation.
Environment : Spring Cloud Angel.SR2 & 3

Resources