Search code examples
javaspring-bootbootstrap-4consulspring-cloud-consul

how to get Array of values from consul in Springboot


I have multiple host values according to different markets how to fetch all those in spring-boot to create a bean. I have tried with

@value("#{${app.host}}")
private String[] host;

consul values

app:
  host:
    hostone: 'localhost:8080'
    hosttwo: 'abc:8089'

Solution

  • You can obtain an array whit the following annotation

    @Value("${app.host}")
    private String[] host;
    

    but your yml should be

    app:
      host:
        - localhost:8080
        - abc:8089
    

    or you can get

    @Value("${app.host}")
    private Map<String,String> host;
    

    and keep your YML file as-is.