springcloud configClient获取configServer信息失败导致启动configClient注入失败报错解决

目录

一、问题现象

二、解决方案

三、运行结果

四、代码地址


一、问题现象

springcloud configClient获取configServer信息失败导致启动configClient注入失败

报错堆栈信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.testController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"

二、解决方案

经排查,原因为gitee配置文件名config-dev.properties与服务注册名不一致导致,bootstrap.yml配置了configServer地址之后,configClient从gitee中获取配置文件的时候指定了profile(dev哪个环境)以及label(newBranch4分支),但是configServer获取配置文件的时候会批量加载所有分支的配置文件信息,因此configClient和configServer配置文件关联必须由服务名以及文件名关联,因此在修改gitee中配置文件的文件名为服务名之后,configClient启动成功,并成功获取gitee中相关配置信息。

gitee中配置文件内容

git clone https://gitee.com/fzggz/springcloud.git

configServer工程的bootstrap.yml

复制代码
spring:
  profiles:
    active: dev
  application:
    name: config-server
  cloud:
    config:
      label: newBranch
      server:
        git:
          uri: https://gitee.com/fzggz/springcloud
          searchPaths: spring-cloud-config-file
          timeout: 60
          username: ***
          password: ***
          default-label: newBranch
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
bobo:
  user:
    name: bobo
    age: 18

configClient的bootstrap.yml

复制代码
spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://127.0.0.1:8030  # 直接连接 Config-Server
      profile: dev
      label: newBranch3
      discovery:
        enabled: false
#        enabled: true
#        service-id: config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

三、运行结果

configClient配置客户端代码,启动configClient后,读取configServer中读取到的配置文件信息,通过文件名,profile,label等映射关系找到version,通过@Value注入version,controller中获取

复制代码
package com.ggz.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ggz on 2025/4/1
 */
@RefreshScope
@RestController
public class TestController {

    @Value("${version}")
    private String version;

    @RequestMapping("/version")
    public String version() {
        return version;
    }
}

四、代码地址

直接导入,启动eureka和configServer以及configClient即可

https://github.com/GeGuozhi/SpringCloudDemo.git

希望有用的同时大家能帮忙能点亮一个小小的star

相关推荐
程序猿阿越1 小时前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序员爱钓鱼1 小时前
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
开发语言·后端·golang·gin
Jiude1 小时前
MinIO 社区版被故意阉割,Web管理功能全面移除。我来试试国产RustFS
后端·docker·架构
仰望星空@脚踏实地1 小时前
Spring Boot Web 服务单元测试设计指南
spring boot·后端·单元测试
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
羊小猪~~2 小时前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
用户8324951417322 小时前
JAVA 版本多版本切换 - 傻瓜式操作工具
后端
estarlee2 小时前
随机昵称网名API接口教程:轻松获取百万创意昵称库
后端
明天好,会的2 小时前
跨平台ZeroMQ:在Rust中使用zmq库的完整指南
开发语言·后端·rust
追逐时光者2 小时前
C#/.NET/.NET Core优秀项目和框架2025年6月简报
后端·.net