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

相关推荐
暮色妖娆丶6 小时前
Spring 源码分析 BeanFactoryPostProcessor
spring boot·spring·源码
神奇小汤圆6 小时前
浅析二叉树、B树、B+树和MySQL索引底层原理
后端
文艺理科生6 小时前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
前端·后端·架构
千寻girling6 小时前
主管:”人家 Node 框架都用 Nest.js 了 , 你怎么还在用 Express ?“
前端·后端·面试
南极企鹅6 小时前
springBoot项目有几个端口
java·spring boot·后端
Luke君607976 小时前
Spring Flux方法总结
后端
define95276 小时前
高版本 MySQL 驱动的 DNS 陷阱
后端
忧郁的Mr.Li7 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端
暮色妖娆丶8 小时前
SpringBoot 启动流程源码分析 ~ 它其实不复杂
spring boot·后端·spring
Coder_Boy_8 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例中相关概念
java·人工智能·spring boot·后端·spring