【Spring Cloud】配置中心详细介绍及使用

目录

[1. Spring Cloud Config 简介](#1. Spring Cloud Config 简介)

功能特点:

[2. 配置中心的架构](#2. 配置中心的架构)

[3. 配置中心的实现步骤](#3. 配置中心的实现步骤)

[3.1 搭建 Config Server](#3.1 搭建 Config Server)

[3.2 搭建 Config Client](#3.2 搭建 Config Client)

[4. 测试和运行](#4. 测试和运行)

[5. 实现配置刷新](#5. 实现配置刷新)

[6. 结论](#6. 结论)


Spring Cloud 配置中心(Spring Cloud Config)是用于管理分布式系统中的外部配置的解决方案。它提供了服务器端和客户端支持,用于集中化的外部配置。以下是对Spring Cloud配置中心的详细介绍及使用代码的详细介绍。

1. Spring Cloud Config 简介

Spring Cloud Config 提供了对分布式系统的外部配置支持,配置服务器为配置客户端提供配置属性,客户端通过访问配置服务器来获取这些配置。

功能特点:
  • 集中管理配置:将所有微服务的配置集中管理在一个地方。
  • 实时刷新配置:支持在运行时刷新配置,而不需要重启服务。
  • 版本管理:支持将配置文件放在版本控制系统(如Git、SVN)中进行版本管理。
  • 环境隔离:支持对不同的环境(如开发、测试、生产)进行配置隔离。

2. 配置中心的架构

Spring Cloud Config 分为两个部分:

  1. Config Server:配置服务器,集中管理配置文件,向客户端提供配置。
  2. Config Client:配置客户端,从配置服务器获取并应用配置。

3. 配置中心的实现步骤

3.1 搭建 Config Server

首先,创建一个新的Spring Boot项目,并添加相关依赖。

依赖配置(pom.xml):

XML 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

配置类(ConfigServerApplication.java):

java 复制代码
package com.example.configserver;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

配置文件(application.yml):

XML 复制代码
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/config-repo
          search-paths: '{application}'

上面的配置文件中,配置服务器会从指定的Git仓库中拉取配置文件。

3.2 搭建 Config Client

然后,创建一个新的Spring Boot项目,并添加相关依赖。

依赖配置(pom.xml):

XML 复制代码
<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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
</dependencies>

配置文件(bootstrap.yml):

XML 复制代码
spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev
      label: master

客户端应用类(ConfigClientApplication.java):

java 复制代码
package com.example.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

@RestController
class MessageRestController {

    @Value("${message:Default Hello}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return this.message;
    }
}

4. 测试和运行

  1. 启动 Config Server :运行 ConfigServerApplication
  2. 启动 Config Client :运行 ConfigClientApplication

访问 http://localhost:8888/config-client/dev/master 可以看到配置服务器返回的配置信息。访问 http://localhost:8080/message 可以看到配置客户端从配置服务器获取的配置信息。

5. 实现配置刷新

Spring Cloud Bus 可以实现配置的实时刷新。

增加依赖(pom.xml):

XML 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

配置文件(application.yml):

XML 复制代码
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

在配置客户端上,调用 /actuator/bus-refresh 端点来刷新配置。

6. 结论

Spring Cloud Config 是一个强大的配置管理工具,它能够有效地管理分布式系统中的配置,支持集中管理、实时刷新、环境隔离和版本控制等功能。通过上述步骤,可以轻松搭建和使用Spring Cloud配置中心,实现配置的集中管理和动态刷新。

相关推荐
小王不爱笑1328 小时前
SpringBoot 项目新建的五种方式详细笔记
spring boot·笔记·后端
superman超哥8 小时前
Rust 内存泄漏检测与防范:超越所有权的内存管理挑战
开发语言·后端·rust·内存管理·rust内存泄漏
悟空码字8 小时前
SpringBoot整合FFmpeg,打造你的专属视频处理工厂
java·spring boot·后端
独自归家的兔8 小时前
Spring Boot 版本怎么选?2/3/4 深度对比 + 迁移避坑指南(含 Java 8→21 适配要点)
java·spring boot·后端
trayvontang8 小时前
Spring Gateway核心概念、流程及原理
spring·gateway·spring gateway
superman超哥8 小时前
Rust 移动语义(Move Semantics)的工作原理:零成本所有权转移的深度解析
开发语言·后端·rust·工作原理·深度解析·rust移动语义·move semantics
superman超哥8 小时前
Rust 所有权转移在函数调用中的表现:编译期保证的零成本抽象
开发语言·后端·rust·函数调用·零成本抽象·rust所有权转移
源代码•宸8 小时前
goframe框架签到系统项目开发(实现总积分和积分明细接口、补签日期校验)
后端·golang·postman·web·dao·goframe·补签
无限进步_9 小时前
【C语言】堆(Heap)的数据结构与实现:从构建到应用
c语言·数据结构·c++·后端·其他·算法·visual studio
初次攀爬者9 小时前
基于知识库的知策智能体
后端·ai编程