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

相关推荐
南雨北斗18 分钟前
linux上TP6安装PHPMailer
后端
大菠萝学姐44 分钟前
基于Spring Boot和Vue的高校图书馆座位预约系统的设计与实现
java·vue.js·spring boot·后端·python·mysql·vue
南雨北斗1 小时前
PHP fileinfo扩展的作用
后端
信码由缰1 小时前
Spring框架中的Component与Bean注解
java·spring
Re2751 小时前
快速理解Spring Bean 实例化与初始化全过程
后端·spring
程序员秘密基地2 小时前
基于html,css,vue,vscode,java,springboot,mysql数据库,在线考勤,管理系统
java·vue.js·spring·html·web app
paopaokaka_luck3 小时前
基于Spring Boot+Vue的DIY手工社预约管理系统(Echarts图形化、腾讯地图API)
java·spring boot·后端
kk_stoper3 小时前
使用Ruby接入实时行情API教程
java·开发语言·javascript·数据结构·后端·python·ruby
我会冲击波3 小时前
告别flag与status:如何为你的布尔值(boolean)变量优雅命名?
java·后端
工藤学编程3 小时前
分库分表之实战-sharding-JDBC水平分库+分表后:查询与删除操作实战
数据库·spring boot·后端·sql·mysql