consul first bootstrap with spring cloud config

随声附和 提交于 2019-12-04 17:28:23

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!