我的springboot应用程序中具有jdbc后端的嵌入式Spring Cloud配置服务器

时间:2019-08-22 23:31:30

标签: spring-cloud-config

我们需要从数据库中读取应用程序属性。为此,我正在尝试使用Spring Cloud。我试图将配置服务器嵌入到具有JDBC后端的Spring Boot应用程序中,以便该应用程序可以通过从DB读取属性来进行自我配置。但是我无法配置bootstrap.properties文件。我收到以下错误:-

identical(a1, a2)

我的bootstrap.properties文件

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)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)

问题是,当我们使用JDBC作为后端来加载所有属性时,为什么需要配置git存储库? 还有如何配置它来使我的JDBC配置工作。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

为了使jdbc正常工作,您需要在application.yml文件中具有以下属性:

spring:
  cloud:
    config:
        server:
            jdbc:
                sql: SELECT KEY, VALUE FROM CONFIG_PROPERTIES WHERE APPLICATION=? AND PROFILE=? AND LABEL=?
            bootstrap: true
datasource:
    url: datasourcehere
    username: username
    password: password
    driver-class-name: oracle.jdbc.OracleDriver
profiles:
    active: jdbc

和Application.java如下:

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@Import({ JdbcEnvironmentRepository.class })
@RestController
public class Application {

    public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
    }


}

祝你好运

答案 1 :(得分:0)

在我的情况下,我需要 META-INF/spring.factories 在初始化 spring-cloud-config 上下文期间将这些内容与数据源 && jdbcTemplate 一起运行(类的顺序很重要 >)

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

答案 2 :(得分:0)

我设法将我的应用程序设置为没有端点和 JDBC 后端的嵌入式云配置服务器。

我希望通过发布我的工作配置来帮助他人:

pom.xml 中我添加了:

<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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
    
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc8</artifactId>
    <scope>compile</scope>
</dependency>

对于 spring 依赖项,我使用 2.2.2.RELEASE 版本。 正如另一个答案所建议的,为了让 Spring 自动配置 JDBCRepository,我必须添加 src/main/resources/META-INF/spring.factories 包含:

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

在没有端点的嵌入式云配置服务器的 Spring 文档之后,我添加了 src/main/resources/bootstrap.yml 包含:

spring:
  application:
    name: myapplication
  profiles:
    active: composite
  cloud:
    config:
      server:
        bootstrap: true
        composite:
        - 
          type: jdbc
          sql: SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
          order: 1
      name: myapplication
  datasource:
    url: mydatabaseurl
    driver-class-name: oracle.jdbc.OracleDriver
    username: mydatabaseusername
    password: mydatabasepassword

我不确定是否需要定义两次“myapplication”。在 PROPERTIES 表中,我有 APPLICATION = myapplication、PROFILE = Composite 和 LABEL = master 的行。

我没有使用 Spring Boot Application,而是定义了一个实现 SpringBootServletInitializer 的类,并且我没有使用任何“@SpringBootApplication”、“@EnableConfigServer”、“@EnableDiscoveryClient”或“@Import({JdbcEnvironmentRepository.class })” .

您可能需要在 src/main/resources/application.properties 中设置 allow-bean-definition-overriding=true。

我希望这有助于有人将 Cloud Config 设置为没有端点的嵌入式服务器。