convert application.yml file to application.properties - java

I have my application.yml file as below. How to convert it to application.properties
I am trying it but how can i write multiple properties in same file. Its giving me duplicate kery error.
---
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2/eureka/
---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1/eureka/

IntelliJ and other IDEs provide plugins for the same.
For eg- https://plugins.jetbrains.com/plugin/13804-convert-yaml-and-properties-file
Install the plugin, right click on your yaml or properties file and choose - "Convert yaml and properties file".

With Spring Boot 2.4, there's the possibility to use the switch spring.config.activate.on-profile for this purpose, everything defined after spring.config.activate.on-profile=myprofile will only be set when the active profile is set to myprofile. In the given example, you would do as follows:
#-- Peer1 Config
spring.config.activate.on-profile=peer1
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
#-- Peer2 Config
spring.config.activate.on-profile=peer2
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/
See https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4 for more information.

You'll need to create differents files, example:
application-dev.properties
application-prod.properties
application-test.properties
And then you deffine your active profile in the application.properties with:
spring.profiles.active=dev

When using properties file, you cannot have multiple "sections" per profile in the same file, this is a feature available only with Yaml.
You will have to create several properties file, one per profile, as described here : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment
To do the same thing with properties files, you can use application-${profile}.properties to specify profile-specific values
You will have one main application.properties file containing common values, and then one application-${profile}.properties file per profile containing values that are environment/profile dependent.
Finally, you will have to set the active profile either as a System property when running the application, or directly in your main application.properties file, as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

to do it manually:
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2/eureka/
will be like this:
spring.profiles=peer1
spring.eureka.instance.hostname=peer1
spring.eureka.client.serviceUrl.defaultZone=http://peer2/eureka/

Related

Profile Groups in Spring Boot 3 not working

After the migration to Spring Boot 3 it's not possible to activate multiple profiles in profile specific files. Instead Profile Groups should be used.
Unfortunately I can't get them to work, here is the snippet from my yml config:
spring:
profiles:
group:
local: debuglogin, profile_a, profile_b, profile_c
I have tried this in application-local.yml. Did anyone had the same experience?
I've just found the issue. I was adding this code block in the profile specific file like application-local.yml instead it should be added in the application.yml:
spring:
profiles:
group:
local: debuglogin, profile_a
integration-test: debuglogin, profile_b
this should work if added in the application.yml but not in profile specific files.

Why yml files inside microservice are not able to read values from yml files of core?

I have core like this:
application-dev.yml
app:
region: east
application-uat.uml
app:
region: south
Then I have a microservice with following yml files
application-dev.yml
direct:
url: http://test-${app.region}.com
application-uat.yml
direct:
url: http://test-${app.region}.com
I believe, my spring application should be able to read values from core but it's not reading values from core. What am I doing wrong here?
You have to change one of the two file name and the micorservice module must depend on core module. Then you can add in application-dev.yml or application-uat.yml of the microservice module
spring:
config:
import: classpath:core-dev.yml

Spring Boot profile properties doesn't work with tests

Spring Boot version 2.2.6. I have case that there is several application-{profile}.yml files in folder src/main/resources/ and I want't to build project with Maven e.g mvn clean package -Dspring.profiles.active=dev
Then I have just application.yml file in folder src/test/resources, this should be a properties file to all test (IT/unit).
Now when I build with command mvn clean package -Dspring.profiles.active=dev, properties src/main/resources/application-dev.yml and src/test/resources/application.yml are MERGED and used for the tests. Well in test properties there might be pretty fatal confs e.g Hibernate auto-ddl: create-drop.
Have been reading docs but I don't find any logic why properties files are MERGED in this case. Is there any good practice that tests can be forced to use ALWAYS certain properties file? I think that just using some annotations in test files isn't the best practice, e.g #TestPropertySource or #ActiveProfiles, cause when you forgot to use these annotations then the case is same again. Is there some global configuration for all tests or some other better solutions?
The application.yml file has higher precedence over any environment-specific file, for example, application-dev.yml file. The standard inheritance applies, so you can override the parent properties in the profile-specific YAML file.
application.yml
server:
port: 9090
spring:
jpa:
hibernate:
ddl-auto: create-drop
application-dev.yml
spring:
jpa:
hibernate:
ddl-auto: none
In case you run with the dev profile, the value of spring.jpa.hibernate.ddl-auto parameter would be none.
I get that application.yml has higher precedence in folder /src/main, but that it takes over also from folder /src/test is kinda weird.
Yes that config can be made into profile-specific files (ddl-auto: none), tough I still have problem with datasource, somehow Spring Boot is picking application-dev.ymls datasource into tests also, this is working like other way around than that ddl-auto attribute.
src/test/resources/application.yml
spring:
datasource:
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL
src/main/resources/application-dev.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/db

Spring config server cannot be disabled for local development

I was updating microservice to the newest Springboot - for now it is 2.5.5 with cloud version 2020.0.4. However there were some changes related to connection with config server. Previously config server configuration looked like this:
spring:
cloud:
config:
uri: ${CONFIG_SERVER_URI:http://localhost:8888}
fail-fast: ${CONFIG_FAIL_FAST:true}
However now it's required to provide it this way:
spring:
config:
import: configserver:${CONFIG_SERVER_URI:http://localhost:8888}
And that would be perfectly fine, except I am not able to run microservice locally without connecting to config server.
Previously I did it like this:
spring:
cloud:
config:
enabled: false
And it was perfectly fine, I had separate application-local.yaml file and had what I wanted.
Now I tried this (according to what is said in documentation):
spring:
cloud:
config:
import-check:
enabled: false
enabled: false
config:
import: "optional:configserver:http://localhost:8888"
But once I run microservice locally, I get only single log like this:
Connected to the target VM, address: '127.0.0.1:59759', transport: 'socket'
And basically nothing more, seems like it keeps trying to connect to config server, because if I run config server and after this try to run my microservice, it's working fine. Additional weird thing is that IntelliJ is showing me that it doesn't recognize this import-check property.
As for dependencies I have only implementation 'org.springframework.cloud:spring-cloud-starter-config:3.0.5' related to config server.
Is there some way to run microservice locally so that it's not connecting to config server using Spring Boot 2.4+? I don't want to use bootstrapping and providing additional dependency, since there is no point to use legacy stuff.
Here is the configuration I have for application.yaml in /src/main/resources folder.
---
spring:
config:
activate:
on-profile: "!config-local"
import: configserver:http://config-server.example.com:8888/
cloud:
config.fail-fast: true
---
spring:
config:
activate:
on-profile: "config-local"
import: optional:configserver:http://localhost:8888/
cloud:
config:
enabled: false
And in Intellij IDEA Run config, I set the active Profile as config-local this works with Springboot 2.5.5 and
ext {
set('springCloudVersion', "2020.0.4")
}

How to select profiles in spring-boot

I have application files according to my environment: application.yml and application-uat.yml. I'm running the application by providing the SPRING_PROFILES_ACTIVE as environment variable so that the correct file is selected. I have a problem with defining additional profiles inside application.
Suppose I have a bean with profile mock-rest that mocks the rest client and I have a bean with profile actual-rest.
I've tried two do it in those ways:
#inside application.yml
spring:
profiles:
include: mock-rest
And for UAT:
#inside application-uat.yml
spring:
profiles:
include: actual-rest
But the problem is that spring will include both profiles, because it takes the both files and only replaces the values from uat yml file.
If I try to use spring.profiles.active inside yml files Spring will ignore that as the SPRING_PROFILES_ACTIVE environment variable will have priority.
So, the question is what is the way to overcome described problem? Basically, I need to define profiles inside application.yml files and I want to only define environment as my SPRING_PROFILES_ACTIVE env variable. Is this possible? If not, what are my options to consider?
The application.yml file is read whatsoever. This is an indented behaviour. Usually, all values in application.yml would be overrided by other more specific application-*.yml.
But profiles are not a single values, you can have multiple one enabled at the same time, so i does not oeverride the profile, instead it cumulate both.
If you want to get rid of the mock-rest on prod environment you might want to remove the mock-rest profile from application.yml and move it to a new application-mock.yml that you can activate using an environment variable.
application.yml
# some default configuration
application-mock.yml
spring:
profiles:
include: actual-rest
application-uat.yml
spring:
profiles:
include: actual-rest

Categories

Resources