当前位置: 首页 > article >正文

【微服务架构】SpringCloud(七):配置中心 Spring Cloud Config

文章目录

  • 配置中心
      • 为什么需要配置中心
      • 配置中心介绍
    • 服务搭建
      • 基于GITHUB
        • 1.创建仓库
        • 2.新建微服务作为配置中心服务
        • 3.启动测试拉取
      • 匹配规则
      • 分支读取
    • 客户端配置
      • 配置文件
      • 引入依赖
      • 使用远程配置
    • 刷新配置
      • 手动配置热更新
      • 自动刷新
        • erlang安装
        • RabbitMQ安装
        • 环境变量
        • 管理界面
        • 服务配置
        • 测试
    • 完整配置
      • Config
        • application.properties
        • 启动类
        • 依赖
      • Consumer
        • bootstrap.properties
        • 依赖
      • 远程配置
        • consumer-dev.properties
  • 总结
    • Spring Cloud Config
      • 服务端(Config Server)
        • 1. 添加依赖
        • 2. 启用 Config Server
        • 3. 配置仓库
      • 客户端(Config Client)
        • 1. 添加依赖
        • 2. 配置 Config Server 地址
        • 3. 获取配置
    • 配置的动态刷新
      • 使用 Spring Cloud Bus
        • 1. 添加依赖
        • 2. 配置消息中间件
        • 3. 启用动态刷新
    • 注意事项

在这里插入图片描述
个人主页:道友老李
欢迎加入社区:道友老李的学习社区

配置中心

为什么需要配置中心

单体应用,配置写在配置文件中,没有什么大问题。如果要切换环境 可以切换不同的profile(2种方式),但在微服务中。

  1. 微服务比较多。成百上千,配置很多,需要集中管理。

  2. 管理不同环境的配置。

  3. 需要动态调整配置参数,更改配置不停服。

配置中心介绍

分布式配置中心包括3个部分:

  1. 存放配置的地方:git ,本地文件 等。
  2. config server。从 1 读取配置。
  3. config client。是 config server 的客户端 消费配置。

服务搭建

基于GITHUB

1.创建仓库

登录GitHub创建仓库,并上传几个配置文件

2.新建微服务作为配置中心服务

依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

配置文件

spring.cloud.config.server.git.uri=https://github.com/piziniao/config-center.git
spring.cloud.config.label=master

eureka.client.service-url.defaultZone=http://euk1.com:7002/eureka/

启动类

package com.dyll.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class AConfigApplication {

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

}
3.启动测试拉取

启动服务后访问服务

http://localhost:70/master/config-client-dev.properties

正确配置后能读到来自git的配置文件

匹配规则

获取配置规则:根据前缀匹配
/{name}-{profiles}.properties
/{name}-{profiles}.yml
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml

name 服务名称
profile 环境名称,开发、测试、生产:dev qa prd
lable 仓库分支、默认master分支

匹配原则:从前缀开始。

分支读取

客户端配置

配置文件

修改 application.properties为bootstrap.properties

#直接URL方式查找配置中心
spring.cloud.config.uri=http://localhost:9999/
#通过注册中心查找
#spring.cloud.config.discovery.enabled=true
#spring.cloud.config.discovery.service-id=a-config
spring.cloud.config.profile=dev
spring.cloud.config.label=dev

引入依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>

使用远程配置

	
	@Value("${config.info}")
	String info;

consumer-dev.properties

config.info="config-dev,v1"

刷新配置

手动配置热更新

  1. 开启actuator中的refresh端点
  2. Controller中添加@RefreshScope注解
  3. 向客户端 url http://localhost:91/actuator/refresh发送Post请求

自动刷新

erlang安装

http://www.erlang.org/downloads

RabbitMQ安装

http://www.rabbitmq.com/install-windows.html

环境变量

path中添加 %ERLANG_HOME%\bin

# 开启RabbitMQ节点
rabbitmqctl start_app
# 开启RabbitMQ管理模块的插件,并配置到RabbitMQ节点上
rabbitmq-plugins enable rabbitmq_management
管理界面

http://localhost:15672

用户名密码均为guest

服务配置

配置文件

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
测试

启动两个微服务

修改配置文件后向其中一个端点发送post请求

http://localhost:91/actuator/bus-refresh

观察另一个服务是否也跟着刷新了

完整配置

Config

application.properties
#################################### common config : ####################################
spring.application.name=a-config
# 应用服务web访问端口
server.port=9999
# ActuatorWeb访问端口
management.server.port=8081
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.config.server.git.uri=https://github.com/piziniao/config-center.git
spring.cloud.config.label=master

eureka.client.service-url.defaultZone=http://euk1.com:7002/eureka/


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
启动类
@EnableConfigServer
依赖
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
		
		
			<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

Consumer

bootstrap.properties
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=a-config
spring.cloud.config.profile=dev
spring.cloud.config.label=dev


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
依赖
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>

远程配置

consumer-dev.properties
config.info="config-dev,v8"

总结

Spring Cloud 配置中心用于集中管理和存储应用程序的配置信息,支持配置的动态刷新、版本管理等功能,能让你在不同环境(开发、测试、生产)下更方便地管理配置。下面为你介绍 Spring Cloud 配置中心的主要组件及使用方法。

Spring Cloud Config

Spring Cloud Config 是 Spring Cloud 官方提供的配置中心解决方案,它由服务端和客户端两部分组成。

服务端(Config Server)

Config Server 作为配置中心的核心,负责从配置仓库(如 Git、SVN 等)中读取配置信息,并以 RESTful 接口的形式提供给客户端使用。

1. 添加依赖

pom.xml 中添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
2. 启用 Config Server

在 Spring Boot 主类上添加 @EnableConfigServer 注解:

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);
    }
}
3. 配置仓库

application.propertiesapplication.yml 中配置 Git 仓库信息:

spring.cloud.config.server.git.uri=https://github.com/your-repo/config-repo.git

客户端(Config Client)

Config Client 是使用配置的应用程序,它会从 Config Server 获取配置信息。

1. 添加依赖

pom.xml 中添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>
2. 配置 Config Server 地址

bootstrap.propertiesbootstrap.yml 中配置 Config Server 的地址:

spring.cloud.config.uri=http://localhost:8888
3. 获取配置

在客户端应用中,可以通过 @Value 注解或 Environment 对象获取配置信息:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {
    @Value("${config.property}")
    private String configProperty;

    @GetMapping("/config")
    public String getConfig() {
        return configProperty;
    }
}

配置的动态刷新

若要实现配置的动态刷新,可结合 Spring Cloud Bus 或使用 Actuator 的 /refresh 端点。

使用 Spring Cloud Bus

Spring Cloud Bus 是一个消息总线,借助它可以将配置刷新的消息广播到所有客户端。

1. 添加依赖

在 Config Server 和客户端的 pom.xml 中添加以下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2. 配置消息中间件

application.propertiesapplication.yml 中配置 RabbitMQ 信息:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3. 启用动态刷新

在客户端的控制器类上添加 @RefreshScope 注解:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.property}")
    private String configProperty;

    @GetMapping("/config")
    public String getConfig() {
        return configProperty;
    }
}

当修改配置仓库中的配置后,向 Config Server 发送 /actuator/bus-refresh 请求,Spring Cloud Bus 会将刷新消息广播到所有客户端。

注意事项

  • 确保 Config Server 能访问配置仓库,并且客户端能访问 Config Server。
  • 合理设置配置的环境和标签,以区分不同环境和版本的配置。

http://www.kler.cn/a/600795.html

相关文章:

  • OpenCV图像拼接(7)根据权重图对源图像进行归一化处理函数normalizeUsingWeightMap()
  • 洛谷 P1351 [NOIP 2014 提高组] 联合权值(树)
  • HTML5 canvas圆形泡泡动画背景特效
  • 最长连续子序列和的所含元素 -- Kadane算法拓展
  • R语言——字符串
  • 一文解读DeepSeek的安全风险、挑战与应对策略
  • C#基础学习(一)复杂数据类型之枚举
  • 【Linux】从开发到系统管理深入理解环境变量
  • RocketMQ 详细知识点总结
  • 文章记单词 | 第2篇(六级)
  • STM32/GD32主要学习内容
  • K8s的网络
  • Java高频面试之集合-18
  • 目录遍历漏洞复现
  • 从零构建大语言模型全栈开发指南:第二部分:模型架构设计与实现-2.2.2文本生成逻辑:Top-k采样与温度控制
  • Vibe Coding:编程的未来?
  • Rust Web 开发新选择:探索 Hyperlane 轻量级 HTTP 服务器框架
  • 《TypeScript 面试八股:高频考点与核心知识点详解》
  • 智慧医院、养老人员高精度定位解决方案
  • 【netstat和ss】Windows和Linux下的,网络连接排查简单案例