Search code examples
javah2spring-cloud-configspring-cloud-config-server

Spring Cloud Config Server with Database backend


I am trying to create a Spring Cloud Config Server that gets its Configurations from a database and not from the default git-repo.

Every time I try to run my Config Server Application I get this Error:

Execution failed for task ':config-server:applications:config-server:bootRun'.
> Process 'command 'C:\Program Files\choco\openjdk-jdk-11\latest\bin\java.exe'' finished with non-zero exit value 1

From the Server itself comes this message (reformatted for better reading):

[ERROR agnostics.LoggingFailureAnalysisReporter : 
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.
[main||||||| - |]]

My application.yaml has this part in it:

spring:
  application:
    name: my-services-api
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    platform: h2
    hikari:
      connection-timeout: 5000
      maximum-pool-size: 10
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT KEY, VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
          order: 1
  h2:
    console:
      enabled: true

As you can see, there is no spring.cloud.config.server.bootstrap=true so I don't know what it wants me to do.
I also don't want to use a git profile, so the Action doesn't help me at all.

Does someone know, how I can fix this issue? Thanks

Update: I added spring.cloud.config.server.git.uri and changed the order of jdbc to 1 and of git to 2.
After that it kind of worked but I still have the issue, that it won't read the configurations from the database
I started from scratch, did the same thing and then it worked fine


Solution

  • I was trying to debug your issue and I have following settings and could successfully start the server as well as query the API.

    My maven build config file

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/>
        </parent>
        <groupId>com.test</groupId>
        <artifactId>config-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>config-server</name>
        <description>Test Config Server</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>       
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.200</version>         
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</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>
    

    The main class

    @EnableConfigServer
    @SpringBootApplication
    public class ConfigserverApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigserverApplication.class, args);
        }
    
    }
    
    #bootstrap.yml
    spring:
      application:
        name: my-services-api
      datasource:
        url: jdbc:h2:mem:testdb
        driver-class-name: org.h2.Driver
        platform: h2
        hikari:
          connection-timeout: 5000
          maximum-pool-size: 10
      profiles:
        active: jdbc
      cloud:
        config:
          server:
            jdbc:
              sql: "SELECT properties.key, properties.value from properties where application=? and profile=? and label=?"
              order: 1
      h2:
        console:
          enabled: true
    

    Also I have put the schema.sql and data.sql into classpath ( resources folder of maven project) so that they autopopulate the data into embedded h2 database.

    enter image description here

    #schema.sql
    
    DROP TABLE IF EXISTS PROPERTIES;
        
    CREATE TABLE PROPERTIES (
      id INT AUTO_INCREMENT  PRIMARY KEY,
      APPLICATION VARCHAR(25) NOT NULL,
      PROFILE VARCHAR(25) NOT NULL,
      LABEL VARCHAR(25) DEFAULT NULL,
      KEY VARCHAR(25) NOT NULL,
      VALUE VARCHAR(200) NOT NULL   
    );
        
    #data.sql
        
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.1', 'prop1', 'val11');
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.1', 'prop2', 'val12');
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.1', 'prop3', 'val13');
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.0', 'prop1', 'val21');
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.0', 'prop2', 'val22');
    INSERT INTO PROPERTIES( APPLICATION, PROFILE, LABEL, KEY, VALUE) 
    values('myapp', 'MYCLIENT', '1.0', 'prop3', 'val23');
    

    enter image description here

    And I could successfully start and query the Config server

    enter image description here