consul first bootstrap with spring cloud config

2019-04-12 17:09发布

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

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

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:

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

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

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

server.port=9080
message=test

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.

2条回答
Summer. ? 凉城
2楼-- · 2019-04-12 17:36

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

查看更多
相关推荐>>
3楼-- · 2019-04-12 17:39

Giving my sample code here for benefit of others. I had to do a lot of tinkering with the properties file to get to this. As answered by @spencergibb it is available in SNAPSHOT only for now.

This time i did not use any key value properties in consul. config server code: pom.xml:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</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>
 </dependency>

application.yml

 spring:
    profiles:
     active: native
    cloud:
     config:
       server:
        native:
          search-locations: file://${HOME}/properties 
    consul:
      port: 8500
      host: localhost
      enabled: true
      discovery:
        enabled: true
        register: true
        service-name: server --registers in consul as server instead of config-server 
        hostname: localhost
server:
  port: 8888

bootstrap.yml ::

spring:
  application:
    name: config-server

COnfigServerApplication.java

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class SpringConfigServerApplication {
public static void main(String[] args) {
  SpringApplication.run(SpringConfigServerApplication.class, args);
 }
}

Client microservice: demo

<parent>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-parent</artifactId>
   <version>Brixton.BUILD-SNAPSHOT</version>
 <!--   <version>Brixton.M5</version> -->
   <relativePath /> 
 </parent>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-consul-all</artifactId>
 </dependency>

bootstrap.properties

spring.application.name=demo-spring-cloud-sleuth
spring.cloud.config.failFast=true
spring.cloud.config.retry.maxAttempts=20
spring.cloud.config.retry.initialInterval=3000
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.consul.discovery.hostName=localhost
spring.cloud.consul.discovery.register=true -- unless this is there, the service fails to register in consul.

git uri property file for client:

server.port=9082
message=message local
foo1=bar
查看更多
登录 后发表回答