领事首先引导与春季云配置 [英] consul first bootstrap with spring cloud config

查看:65
本文介绍了领事首先引导与春季云配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam使用spring-cloud-config进行集中配置,并使用consul进行服务发现。像eureka first bootstrap一样-spring是否支持consul first bootstrap,即在启动客户端服务时-我应该通过consul查找配置服务器。反之,则可以正常工作,即-在配置客户端bootstrap.properties中-我提供 spring.cloud.config.uri = http:// localhost:8888 ,它找到了配置服务器并从中提取配置。并在我的客户端应用程序的配置库中-我提供以下consul配置:

Iam using spring-cloud-config for centralized configuration and consul for service discovery. Like eureka first bootstrap - does spring support consul first bootstrap i.e on booting up a client service - I should look up the config server through consul. The otherway round works perfectly fine i.e - in config client bootstrap.properties - I provide the spring.cloud.config.uri=http://localhost:8888 which located the config server and pulls config from it. And in the config repository for my client application - I provide the consul config like :

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

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

However, when i try to use consul first bootstrap I am unable to read the properties from config server.

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

Client Application (for 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:

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

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;
      }
}

在领事K / V商店
文件夹结构config / demo
键/值: spring.cloud.config.uri = http:// localhost:8888

In the consul K/V store folder structure config/demo Key/Value : spring.cloud.config.uri=http://localhost:8888

配置服务器git repo:不为简洁起见添加配置服务器代码
demo.properties

Config server git repo: not adding the config server code for brevity demo.properties

server.port=9080
message=test

理想情况下,我首先实施领事的概念引导程序-我认为应该启动领事,客户端应使用 @EnableDiscoveryClien t批注以及在领事属性中标识自己-查找配置服务器url,并获取配置属性从服务器配置。但就我而言,服务已在领事中发现并注册,但我无法从配置服务器git repo中读取属性。

Ideally when I implement the concept of consul first bootstrap - I am thinking consul should be started and the client should identify itself using the @EnableDiscoveryClient annotation and in the consul properties - find config server url , and fetch the config properties from the server configurations. But in my case, service is being discovered and registered in consul but i am not able to read properties from config server git repo.

推荐答案

已在此处完成。它在SNAPSHOTS和RC2中可用,希望在下周发布。

It was done here. It is available in SNAPSHOTS and in RC2 which will come hopefully next week.

这篇关于领事首先引导与春季云配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆