无法在 spring-boot 应用程序中从 Consul 读取配置

Posted

技术标签:

【中文标题】无法在 spring-boot 应用程序中从 Consul 读取配置【英文标题】:Not able to read configuration from Consul in spring-boot application 【发布时间】:2018-05-13 23:05:32 【问题描述】:

我正在创建一个Spring Boot 应用程序,它将从Consul 读取数据库属性等配置。但我无法使用我的应用程序从 Consul 读取键值。以下是我正在尝试做的事情。

**pom.xml**
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tuturself</groupId>
    <artifactId>spring-boot-consul</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.retry.version>1.2.1.RELEASE</spring.retry.version>
        <consul.version>1.1.2.RELEASE</consul.version>
        <consul.discovery.version>1.1.2.RELEASE</consul.discovery.version>
        <jackson.version>2.8.1</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-consul-discovery</artifactId>
            <version>$consul.discovery.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>$spring.retry.version</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>$jackson.version</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>$jackson.version</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-consul-dependencies</artifactId>
                <version>1.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

以下是我的主要课程:

@EnableRetry
@RefreshScope
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.test.*")
public class SpringBootConsulApplication 

    private static ConsulConfiguration consulConfiguration;

    public static void main(String[] args) 
        try 
            String consulHost = System.getProperty("spring.cloud.consul.host");
            System.out.println("consulHost ::" + consulHost);
            String consulPort = System.getProperty("spring.cloud.consul.port");
            System.out.println("consulPort ::" + consulPort);
            String consulPrefix = System.getProperty("spring.cloud.consul.config.prefix");
            System.out.println("consulPrefix ::" + consulPrefix);
            new SpringApplicationBuilder(SpringBootConsulApplication.class).web(true).run(args);
         catch (Exception ex) 
            ex.printStackTrace();
        
    

我正在使用@Value 注释读取领事属性:

@Configuration
@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)
public class ConsulConfiguration 

    @Value("$cassandra.host")
    private String cassandraHost;

    @Value("$cassandra.user")
    private String userName;

    @Value("$cassandra.password")
    private String password;

我的 bootstrap.yml 在资源文件夹中:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      enabled: true
      config:
        enabled: true
        prefix: config/application
        defaultContext: apps
        profileSeparator: '::'
  application:
    name: spring-boot-consul

Consul 在我的本地系统 localhost:8500 上启动并运行,我有文件 config/application/spring-boot-consul.yml 文件;

spring:
  application:
    name: spring-boot-consul
cassandra:
  host: 127.0.0.1:9042,127.0.0.2:9042
  user: my_user
  password: my_pass
  pooling:
    maxThread: 10
    timeout: 50
  keyspace:
    name: test_keyspace
    readConsistency: ONE
    writeConsistency: ONE

当我对应用程序进行分层时,它显示无法在我的 ConsulConfiguration 类中绑定 cassandra.host。从而停止应用程序。任何提示,我在这里做错了什么?

【问题讨论】:

您是否尝试按照文档在 bootstrap.yml 文件中设置 spring.cloud.consul.config.format? 你可以通过从配置类中删除注释来检查@EnableConfigurationProperties(PropertySourceBootstrapProperties.class) 你是如何开始你的领事代理的? 【参考方案1】:

您可以找到一个工作示例here。

Consul 配置 KV 存储

您需要通过 Consul UI 或命令行将属性存储在 Consul KV 存储中。 Consul Agent 不会从文件系统加载您的属性。要从命令行加载属性,您可以在 Consul Agent 启动并运行后使用以下命令。可以通过在文件名前加上 @ 符号来从文件中读取 YAML 数据。

./consul kv put config/application/data @spring-boot-consul.yml

其中config/application/data 是键名。

如果数据成功写入KV,应该会得到如下响应,

Success! Data written to: config/application/data

您还可以使用以下命令从 KV 中获取属性,

$ ./consul kv get config/application/data
cassandra:
  host: 127.0.0.1:9042,127.0.0.2:9042
  user: my_user
  password: my_pass

您还可以从 Consul Web UI 查看属性,

对 bootstrap.yml 的更改

您需要稍微修改您的bootstrap.yml。以下是更改:

prefix 值为 config defaultContext 值为applicationformat 添加到yaml

data 的名称添加data-key 以获取YAML blob。

spring:
  profiles: default
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true
        prefix: config
        defaultContext: application
        data-key: data
        profileSeparator: '::'
        format: yaml
  application:
    name: spring-boot-consul

对 ConsulConfiguration 的更改

@Configuration
@RefreshScope
public class ConsulConfiguration 

    @Value("$cassandra.host")
    private String cassandraHost;

    @Value("$cassandra.user")
    private String userName;

    @Value("$cassandra.password")
    private String password;

    @PostConstruct
    public void postConstruct() 
        // to validate if properties are loaded
        System.out.println("** cassandra.host: " + cassandraHost);
        System.out.println("** cassandra.user: " + userName);
        System.out.println("** cassandra.password: " + password);
    

对应用程序类的更改,

@EnableRetry
@RefreshScope
@EnableDiscoveryClient
@EnableAutoConfiguration
@EnableConfigurationProperties
@SpringBootApplication
@ComponentScan("com.test.*")
public class SpringBootConsulApplication 

    public static void main(String[] args) 
        ...
    


【讨论】:

以上是关于无法在 spring-boot 应用程序中从 Consul 读取配置的主要内容,如果未能解决你的问题,请参考以下文章

为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?

在 C# 中从 Access 数据库存储和检索图像

spring-boot 无法在 docker 中启动

无法从 spring-boot 应用程序中调出 swagger-ui

Thymeleaf 无法检测到 spring-boot 项目中的模板

Spring-boot,无法自动装配类。未找到默认构造函数引发异常