如何使spring boot客户端的application.yml从配置服务器获取值

时间:2018-03-02 08:04:12

标签: spring spring-boot spring-config

有没有办法让spring cloud config客户端的application.yml从spring配置服务器读取值? 例如, 在我的spring cloud配置客户端上,application.yml就是这样的

spring:
  application:
  name: clienttest
mvc:
view:
  prefix: /jsp/
  suffix: .jsp


server: 
  port: 8080
  context-path: /clienttest
  tomcat:
   uri-encoding: UTF-8

eureka:
 client:
   service-url: {"defaultZone":"http://dev.euraka01.app.com:8769/eureka/,http://dev.euraka02.app.com:8770/eureka/"}
instance:
 prefer-ip-address: true

和我的bootstrap.yml文件如下

spring:
  application:
    name: clienttest
  cloud:
    config:
      name: clienttest
      uri: http://192.168.2.101:9000
      enabled: true
      profile: out_test
      label: master

现在对于service-url值,对于不同的环境,我必须配置不同的eureka url值,我的问题是,无论如何我是否可以在配置服务器中配置service-url值?就像我在application.yml中将值设置为$ {service-url}一样,当我启动配置客户端服务器时,它会根据我在bootstrap.yml中设置的配置文件和标签从配置服务器获取值。 / p>

2 个答案:

答案 0 :(得分:1)

您可以通过配置文件和标签在配置服务器上查找属性,其中label是分支,标记。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

在上面的示例中,您的配置服务器将尝试查找名为

的文件
clienttest-out_test.properties

在主分支上的git repo中。

spring:
  application:
    name: clienttest
  cloud:
    config:
      profile: out_test
      label: master

请参阅example以及好的文档here

答案 1 :(得分:0)

Essex Boy,  非常感谢您的帮助,我之前能够从不同的配置文件中读取值。 我的问题是如何使application.yml从配置服务器获取值,现在我自己解决了,答案很简单,在应用程序中,设置值如$ {service-url},完整答案如下:

在我的application.yml中,内容如下:

spring:
  application:
  name: clienttest

server: 
  port: 8080
  context-path: /clienttest
  tomcat:
   uri-encoding: UTF-8

eureka:
 client:
   service-url:     {"defaultZone":"${service-url}"}
instance:
 prefer-ip-address: true

请注意service-url值,现在值设置为{" defaultZone":" $ {service-url}"},并在我的application.properties文件中在配置服务器上,属性文件内容如下:

service-url=http://192.168.2.101:8769/eureka/,http://192.168.2.101:8770/eureka/

然后当我启动mocroservice时,它可以在http://192.168.2.101:8769/eureka/http://192.168.2.101:8770/eureka/

上自我抵制

这就是我想要的结果。

相关问题