Spring 带数据库后端的云配置服务器

Spring Cloud Config Server with Database backend

我正在尝试创建一个 Spring 云配置服务器,它从 database 而不是默认的 git-repo 获取配置。

每次我尝试 运行 我的配置服务器应用程序时,我都会收到此错误:

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

此消息来自服务器本身(重新格式化以便更好地阅读):

[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||||||| - |]]

我的application.yaml里面有这个部分:

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

如您所见,没有 spring.cloud.config.server.bootstrap=true 所以我不知道它要我做什么。
我也不想使用 git 配置文件,所以 Action 对我一点帮助都没有。

有人知道我该如何解决这个问题吗?谢谢

更新: 我添加了 spring.cloud.config.server.git.uri 并将 jdbc 的顺序更改为 1,将 git 的顺序更改为 2.
在那之后它有点工作但我仍然有问题,它不会从数据库中读取配置

我正在尝试调试你的问题,我有以下设置并且可以成功启动服务器以及查询 API。

我的 maven 构建配置文件

<?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>

主要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

我也将 schema.sql 和 data.sql 放入 class 路径(maven 项目的资源文件夹),以便它们将数据自动填充到嵌入式 h2 数据库中。

#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');

我可以成功启动和查询配置服务器

您的 application.yaml 文件丢失 属性: spring: 简介: 活动:jdbc

我已经为具有 JDBC 后端的云配置服务器实现了类似的项目,下面是参考: https://github.com/Santhoshinftech/cloud-config-custom-svc