In embedded config server, I configured MYSQL database using jdbc to fetch properties. After configuring bootstrap - true, it gives exception saying composite configuration needed. But in composite, only git and vault configuration supports. Could you let me know to connect jdbc to fetch properties in embedded config server.

Comment From: spencergibb

please provide the information requested when you filed this issue. Please send your versions, dependencies, configuration, and any relevant errors or logs.

Comment From: itslbzone

Spring boot starter parent - 2.3.3 release Spring cloud config server - hoxton.sr3 Com.oracle.jdbc -ojdbc8 - 12.2.0.1

Bootstrap.yml with composite configuration - Jdbc configuration

Exception: Java.lang.illegalstateexception : Error processing condition on org.springframework.cloud.config.server.config.compositerepositoryconfiguration.searchpathcompositeenvironmentrepository

Comment From: spencergibb

Please provide the full exception and full configuration as text, not as images (whatever image you posted didn't work).

Comment From: itslbzone

https://github.com/spring-cloud/spring-cloud-config/issues/1454

Same configuration. But with Oracle database.

Comment From: spencergibb

Did you try the suggestion here https://github.com/spring-cloud/spring-cloud-config/issues/1454#issuecomment-524380145

Comment From: itslbzone

Yes , I tried adding those configuration in spring.factories. dint resolve it. Either it is asking for git uri or gives exception as Java.lang.illegalstateexception : Error processing condition on org.springframework.cloud.config.server.config.compositerepositoryconfiguration.searchpathcompositeenvironmentrepository

Comment From: spencergibb

Please provide the full exception

Comment From: itslbzone

bootstrap.properties

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

spring.datasource.url=jdbc:mysql://xxx:3306/xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
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.profiles.active=jdbc

console

***************************
APPLICATION FAILED TO START
***************************

Description:

Invalid config server configuration.

Action:

If you are using the git profile, you need to set a Git URI in your configuration.  If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

2021-03-27 12:22:40.614 ERROR 12524 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: You need to configure a uri for the git repository.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:80) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at com.howtodoinjava.demo.SpringBootHelloWorld.main(SpringBootHelloWorld.java:19) ~[classes/:na]
Caused by: java.lang.IllegalStateException: You need to configure a uri for the git repository.
    at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.afterPropertiesSet(JGitEnvironmentRepository.java:253) ~[spring-cloud-config-server-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.afterPropertiesSet(MultipleJGitEnvironmentRepository.java:66) ~[spring-cloud-config-server-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 28 common frames omitted

Comment From: itslbzone

Any suggestions

Comment From: spencergibb

You must use the composite environment repository when using spring.cloud.config.server.bootstrap=true. I've created #1852 to address the hint.

spring.cloud.config.server.composite[0].type=jdbc
spring.cloud.config.server.composite[0].sql= SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.cloud.config.server.composite[0].order=1
spring.profiles.active=composite

You must create a bootstrap configuration that imports the correct datasource and jdbc auto configuration

package com.example.democonfigserverjdbc;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;

@Configuration
@Import({DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})
public class MyBootstrapConfiguration implements Ordered {

    @Override
    public int getOrder() {
        return 0;
    }
}

and in src/main/resources/META-INF/spring.factoris

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.democonfigserverjdbc.MyBootstrapConfiguration

We will reuse this issue to create that bootstrap configuration automatically.

Comment From: itslbzone

bootstrap.properties

spring.application.name= demo
spring.cloud.config.server.bootstrap=true
#spring.datasource.url=jdbc:mysql://localhost:3306/mysql
#spring.datasource.username=root
#spring.datasource.password=mysql
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.cloud.config.server.composite[0].type=jdbc
spring.cloud.config.server.composite[0].sql= SELECT PROP_KEY, VALUE from PROPERTIES where APPLICATION= ? and PROFILE= ? and LABEL= ?
spring.cloud.config.server.composite[0].order=1
spring.profiles.active=composite

resources/META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.howtodoinjava.demo.MyBootstrapConfiguration

MyBootstrapConfiguration .java

@Configuration
@Import({DynamicDatasourceConfig.class, Template.class})
public class MyBootstrapConfiguration implements Ordered {
    @Override
    public int getOrder() {
        return 0;
    }
}

DynamicDatasourceConfig .java

@Configuration
public class DynamicDatasourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource getDataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();

        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://localhost:3306/mysql");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("mysql");
        return dataSourceBuilder.build();
    }
}

Template.java

@Configuration
public class Template {
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.codejava</groupId>
    <artifactId>spring-boot-hello-world</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>     

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
            <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
      <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.2.0</version>
</dependency>  
    </dependencies>
     <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR8</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>  
</project>

i tried in both datasource bootstrap configuration and also in bootstrap.properties. Application can able to build successfully. But database binding is not happening. Please check above code and guide accordingly.

Comment From: spencergibb

the project you shared with me didn't have a Template.primaryJdbcTemplate() or DynamicDatasourceConfig. I used what boot provides.

Comment From: itslbzone

I can able to see loaded property values from DB in browser http://localhost:8888/OPO/dev/latest

{"name":"OPO","profiles":["dev"],"label":"latest","version":null,"state":null,"propertySources":[{"name":"OPO-dev","source":{"Environment":"Development"}}]}

but i could not able to read inside application, it gives null for below code execution

@Configuration
@RefreshScope
public class EmployeeController {

    @Autowired
    private Environment env;


    @PostConstruct
    public String getValue() 
    {
        System.out.println(env.getProperty("Environment"));
        return env.getProperty("Environment");
    }

}

Kindly guide on above issue.

Comment From: itslbzone

After inclusion of datasource and composite configurations in bootstrap.properties with bootstrap = true . Added mybootstrapconfiguration.java class mentioned above along with spring.factories. I can able to see the config details fetched from db through browser, but I couldn't read property values inside code. Kindly provide your suggestions.

Comment From: itslbzone

Please let me know the version and release tentative schedule in which this issue will be fixed.

Comment From: spencergibb

The projects on the issue will be updated when they are scheduled

Comment From: spencergibb

In your previous comment you said this URL worked http://localhost:8888/OPO/dev/latest.

For that to be what is requested by the config client set the following values:

spring.application.name=OPO
spring.profiles.active=dev
spring.cloud.config.label=latest

Comment From: itslbzone

As mentioned, it will work for client application. But I am working embedded config server. I need to read property values inside same application (embedded config server).

I guess, Properties which fetched from db is not loaded into the application's context. But I could see the values in the browser url. http://localhost:8888/OPO/dev/latest

Please provide suggestions to read properties inside same application.

Comment From: spencergibb

My previous comment still stands. That is the way to get the values you are requesting regardless if it is embedded or not.

Comment From: itslbzone

spring.profiles.active=dev doesnt work. as it needs to be composite. PFB bootstrap.properties

spring.application.name= OPO spring.application.profile= dev spring.application.label= latest

spring.profiles.active=dev

spring.cloud.config.label=latest

spring.cloud.config.server.bootstrap=true spring.profiles.active=composite spring.cloud.config.server.composite[0].type=jdbc spring.cloud.config.server.composite[0].sql= SELECT PROP_KEY, VALUE from PROPERTIES where APPLICATION= ? and PROFILE= ? and LABEL= ? spring.cloud.config.server.composite[0].order=1 spring.datasource.url=jdbc:mysql://localhost:3306/mysql spring.datasource.username=root spring.datasource.password=mysql spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Comment From: spencergibb

you can use spring.cloud.config.profile instead of spring.profiles.active.

Comment From: itslbzone

It works. Thanks Spencer!

Comment From: GabrielOlOr

Good day.

I wonder how I can configure it so that instead of executing a select * from. Be a call to a store procedure.

call shema.package.store(?,?,?)

Tanks