Spring Cloud Config客户端连接拒绝、服务端启动报错问题求助
异常现象
- 客户端启动后访问接口仅返回
MessageController中设置的默认值 - 客户端日志报错:
Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused; nested exception is java.net.ConnectException: Connection refused
- 服务端将
application.yml改为bootstrap.yml后启动报错:
If you are using the git profile, you need to set a Git URI in your configuration. If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.
Config Server端配置
pom.xml
<properties> <java.version>11</java.version> <spring-cloud.version>2020.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml
server: port: 9090 spring: cloud: config: server: git: uri: https://github.com/TEK-Leads/Config-Properties.git clone-on-start: true bootstrap: true enabled: true application: name: Config-Server management: security: enabled: false
启动类代码
package com.ramgovindhare.configserverapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerAppApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerAppApplication.class, args); } }
Config Client端配置
pom.xml
<properties> <java.version>11</java.version> <spring-cloud.version>2020.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml
spring: application: name: client-config cloud: config: uri: http://localhost:9090 config: activate: on-profile: active:prod management: security: enabled:false
MessageController代码
package com.ramgovindhare.configserverclientapp.contorller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class MessageController { @Value("${msg:Config Server is not working. Please check..}") private String msg; @GetMapping("/msg") public String getMsg() { return this.msg; } }
问题原因与修复方案
问题根因
- 客户端默认请求8888端口:Spring Cloud 2020.x版本后引导上下文配置加载优先级更高,你将Config Server地址写在
application.yml中,引导阶段读取不到配置,就会走默认的8888端口。 - 服务端改bootstrap.yml启动报错:
spring.cloud.config.server.bootstrap=true是服务端自身需要从复合配置源读取配置时才启用的参数,你没有配置复合配置源,开启后就会触发异常,服务端无需使用bootstrap.yml。 - 客户端配置格式错误:profile激活配置位置错误、冒号后缺少空格,导致配置无法被正确解析。
修复步骤
1. 修复Config Server配置
删除application.yml里的spring.cloud.config.server.bootstrap=true配置,修正后配置如下:
server: port: 9090 spring: cloud: config: server: git: uri: https://github.com/TEK-Leads/Config-Properties.git clone-on-start: true enabled: true application: name: Config-Server
启动服务端后访问http://localhost:9090/client-config/prod,能返回Git仓库配置说明服务端正常。
2. 修复Config Client配置
新建bootstrap.yml,把Config Server地址和profile激活配置移到该文件中,内容如下:
spring: application: name: client-config profiles: active: prod cloud: config: uri: http://localhost:9090
保留application.yml仅放剩余配置,修正格式错误:
management: security: enabled: false
3. 验证
先启动Config Server,完全启动后再启动Config Client,访问客户端/msg接口即可获取Git仓库中配置的msg值,不会再返回默认值。
内容的提问来源于stack exchange,提问作者Ng Sharma

