springboot启动配置文件bootstrap.yml无法加载,导致无法使用配置中心拉取配置启动项目的解决方法

springboot启动配置文件bootstrap.yml无法加载,导致无法使用配置中心拉取配置启动项目的解决方法

使用springcloud开发项目,需要从配置中心根据需求拉取环境配置,最近在本地开发环境进行调试时,突然发现无法启动项目工程,报错如下:

2019-03-21 15:00:58.392  INFO 155060 --- [  main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2019-03-21 15:00:59.461  INFO 155060 --- [  main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2019-03-21 15:00:59.461  WARN 155060 --- [  main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/dev": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
2019-03-21 15:00:59.462  INFO 155060 --- [  main] c.c.cldmp.report.ReportApplication       : The following profiles are active: dev
2019-03-21 15:00:59.471  INFO 155060 --- [  main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1fb19a0: startup date [Thu Mar 21 15:00:59 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5bda8e08

bootstrap.yml文件的配置如下:

server:
  port: 19019
spring:
  application:
    name: report
  cloud:
    config:
      username: sky
      password: xxx
      uri: http://11.193.166.30:19022

正常启动,项目应该前往http://11.193.166.30:19022配置中心这个地址去获取不同的配置,如获取我的dev环境配置,但错误提示了Fetching config from server at : http://localhost:8888,这明显是不对的,http://localhost:8888查看源码可知这是配置中心的默认地址.

 {
      "sourceType": "org.springframework.cloud.config.client.ConfigClientProperties",
      "defaultValue": [
        "http:\/\/localhost:8888"
      ],
      "name": "spring.cloud.config.uri",
      "description": "The URI of the remote server (default http:\/\/localhost:8888).",
      "type": "java.lang.String[]"
    },
  /**
	 * The URI of the remote server (default http://localhost:8888).
	 */
	private String[] uri = { "http://localhost:8888" };  

问题解决

刚开始,我以为bootstrap.yml配置文件里的参数填写有误,一直在修改配置文件里的参数,但没有解决问题.后来想到会不会是因为不同路径下配置文件的优先级不同,造成我的配置文件被其它配置文件所覆盖,导致无法加载我的配置文件.SpringBoot配置文件可以放置在多种路径下,不同路径下的配置优先级有所不同。配置文件可放置目录(优先级从高到低)如下:

file:./config/ (当前项目路径config目录下);
file:./ (当前项目路径下);
classpath:/config/ (类路径config目录下);
classpath:/ (类路径config下).

并没有发现其它配置文件.debug跟踪项目的启动情况,发现项目启动过程中根本没有读取到配置,这到底是什么原因呢?
一步步排查,突然发现bootstrap.yml文件在打包后存放路径有问题,classpath根路径下面没有bootstrap.yml,而是放在了BOOT-INF/classes这个路径下.
在这里插入图片描述

查看项目的pom文件,发现在resources配置中的部分代码如下:

<resource>
       <directory>src/main/resources</directory>
       <targetPath>BOOT-INF/classes/</targetPath>
</resource>

由此发现问题,是资源文件的打包路径出错,因代码是从git上拉取下来,此部分代码被其他同事修改,造成我在本地启动工程出现了启动失败的问题.将上述代码删除,项目顺利启动成功.查看打包后bootstrap.yml路径,bootstrap.yml路径已正确存放在classpath根路径下,如下图显示:
在这里插入图片描述

发布了11 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/foundtime/article/details/88718052