spring-cloud-config:配置文件无法正常工作

时间:2018-09-23 20:59:34

标签: java spring spring-cloud spring-cloud-config

我正在学习spring-cloud-config,我想使用其他配置文件,但是我总是从default配置文件中获取值。

我在git信息库中有两个属性文件,后缀为prod的文件会覆盖default配置文件中的键:

  • image-service.properties
  • image-service-prod.properties

看来我的配置服务器工作正常:

获取http://localhost:8888/image-service/default

{"name":"image-service","profiles":["default"],"label":null,"version":"fb78fe4429a33c266d6eb07a9e482b8fd264dd7c","state":null,"propertySources":
[{"name":"https://bitbucket.org/.../...-configuration.git/image-service.properties","source":{"service.image.hello":"image-common"}}]}

获取http://localhost:8888/image-service/prod

{"name":"image-service","profiles":["prod"],"label":null,"version":"fb78fe4429a33c266d6eb07a9e482b8fd264dd7c","state":null,"propertySources":
[{"name":"https://bitbucket.org/.../...-configuration.git/image-service-prod.properties","source":{"service.image.hello":"image-prod"}},
{"name":"https://bitbucket.org/.../...-configuration.git/image-service.properties","source":{"service.image.hello":"image-common"}}]}

我在REST应用程序中激活了prod配置文件,但始终显示默认配置文件中的值。

客户端应用程序的

application.properties:

server.port=8889
spring.application.name=image-service

spring.cloud.config.uri=http://localhost:8888
spring.profiles.active=prod

客户端应用程序的REST控制器:

@RefreshScope
@RestController
public class EchoController {

    @Value("${service.image.hello}")
    private String hello;

    @RequestMapping("/show")
    @ResponseBody
    public String showConfig() {
        return new StringBuilder()
                .append("image-service: ").append(hello)
                .toString();
    }
}

结果:

image-service: image-common

从客户端应用登录:

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
c.c.c.ConfigServicePropertySourceLocator : Located environment: name=image-service, profiles=[default], label=null, version=fb78fe4429a33c266d6eb07a9e482b8fd264dd7c, state=null
b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://bitbucket.org/.../...-configuration.git/image-service.properties'}]}
c.r.d.springconfig.client.Application    : The following profiles are active: prod
ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7ccdc9e7: startup date [Sun Sep 23 22:31:55 CEST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7fd618b5
o.s.cloud.context.scope.GenericScope     : BeanFactory id=51790958-c0a2-3d61-91d6-a8dcd5395c7e
trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$bf37cae6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8889 (http)

似乎我错过了一些东西,但看不到。

1 个答案:

答案 0 :(得分:0)

我也做同样的事情,并对如何在Spring Cloud Config服务器配置中传递版本有疑问

我知道我们可以通过REST端点进行访问

https:// {domain} / v1 / secret / data / myapp / dev?version = 2

想知道如何在Spring Cloud Config Server +客户端实现中配置它。