5
votes

I'm trying to share configuration between Spring Cloud clients with a Spring Cloud config server which have a file-based repository:

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

// application.yml
server:
  port: 8888

spring:
  profiles:
    active: native

test:
  foo: world

One of my Spring Cloud client use the test.foo configuration, defined in the config server, and it is configured like below:

@SpringBootApplication
@RestController
public class HelloWorldServiceApplication {

    @Value("${test.foo}")
    private String foo;

    @RequestMapping(path = "/", method = RequestMethod.GET)
    @ResponseBody
    public String helloWorld() {
        return "Hello " + this.foo;
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldServiceApplication.class, args);
    }
}

// boostrap.yml
spring:
  cloud:
      config:
        uri: ${SPRING_CONFIG_URI:http://localhost:8888}
      fail-fast: true

// application.yml
spring:
  application:
    name: hello-world-service

Despite this configuration, the Environment in the Spring Cloud Client doesn't contains the test.foo entry (cf java.lang.IllegalArgumentException: Could not resolve placeholder 'test.foo')

However it's works perfectly if i put the properties in a hello-world-service.yml file, in my config server file-based repository.

Maven dependencies on Spring Cloud Brixton.M5 and Spring Boot 1.3.3.RELEASE with spring-cloud-starter-config and spring-cloud-config-server

1
@AliDehghani The question have been updated with the correct spellingherau
You said "file-base repository" but you didn't show the configuration for it, nor did you say what the location of the application.yml is.Dave Syer
@DaveSyer i'm saying file-base repository because i'm using the native spring profile. The application.yml file is in the local classpath like standard spring boot applicationherau
If I remember correctly we don't server the config server's own application.yml by default, otherwise ever service would try and run with server.port: 8888. Put test.foo in application.yml in the same dir where you put hello-world-service.yml.spencergibb
@spencergibb hello-world-service.yml file is in the same dir than the application.yml. I finally found the cause of my issue (see my answer)herau

1 Answers

1
votes

From Spring Cloud documentation

With the "native" profile (local file system backend) it is recommended that you use an explicit search location that isn’t part of the server’s own configuration. Otherwise the application* resources in the default search locations are removed because they are part of the server.

So i should put the shared configuration in an external directory and add the path in the application.yml file of the config-server.

// application.yml
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:/Users/herau/config-repo

// /Users/herau/config-repo/application.yml
test:
  foo: world