【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配置中心,实现配置的集中管理和动态刷新。

相关推荐
小钊(求职中)3 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven
java技术小馆4 小时前
Deepseek整合SpringAI
java·spring cloud
Asthenia04126 小时前
如何在项目中集成GC日志输出与高效分析?一篇开发者必读的实践指南
后端
码界筑梦坊6 小时前
基于Flask的第七次人口普查数据分析系统的设计与实现
后端·python·信息可视化·flask·毕业设计
独泪了无痕7 小时前
MySQL查询优化-distinct
后端·mysql·性能优化
阿波茨的鹅7 小时前
Asp.Net 前后端分离项目——项目搭建
后端·asp.net
Asthenia04127 小时前
Jvm参数——规律记忆方法
后端
超爱吃士力架7 小时前
MySQL 三层 B+ 树能存多少数据?
java·后端·面试
转转技术团队8 小时前
高并发下秒杀系统的设计
后端