spring-boot - consul first bootstrap with spring cloud 配置

标签 spring-boot config spring-cloud service-discovery consul

我使用 spring-cloud-config 进行集中配置,并使用 consul 进行服务发现。就像 eureka first bootstrap 一样——spring 是否支持 consul first bootstrap,即在启动客户端服务时——我应该通过 consul 查找配置服务器。另一轮工作得很好,即 - 在配置客户端 bootstrap.properties - 我提供了 spring.cloud.config.uri= http://localhost:8888它找到了配置服务器并从中提取配置。在我的客户端应用程序的配置存储库中 - 我提供了 consul 配置,如:

spring.cloud.consul.host=localhost , 
spring.cloud.consul.port=8500

但是,当我尝试使用 consul first bootstrap 时,我无法从配置服务器读取属性。

客户端应用程序(consul first bootstrap):

pom.xml
<parent>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-parent</artifactId>
      <!-- <version>Brixton.BUILD-SNAPSHOT</version> -->
      <version>Brixton.M5</version>
      <relativePath /> 
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-consul-config</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-starter-zuul</artifactId>
        </dependency>
</dependencies>

bootstrap.properties:

spring.application.name=demo
spring.cloud.config.failFast=true
spring.cloud.config.retry.maxAttempts=20
spring.cloud.config.retry.initialInterval=3000
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

DemoApplication.java

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class DemoSleuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoSleuthApplication.class, args);
    }
}
@RefreshScope
@RestController
class RestAPIController 
{
    @Value(value = "${server.port}")
    String port;

    @Value(value = "${message}")
    String message;

@RequestMapping("/message")
      public String welcome(){
          String s = this.restTemplate.getForObject("http://localhost:"+this.port+"/message", String.class);
          return this.message + s;
      }
}

在consul K/V store 文件夹结构配置/演示 键/值:spring.cloud.config.uri=http://localhost:8888

配置服务器 git repo:为简洁起见未添加配置服务器代码 演示.properties

server.port=9080
message=test

理想情况下,当我实现 consul first bootstrap 的概念时 - 我认为 consul 应该启动并且客户端应该使用 @EnableDiscoveryClient 注释和在 consul 属性中识别自己 - 找到配置服务器 url ,并从服务器配置中获取配置属性。但就我而言,正在发现服务并在 consul 中注册,但我无法从配置服务器 git repo 读取属性。

最佳答案

完成了here .它在 SNAPSHOTS 和 RC2 中可用,有望在下周推出。

关于spring-boot - consul first bootstrap with spring cloud 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629943/

相关文章:

java - 自动发送数据到客户端,无需API调用

git - Repository Migration to Gerrit,git push --mirror 返回refs/meta/config(不能删除项目配置)

java - 由于开头字符无效,Feign 客户端无法将响应 json 转换为 Java 对象

mercurial - 我可以以编程方式设置 Mercurial 配置选项吗?

python - 使用python的带有字典的配置文件

spring - 使用 Spring Cloud FeignClients 时出现 404

spring-cloud - 在没有 spring boot 的 Web 应用程序中使用 spring cloud config

java - 导入 javax.annotation.PostConstruct 无法解析

javascript - 通过 REST API 响应 native /Spring Boot,值为 0 或 null

独立 servlet 容器中的 Spring Boot 应用程序 war