spring-cloud / spring-cloud-config

External configuration (server and client) for Spring Cloud
Apache License 2.0
1.96k stars 1.29k forks source link

Embedded config server with JDBC backend #1454

Closed girish-jung closed 4 years ago

girish-jung commented 5 years ago

We have a requirement to read the application properties from the Database. To achieve this I am trying to use spring cloud. I am trying to embed the config server in a spring boot application with JDBC backend so that the application can configure itself by reading properties from the DB. But I am unable to configure bootstrap.properties file. I am getting the following error:- Thanks for raising a Spring Cloud issue. What sort of issue are you raising?

Caused by: java.lang.IllegalStateException: You need to configure a URI for the git repository. at org.springframework.util.Assert.state(Assert.java:73) at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.afterPropertiesSet(JGitEnvironmentRepository.java:253) at org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.afterPropertiesSet(MultipleJGitEnvironmentRepository.java:66)

Why do we need to configure a git repository when we are using JDBC as a backend to load all the properties?

spencergibb commented 5 years ago

Please show your versions and configuration

girish-jung commented 5 years ago

Thanks for your quick response. Following is my bootstrap.properties file

#management.endpoints.web.exposure.include=refresh
#spring.cloud.config.enabled=false

spring.application.name=demo
spring.cloud.config.label=master
spring.profiles.active=jdbc
spring.cloud.config.server.bootstrap=true

Following is my application.properties file

spring.cloud.config.server.jdbc.sql= SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.cloud.config.server.jdbc.order=1

spring.datasource.url=jdbc:postgresql://localhost:5432/testsdb
spring.datasource.username=******
spring.datasource.password=******

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.connectionTestQuery=SELECT 1
datasource.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

 (Apart from the above config, I have normal application configs(properties) that I would like to move to DB, once this embedded server is able to configure itself works)
scottfrederick commented 5 years ago

Why do we need to configure a git repository when we are using JDBC as a backend to load all the properties?

The problem is that the JDBC environment repository configuration isn't meeting all the conditions that are required, so it isn't getting created and the Config Server auto-configuration is defaulting to a git repository.

I think there's a problem with using JDBC with an embedded config server (i.e. spring.cloud.config.server.bootstrap=true). The JDBC Config Server configuration depends on JdbcTemplate and DataSource beans in the application context, which are normally provided by Boot's JdbcTemplateAutoConfiguration and DataSourceAutoConfiguration. These auto-config classes do not appear to be getting run in the bootstrap application context before the Config Server EnvironmentRepositoryConfiguration auto-configuration is run.

You can see this by setting the system property debug=true when running the application and inspecting the CONDITIONS EVALUATION REPORT in the application log output. I see EnvironmentRepositoryConfiguration running, but failing the @ConditionalOnBean(JdbcTemplate.class) condition. JdbcTemplateAutoConfiguration and DataSourceAutoConfiguration are not in the condition evaluation report at all.

I got further by forcing those auto-configuration classes to run in the bootstrap application context. As an experiment/potential temporary work-around you can create a /resources/META-INF/spring.factories file in your project containing these lines:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

and then inspect the CONDITIONS EVALUATION REPORT to see if the JDBC repository configuration is working.

spencergibb commented 4 years ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spencergibb commented 3 years ago

See updated workaround here https://github.com/spring-cloud/spring-cloud-config/issues/1848#issuecomment-810441931