8

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/
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sudhanshu Saini
  • 127
  • 1
  • 2
  • 9
  • The closest thing you can do is having an array of elements. – Maaaatt Aug 28 '18 at 13:58
  • I do not understand what you are trying to accomplish. You should clarify that, or you won't get any high-quality answers. –  Jul 29 '20 at 04:18

5 Answers5

6

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".

Satakshi Pandey
  • 234
  • 4
  • 9
1

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.

A. Markóczy
  • 673
  • 7
  • 15
1

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/
HibaHasan
  • 1,255
  • 2
  • 5
  • 12
0

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

M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
0

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
  • but i need to run all instances together. These profiles are my eureka instances and i need to make cluster of them and run together. so how can i make one of them active at one time. – Sudhanshu Saini Aug 28 '18 at 14:12
  • you can have several actives profiles : just provide the list with coma separated , e.g.: `-Dspring.profiles.active=peer1,peer2` – M.Ricciuti Aug 28 '18 at 14:13
  • 1
    If you are running multiple Eureka instances and want to access them interchangeably, then you should probably run a load balancer (like nginx) and point your app to that, or you should define a property which contains a list of URLs. The way you are trying to do it right now, it won't work as a yaml or as a properties file. –  Jul 29 '20 at 04:22
  • Oh wait, are you trying to follow this tutorial: https://medium.com/swlh/spring-cloud-high-availability-for-eureka-b5b7abcefb32 or something similar? If so: You do NOT "need to run all instances together". You need to have multiple profiles, and multiple containers, and in every container, exactly ONE of the profiles is active. –  Jul 29 '20 at 04:24