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

发布于:2024-06-19 ⋅ 阅读:(28) ⋅ 点赞:(0)

目录

1. Spring Cloud Config 简介

功能特点:

2. 配置中心的架构

3. 配置中心的实现步骤

3.1 搭建 Config Server

3.2 搭建 Config Client

4. 测试和运行

5. 实现配置刷新

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):

<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):

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):

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):

<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):

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev
      label: master

客户端应用类(ConfigClientApplication.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):

<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):

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


网站公告

今日签到

点亮在社区的每一天
去签到