Spring Boot Config Client连接Config Server时遇连接拒绝错误求助
先看你给出的错误日志:
Fetching config from server at : http://localhost:8888 2023-05-15T10:29:29.732+05:30 INFO 14600 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Exception on Url - http://localhost:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/application/default": Connection refused: no further information. Will be trying the next url if available 2023-05-15T10:29:29.732+05:30 WARN 14600 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused: no further information
核心问题是Client无法连接到localhost:8888的Config Server,按以下步骤排查解决:
确认Config Server是否正常启动:
查看Server的启动日志,检查是否有启动异常,是否成功在8888端口监听。直接用浏览器或Postman访问http://localhost:8888/application/default,如果无法访问,优先解决Server的启动问题。核对端口配置一致性:
检查Config Server的配置文件(application.yml/properties)中server.port是否为8888;再确认Client的bootstrap.yml/properties里spring.cloud.config.uri是否指向http://localhost:8888,避免端口写错。排查本地端口占用:
8888端口可能被其他程序占用,Windows执行netstat -ano | findstr :8888,Linux/Mac执行lsof -i :8888查看占用进程,杀掉占用进程或修改Config Server的端口。验证Config Server基础配置:
确保Server的依赖管理文件(pom.xml/gradle)中添加了spring-cloud-config-server依赖;启动类上是否标注了@EnableConfigServer注解;配置仓库(Git/本地文件)的路径、权限是否正确,避免因仓库加载失败导致Server启动异常。检查Client配置加载逻辑:
Spring Boot 2.4之前版本,Client的Config Server配置必须放在bootstrap.yml/properties中,不能放在application.yml;2.4+版本,要么引入spring-cloud-starter-bootstrap依赖继续使用bootstrap配置,要么在application.yml中添加spring.config.import: configserver:启用配置中心导入。
内容的提问来源于stack exchange,提问作者Aravind A

