Spring Cloud Config Client获取 Config Server配置服务器中的文件内容无法注入属性问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013233360/article/details/86666279

Config Client 客户端无法获取Config Server服务器端配置文件内容需注意两个方面问题。

Spring Cloud Config Server 配置文件以GitHub作为远程仓库。

1、配置文件命名

使用Spring Cloud Config Client 获取 Config Server 配置文件时,如果Config Server端口不是8888时,则Config Client的配置文件应该设置为bootstrap.yml,而不是application.yml,bootstrap.yml加载顺序优先于application.yml,否则会获取失败。

如果配置文件为application.yml,报错信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'serverPort' in value "${serverPort}"
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]

上图显示 无法解决占位符${serverPort},这是因为Config Client 客户端没有成功获取到Config Server 服务器(GitHub)上的配置文件信息。故而导致启动失败。

2、Config Server 服务端配置文件名称(也就是GitHub远程仓库的配置文件名称)

GitHub上的配置文件命名规则如下:

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

 /{application}/{profile}[/{label}]:

  ~: /{application}-{profile}.yml
  ~: /{label}/{application}-{profile}.yml
  ~: /{application}-{profile}.properties
  ~: /{label}/{application}-{profile}.properties

GitHub 远程服务器的配置文件需与客户端文件名称对应。如:{application}-{profile}.yml,则标识 Config-Client 客户端应用的名称 + 应用环境。 profile 可以设置为dev(开发环境)、test(测试环境)、pro(正式环境)。以当前客户端为例:GitHub所对应的配置文件为:eureka-config-client-dev.properties。

Config Client 客户端配置文件如下:

server:
  port: 7200
spring:
  application:
    name: eureka-config-client
  cloud:
    config:
      #指明仓库所属分支
      label: master
      #设置环境所属配置文件
      #dev开发环境配置文件
      #test测试环境
      #pro正式环境
      profile: dev
      #指明配置文件服务器地址
      uri: http://127.0.0.1:7100/


GitHub远程服务端的配置文件名称如下:

客户端配置属性必须与服务端GitHub远程配置文件名称保持一致。

客户端访问控制层如下:

最终访问结果如下:

成功获取到Config Server 服务器端的配置文件属性。

猜你喜欢

转载自blog.csdn.net/u013233360/article/details/86666279