You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Spring Config Server与Client的示例不清晰且无法正常运行?

可直接运行的Spring Cloud Config 完整配置示例
  • 官方验证稳定适配版本组合:Spring Boot 2.7.12 + Spring Cloud 2021.0.8,不存在依赖冲突问题

1. Config Server 端配置

1.1 Maven核心依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.12</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.8</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

1.2 启动类配置

在启动类上加@EnableConfigServer注解即可:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }
}

1.3 application.yml 配置

支持本地/远程Git仓库,按实际情况替换参数即可:

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # 本地仓库示例格式:file:/Users/xxx/config-repo,远程仓库填Git地址即可
          uri: 你的Git仓库地址
          default-label: main
          # 配置文件在仓库中的子目录,根目录可不填
          search-paths: config
          # 私有仓库填写对应的账号密码
          # username: 你的Git账号
          # password: 你的Git密码

在配置仓库中新建config-client-dev.yml写入测试配置:

test:
  msg: hello from config server

2. Config Client 端配置

2.1 Maven核心依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.12</version>
    <relativePath/>
</parent>
<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>
    <!-- 2021.0.x版本必须额外引入该依赖才能加载bootstrap配置 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.8</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.2 bootstrap.yml 配置

配置中心相关配置必须放在bootstrap.yml中,优先级高于application.yml:

server:
  port: 8080
spring:
  application:
    # 和配置文件前缀保持一致,对应config-client-dev.yml的config-client段
    name: config-client
  profiles:
    # 和配置文件环境标识保持一致,对应config-client-dev.yml的dev段
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      label: main

2.3 测试代码

编写接口验证配置读取效果:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Value("${test.msg}")
    private String testMsg;

    @GetMapping("/test")
    public String getTestMsg() {
        return testMsg;
    }
}

运行验证步骤

  • 先启动Config Server,访问http://localhost:8888/config-client/dev/main,返回配置内容则服务端运行正常
  • 再启动Config Client,访问http://localhost:8080/test,返回hello from config server则整体配置生效

内容的提问来源于stack exchange,提问作者Akshat M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 06:18:01