ASR
ASR

Reputation: 3559

Spring config client not able to get values from config server

I am trying to read a message from different properties based on the profile. For example I have placed 3 properties files in github:

  1. test-app-dev.properties
  2. test-app-prod.properties
  3. test-app-stage.properties

Now my config server has below details in application.properties

spring.cloud.config.server.git.uri=https://github.com/asudheer09/local-config-server-configs.git server.port=8888 spring.cloud.config.server.git.default-label=main spring.application.name=config-server server.servlet.context-path=/config-service

When I am trying to access the http://localhost:8888/config-service/test-app-prod.properties I can see the properties file on browser and similarly others also i can see.

The below are my config client details:

In bootstrap.properties:

spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8888/config-service
management.security.enabled=false
spring.application.name=test-app

java file :

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RefreshScope
public class SpringProfilesExampleApplication {

    @Autowired
    public void setEnv(Environment e) {
        System.out.println(e.getActiveProfiles().toString());
        System.out.println(e.getProperty("message"));
    }

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

}

@RefreshScope
@RestController
class MessageRestController {

    @Value("${message:Config Server is not working. Please check...}")
    private String msg;

    @GetMapping("/msg")
    public String getMsg() {
        return this.msg;
    }
}

When i run my config client i am getting the message value as null, but i am not able to see any error message. can any one help me on this ?

Upvotes: 1

Views: 1868

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 19173

Try making the following changes

In git you must have different urls for each enviroment (dev, prod, ..).

spring:
  cloud:
    config:
      server:
        git:
          repos:
            dev:
              pattern: test-app/dev
              uri: https://github.com/asudheer09/local-config-server-configs/test-app-dev.properties
            prod:
              pattern: test-app/prod
              uri: https://github.com/asudheer09/local-config-server-configs/test-app-prod.properties

With these configurations your config server will serve clients using the following logic

If client application has name test-app and profile dev then it will load the properties from https://github.com/asudheer09/local-config-server-configs/test-app-dev.properties

If client application has name test-app and profile prod then it will load the properties from https://github.com/asudheer09/local-config-server-configs/test-app-prod.properties

It does not make any sense to try and make System.out.println directly on config server when it starts up. You don't need the application properties of config server but those properties that will be served on clients.

Check more info here

spring cloud config documentation

Upvotes: 1

Related Questions