OD Street
OD Street

Reputation: 1087

Properties on Spring Cloud Config Server doesn't inject into Spring application

I'm teaching myself the Spring Cloud Config Server and having some trouble with injecting the properties into the bean.

So I have a simple Spring Boot Application as config client, just for testing:

@SpringBootApplication
@ConfigurationProperties
public class DemoApplication {

    @Value("${greeting}")
    static private String greeting;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);  
        System.out.println("The greeting is: " + greeting);
    }
}

But the system print:

The greeting is: null

Checking the env endpoint, I actually found the "${greeting}" property was in the environment:

profiles: [ ],
configService:https://github.com/mstine/config-repo.git/demo.yml: {
greeting: "Webhook"
},
configService:https://github.com/mstine/config-repo.git/application.yml: {
eureka.instance.hostname: "localhost",
eureka.instance.leaseRenewalIntervalInSeconds: 10,
eureka.instance.metadataMap.instanceId: "${vcap.application.instance_id:${spring.application.name}:${server.port:8080}}",
eureka.client.serviceUrl.defaultZone: "${vcap.services.service-registry.credentials.uri:http://127.0.0.1:8761}/eureka/",
foo: "barnyardAnimal"
},

Notice that in the configService, there is a property called greeting which has value of "Webhook"

I'm new to Spring Framework so I'm wondering didn't I mess up something? Someone suggest I can also access external properties using Environment but I didn't find too much tutorial of how to use it. Any thought?

===================================UPDATE====================================== Adding the code of the config server:

Application.java:

package io.spring.cloud.samples.fortuneteller.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class Application {

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

application.yml:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mstine/config-repo.git

Upvotes: 3

Views: 3415

Answers (5)

SARIKA
SARIKA

Reputation: 27

If you are using a local repository, the application.yml should look like this:

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file: --Path--

Upvotes: 2

Mukund Jalan
Mukund Jalan

Reputation: 1339

You need to add the spring-cloud-dependencies to the classpath of your client application for it to take cloud config in its auto starter configuration.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring.cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Upvotes: 0

Alice
Alice

Reputation: 21

You just cannot inject properties as static fields. Try to inject it in some @Component

Upvotes: 0

lopushen
lopushen

Reputation: 1137

As far as I see, you've forgotten to add

@EnableDiscoveryClient

above the main class in client app

I did this and it worked for me in similar case

Upvotes: 0

Koitoer
Koitoer

Reputation: 19533

I think you need to create and start your server as well, I did not see the code

@SpringBootApplication
@EnableConfigServer
public class Application {

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

Once you enable your server, your client need to connect to it and ask for the configuration parameters that the server will store for it. This server need to store the .yml in which the values are going to be stored.

Client code need to talk to the server and ask for the specific values with the bootstrap.yml

---
spring:
  profiles:
    active: northamerica
  application:
    name: koitoerclient
  cloud:
     config:
       uri: http://localhost:8001
server:
  port: 8002

You can find the configuration and a example for client and server code in github here, is very simple so you could understand the entire config.

Upvotes: 0

Related Questions