首页 文章

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

提问于
浏览
2

反正有没有让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中将值设置为$ 一样,当我启动配置客户端服务器时,它会根据我在bootstrap.yml中设置的配置文件和标签从配置服务器获取值 .

2 回答

  • 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

  • 0

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

    在我的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”:“$ ”},在我的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/上自我抵抗

    这就是我想要的结果 .

相关问题