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

news2025/4/21 19:48:37

文章目录

  • 配置中心
      • 为什么需要配置中心
      • 配置中心介绍
    • 服务搭建
      • 基于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.coloradmin.cn/o/2321833.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux学习笔记(应用篇二)

基于I.MX6ULL.MINI开发板 开发板与电脑相互通信电脑与开发板互传文件 开发板与电脑相互通信 用网线将电脑与开发板连接 本人使用的是Ubuntu系统&#xff0c;不是虚拟机 一般来说刚开始电脑和开发板是ping不通的 首先查看电脑的 IP WinR&#xff0c;cmd调出终端 我使用的是…

记录一次部署k3s后,服务404 page not found,nginx显示正常

服务部署k3s后&#xff0c;正常入口端怎么返回都是80&#xff0c;且返回错误 TRAEFIK DEFAULT CERT ERR_CERT_AUTHORITY_INVALID ngnix显示也是正常&#xff0c;怎么找也找不到问题 后来通过 iptables -L -n -t nat|grep 80 发现入口端流量被DNAT转到新的服务 而k3s中&#…

mac上安装nvm及nvm的基本语法使用!!

种一棵树&#xff0c;最好是十年前&#xff0c;其次是现在&#xff01;想要改变&#xff0c;从此刻开始&#xff0c;一切都不晚&#xff01; 目录 nvm是什么&#xff1f;前提条件&#xff1a;安装homebrew如果系统已经有node版本&#xff1a;在mac上安装nvm&#xff1a;用nvm安…

(基本常识)C++中const与引用——面试常问

作者&#xff1a;求一个demo 版权声明&#xff1a;著作权归作者所有&#xff0c;商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处 内容通俗易懂&#xff0c;没有废话&#xff0c;文章最后是面试常问内容&#xff08;建议通过标题目录学习&#xff09; 废话不多…

dfs(深度优先)——太抽象了

1. 两种方法 #include<bits/stdc.h> using namespace std; //void dfs(int index,int n,vector<int> current) //{ // if(index>n){ // for(int i0;i<current.size();i){ // cout<<current[i]<<" "; // } // cout<<endl;…

python --face_recognition(人脸识别,检测,特征提取,绘制鼻子,眼睛,嘴巴,眉毛)/活体检测

dlib 安装方法 之前博文 https://blog.csdn.net/weixin_44634704/article/details/141332644 环境: python3.8 opencv-python4.11.0.86 face_recognition1.3.0 dlib19.24.6人脸检测 import cv2 import face_recognition# 读取人脸图片 img cv2.imread(r"C:\Users\123\…

redis解决缓存穿透/击穿/雪崩

文章目录 1.缓存穿透1.1 概念1.2 解决方案1.2.1 缓存空对象1.2.2 布隆过滤 1.2 店铺查询使用缓存穿透解决方案1.2.1 流程 2.缓存雪崩2.1 什么是缓存雪崩&#xff1f;2.2 雪崩解决方案 3.缓存击穿3.1 什么是缓存击穿&#xff1f;3.2解决方案3.2.1 基于互斥锁解决缓存击穿问题&am…

《TCP/IP网络编程》学习笔记 | Chapter 22:重叠 I/O 模型

《TCP/IP网络编程》学习笔记 | Chapter 22&#xff1a;重叠 I/O 模型 《TCP/IP网络编程》学习笔记 | Chapter 22&#xff1a;重叠 I/O 模型理解重叠 I/O 模型重叠 I/O本章讨论的重叠 I/O 的重点不在于 I/O 创建重叠 I/O 套接字执行重叠 I/O 的 WSASend 函数进行重叠 I/O 的 WSA…

python每日十题(10)

在Python语言中&#xff0c;源文件的扩展名&#xff08;后缀名&#xff09;一般使用.py。 保留字&#xff0c;也称关键字&#xff0c;是指被编程语言内部定义并保留使用的标识符。Python 3.x有35个关键字&#xff0c;分别为&#xff1a;and&#xff0c;as&#xff0c;assert&am…

LabVIEW液压振动锤控制系统

在现代工程机械领域&#xff0c;液压振动锤的高效与精准控制日益显得重要。本文通过LabVIEW软件&#xff0c;展开液压振动锤启停共振控制技术的研究与应用&#xff0c;探讨如何通过改进控制系统来优化液压振动锤的工作性能&#xff0c;确保其在复杂工况下的稳定性与效率。 ​ …

简单介绍My—Batis

1.什么是My—Batis&#xff1f; My—Batis是一个持久层框架&#xff0c;提供了sql映射功能&#xff0c;能方便的将数据库表和java对象进行映射&#xff0c;通过My—Batis可以将项目中的数据存储在数据库中&#xff0c;以便我们进行调用。值得注意的是My—Batis和spring不是一回…

ALTER TABLE SHRINK SPACE及MOVE的区别与适用场景

以下是 ‌Oracle 数据库‌中三个收缩表空间命令的对比&#xff1a; 1. ALTER TABLE table_name SHRINK SPACE;‌ ‌作用‌&#xff1a;直接重组表数据并移动高水位线&#xff08;HWM&#xff09;&#xff0c;释放未使用的空间到表空间‌。 影响‌&#xff1a; 会锁表&#…

docker远程debug

1. 修改 Java 启动命令 在 Docker 容器中启动 Java 程序时&#xff0c;需要添加 JVM 调试参数&#xff0c;jdk8以上版本 java -agentlib:jdwptransportdt_socket,servery,suspendn,address*:5005 -jar your-app.jar jdk8及以下版本&#xff1a; java -Xdebug -Xrunjdwp:tra…

rosbag|ROS中.bag数据包转换为matlab中.mat数据类型

代码见代码 msg_dict中设置自定义消息类型 test_config中设置需要记录的具体的值 test_config中topic_name以及message_type照搬plotjuggler打开时的参数 最后生成.mat文件在matlab中进行使用

pytest-xdist 进行高效并行自动化测试

pytest-xdist 的核心功能是通过多进程分发测试任务&#xff0c;每个进程独立运行测试&#xff0c;确保测试隔离。2025 年 3 月 25 日&#xff0c;pytest-xdist 在 GitHub 上已有超过 1,200,000 次下载&#xff0c;表明其在测试社区中的广泛接受。 在自动化测试中&#xff0c;随…

位置编码再思考

最近在做多模态&#xff0c;发现基于 transformer 的多模态&#xff0c;position embedding 是一个非常重要的内容&#xff0c;而且还没有统一方案&#xff0c;先暂做记录&#xff0c;几篇还不错的博客&#xff1a; Transformer学习笔记一&#xff1a;Positional Encoding&…

Deepseek API+Python 测试用例一键生成与导出 V1.0.3

** 功能详解** 随着软件测试复杂度的不断提升,测试工程师需要更高效的方法来设计高覆盖率的测试用例。Deepseek API+Python 测试用例生成工具在 V1.0.3 版本中,新增了多个功能点,优化了提示词模板,并增强了对文档和接口测试用例的支持,极大提升了测试用例设计的智能化和易…

[c语言日寄MAX]深度解析:大小端字节序

【作者主页】siy2333 【专栏介绍】⌈c语言日寄MAX⌋&#xff1a;这是一个专注于C语言刷题的专栏&#xff0c;精选题目&#xff0c;搭配详细题解、拓展算法。从基础语法到复杂算法&#xff0c;题目涉及的知识点全面覆盖&#xff0c;助力你系统提升。无论你是初学者&#xff0c;还…

Android ADB工具使用教程(从安装到使用)

目录 ADB工具介绍 什么是ADB&#xff1f; 组成 主要功能 ADB工具安装与连接设备 WIFI连接&#xff0c;提示计算机积极拒绝10061 WIFI成功连接后&#xff0c;拔掉数据线显示offline 提示adb版本不一致​编辑 ADB工具使用 ★日志操作命令 adb logcat:抓取日志 日志格式…

基于SSM框架的线上甜品销售系统(源码+lw+部署文档+讲解),源码可白嫖!

摘要 网络技术和计算机技术发展至今&#xff0c;已经拥有了深厚的理论基础&#xff0c;并在现实中进行了充分运用&#xff0c;尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代&#xff0c;所以对于信息的宣传和管理就很关键。因此网上销售信息的…