gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:13:49 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com