Loading properties from a custom config server using Eureka

Viewed 499

I want to bootstrap my web application with a set of properties that are supplied by a service that is registered with Eureka.

I set up Eureka, ConfigServer (REST service) & Client Application locally.

Eureka Server exposed in port 8761 has the service registered to it. enter image description here

/data endpoint of the service responds with a set of key-value pairs as given below. enter image description here

Config Server Code

@GetMapping("/data")
    public Map<String, String> getAllProperties(){
        
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("A", "1");
        dataMap.put("B", "2");
        dataMap.put("C", "3");
        dataMap.put("D", "4");
        dataMap.put("E", "5");
        dataMap.put("F", "6");
        dataMap.put("G", "7");
        
        return dataMap;
        
    }

Client/Application Side

Since I require customized handling of the response, I created a CustomConfigLocator class that implements org.springframework.cloud.bootstrap.config.PropertySourceLocator and override the locate() method to query and set the custom properties.

In order to query the data (properties) , I need an instance of DiscoveryClient to discover the Config Server and query the /data end point.

CustomConfigLocator is initialized while bootstrapping the application using spring.factories file.

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.config.client.ConfigClientApplication.CustomConfigLocator

The main class is annotated with @EnableDiscoveryClient and the bootstrap.properties are supplied with the following properties.

spring.cloud.config.discovery.enabled = true
spring.cloud.config.discovery.service-id = CUSTOMCONFIGSERVER
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

Now coming to the real issue. As I mentioned earlier, an instance of DiscoveryClient is required to discover the Config Server instance from Eureka. But when I tried to inject (@Autowire) the instance of org.springframework.cloud.client.discovery.DiscoveryClient, the application failed to start complaining that the instance of that type is not available in the container.

public class CustomConfigLocator implements PropertySourceLocator{
    
    @Autowired
    private DiscoveryClient discoveryClient;

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.config.client.ConfigClientApplication.CustomConfigLocator': Unsatisfied dependency expressed through field 'discoveryClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:

Can anyone suggest a way to access the DiscoveryClient from the CustomConfigLocator class?

############## UPDATE-1 ################

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
0 Answers
Related