2016-04-14 40 views
2

Iam使用spring-cloud-config进行集中配置和领事进行服务发现。像尤里卡第一引导程序 - 春支持领事第一引导,即启动客户端服务 - 我应该通过领事查找配置服务器。另一方面工作完全正常,即 - 在配置客户端bootstrap.properties - 我提供spring.cloud.config.uri = http://localhost:8888它位于配置服务器并从它拉配置。而在我的客户端应用程序的配置库 - 我提供领事配置,如:领事第一bootstrap与春天云配置

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

但是,当我尝试使用领事首先引导我无法读取配置服务器的属性。

客户端应用程序(用于领事第一自举):

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

在领事ķ/ V存储 夹结构配置/演示 关键/值:spring.cloud.config.uri=http://localhost:8888

配置服务器混帐回购协议:不添加配置服务器代码简洁 demo.properties

server.port=9080 
message=test 

理想的情况下,当我执行领事首先引导的概念 - 我想领事应启动和客户端应该使用@EnableDiscoveryClien t注释和consul属性来标识自身 - 找到配置服务器url,并从服务器配置中获取配置属性。但在我的情况下,服务正在发现和注册领事,但我无法从配置服务器git回购的属性。

回答

2

它已完成here。它在SNAPSHOTS和RC2中可用,下周将有望出现。

0

给我的示例代码在这里为他人的利益。我不得不做很多修改属性文件来达到此目的。 正如@spencergibb所回答的,目前仅在SNAPSHOT中可用。

这次我没有在领事中使用任何键值属性。 配置服务器代码: 的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); 
} 
} 

氯客户微服务:演示

<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。客户端性能

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. 

混帐uri属性文件:

server.port=9082 
message=message local 
foo1=bar