Spring Boot 3配置中心客户端已配置URL仍访问localhost:8888求助
问题:Spring Boot 3启动时仍默认访问Config Server本地8888端口?
已为Spring Boot 3应用配置各环境对应的Config Server地址,但启动时仍尝试访问http://localhost:8888,若该端口被占用,应用会卡住。
日志信息
2024-03-20 14:32:22.078 INFO 18756 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888 2024-03-20 14:32:22.110 INFO 18756 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
依赖配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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>
当前application.yml配置
spring: application: name: my-app profiles: active: stage --- spring: cloud: config: label: dev config: import: configserver:http://myconfig:5232 activate: on-profile: stage --- spring: cloud: config: label: prod config: import: optional:configserver:http://config.abc.com activate: on-profile: prod ---
解决方案
1. 修正Profile激活配置的层级
核心问题是**activate: on-profile配置的层级错误**:你将其放在了spring.config节点下,导致对应环境的Config Server配置完全没有生效,应用只能使用默认的http://localhost:8888地址。
修正后的application.yml需将activate移至spring.profiles节点下:
spring: application: name: my-app profiles: active: stage --- spring: profiles: activate: on-profile: stage cloud: config: label: dev config: import: configserver:http://myconfig:5232 --- spring: profiles: activate: on-profile: prod cloud: config: label: prod config: import: optional:configserver:http://config.abc.com ---
2. 确认Bootstrap上下文启用
你已正确引入spring-cloud-starter-bootstrap依赖,这是Spring Boot 3中启用Bootstrap上下文的必要条件,请确保该依赖的版本与Spring Cloud版本匹配(Spring Cloud 2022.0.x系列适配Spring Boot 3)。
3. 验证配置生效状态
启动应用时,检查日志中是否包含The following profiles are active: stage(对应你激活的环境),确认目标Profile已正确加载。如果仍有问题,可添加JVM启动参数-Dspring.config.location=classpath:/application.yml强制指定配置文件路径。
4. 可选:禁用默认地址 fallback
若想彻底避免应用尝试访问默认的localhost:8888,可在主配置块中添加以下配置:
spring: cloud: config: uri: ""
这样当配置未正确加载时,应用会直接抛出错误而非尝试访问默认地址。
内容的提问来源于stack exchange,提问作者Viraj
相关产品推荐
相关产品推荐

