I'm using Spring Cloud Config 3.1.3 with Spring Boot 2.7.2. I created an embedded configuration server using the instructions contained in the official documentation.
My goal is embedding the configuration server into an application. The application should load the configuration properties provided by the embedded configuration server using a custom EnvironmentRepository. It seems that this should be possible:
If you want to read the configuration for an application directly from the backend repository (instead of from the config server), you basically want an embedded config server with no endpoints
But the configuration properties are not loaded. This is the sample project:
https://github.com/taxone/embedded-config-server
The custom repository works fine if the endpoint below is called:
http://localhost:8888/config/application/default
and the response is:
{
"name": "application",
"profiles": [
"default"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "test",
"source": {
"pippo.name": "ciao"
}
}
]
}
but the property is not injected:
@Configuration
@ConfigurationProperties(prefix = "pippo")
public class Embedde2Properties {
@Getter
@Setter
private String name;
}
It seems that someone else had a similar issue on stack overflow.
Comment From: ryanjbaxter
Can you try adding the dependency spring-cloud-starter-bootstrap
?
Also can you move
spring:
application:
name: myconfigserver
profiles:
active: composite
cloud:
config:
server:
prefix: /config
bootstrap: true
composite:
- type: prova
to bootstrap.yml
Comment From: taxone
Hi Ryan, thank you for your answer. I did the suggested modifications and pushed to the sample project. But now the application fails to start with the error below:
2022-08-04 08:37:34.965 ERROR 20260 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Error processing condition on org.springframework.cloud.config.server.config.CompositeRepositoryConfiguration.searchPathCompositeEnvironmentRepository
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.7.2.jar:2.7.2]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:193) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:153) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:343) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:164) [spring-boot-2.7.2.jar:2.7.2]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:195) [spring-cloud-context-3.1.3.jar:3.1.3]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:114) [spring-cloud-context-3.1.3.jar:3.1.3]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:77) [spring-cloud-context-3.1.3.jar:3.1.3]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) [spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) [spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) [spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) [spring-context-5.3.22.jar:5.3.22]
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85) [spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66) [spring-boot-2.7.2.jar:2.7.2]
at java.util.ArrayList.forEach(ArrayList.java:1259) ~[na:1.8.0_281]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) [spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) [spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65) [spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:344) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.2.jar:2.7.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.2.jar:2.7.2]
at eu.tasgroup.poc.embedded.Embedded2ServerApplication.main(Embedded2ServerApplication.java:19) ~[classes/:na]
Caused by: java.lang.NullPointerException: null
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[na:1.8.0_281]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:869) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.cloud.config.server.composite.CompositeUtils.getEnvironmentRepositoryFactoryTypeParams(CompositeUtils.java:77) ~[spring-cloud-config-server-3.1.3.jar:3.1.3]
at org.springframework.cloud.config.server.composite.OnSearchPathLocatorPresent.getMatchOutcome(OnSearchPathLocatorPresent.java:46) ~[spring-cloud-config-server-3.1.3.jar:3.1.3]
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.7.2.jar:2.7.2]
... 32 common frames omitted
I suppose that the property spring.cloud.config.server.bootstrap: true could be moved to application.yaml. After moving that property back to application.yaml, the application starts, but the properties are not loaded.
Comment From: ryanjbaxter
OK I didn't realize that you were configuring your own EnvironmentRepository. To make this you need to make sure your EnvironmentRepository so its available via bootstrap. Here is a modified project that works embedded-config-server.zip
Comment From: taxone
It was the missing bit! Now it works like a charm. Thank you very much Ryan, I'm going to update the sample project and reply the question on stack overflow so it could help someone else.