当前位置: 首页 > news >正文

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

http://www.dtcms.com/a/107293.html

相关文章:

  • 安徽京准:GPS北斗卫星校时服务器助力大数据云计算
  • 《Linux内存管理:实验驱动的深度探索》大纲
  • 项目练习:若依系统二次开发中,某些情况下,v-hasPermi不适合的解决办法
  • DoDAF科普
  • Python入门(5):异常处理
  • 蓝桥杯练习:对称二叉树
  • 全国产FMC子卡-16bit 8通道2.4G
  • Leetcode 6207 -- DP | 思维 | 双指针
  • catch-all路由
  • 数据结构初阶: 顺序表的增删查改
  • 【LeetCode Solutions】LeetCode 126 ~ 130 题解
  • Selenium自动化中的 三大时间等待
  • gcc 链接顺序,静态库循环依赖问题
  • 「青牛科技」GC5849 12V三相无感正弦波电机驱动芯片
  • RISC-V debug专栏2 --- Debug Module(DM)
  • 在将asc文件导入maxent文件时出现for input string:“nan“
  • (kotlin) Android 13 高版本 图片选择、显示与裁剪功能实现
  • Docker容器部署Java项目的自动化脚本(Shell编写)
  • 动态规划练习题①
  • 蓝桥杯 web 灯的颜色变化(Dom操作及样式动态修改、浏览器解析顺序、定时器)
  • 计算机科学基础设施之数学:科研工具、资源与环境详介
  • qt.qpa.xcb: could not connect to display解决方法
  • Keil5烧录后STM32不自动运行?必须复位才能启动的终极解决方案
  • element-plus中,Upload上传组件的使用 + 后端处理
  • DMA在SPI和I2C通信中的应用详解
  • 解锁异步编程新姿势:CompletableFuture 深度探秘
  • java根据集合中对象的属性值大小生成排名
  • [NOIP 1999 提高组] 导弹拦截
  • C++ STL简单的几个容器
  • I²C总线高级特性与故障处理分析