config: 分布式配置中心 bus: 消息总线

news/2024/10/21 7:31:58/

文章目录

  • hello world
  • 热刷新
  • Spring Cloud Bus:消息总线

每一个应用程序在运行时都需要相应的yml配置,分布式架构下多个服务器和应用服务面临着多个配置文件,在修改和发布上难度较大,需要有一个管理中心来统一管理,优雅的解决了配置的动态变更、持久化、运维成本等问题

流程:
分布式配置中心去远程仓库将创建好的yml文件读取,application client去分布式配置中心获取配置

Spring Cloud Config: spring cloud config server和spring cloud config client
基于Http协议

hello world

一.在gitee上创建一个仓库,创建application.yml,如下配置:

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: fsfs

二.创建一个项目config_server
1.导入依赖:

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

2.yml配置:

server:port: 8888# 增加分布式配置中心服务端配置。连接GIT仓库
spring:cloud: # spring cloud常用配置前置config: # 分布式配置中心配置前置server: # 服务端配置git: # git文件仓库配置uri: https://gitee.com/xxxx/spring_cloud_config.git # git仓库具体地址#username: xxxxxx # 私有仓库必须配置用户名和密码。#password: xxxxxx # 公开仓库可以省略用户名和密码配置。

3.启动类上加@EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigApplication {public static void main(String[] args) {SpringApplication.run(SpringcloudConfigApplication.class, args);}
}

三.访问测试:
在浏览器地址栏输入 http://localhost:8888/name/profile/label ,基于Config Server获取需要的配置文件。
name-必要restful参数,代表配置文件主体名称,及一般为application
profile-必要restful参数,代表配置文件扩展环境名称,默认读取default环境扩展配置,即application-xx.yml中的xx
label-可选参数,代表配置文件所在GIT分支名称,默认null,相当于master分支

访问 http://localhost:8888/application/default(没有可以不写)/master(分支名称)

四.创建项目config_client获取config_server的配置信息
1.导入依赖

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

2.创建bootstrap.yml,不使用application.yml,防止冲突
新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境

spring:cloud:config: # spring cloud config 客户端配置uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888name: application # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application# profile: default # 要读取的配置文件环境是什么,默认defaultlabel: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

四.改变远程仓库的端口server.port,
反复启动cofig_client项目,观察端口号变化

热刷新

1.添加导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.Config Client新增配置内容

management:endpoints:web:exposure:include: refresh,info,health

3.Config Client修改服务实现
在使用远程配置内容的类上(案例中的Service,放在controler上无效)增加新注解:

@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClientService {@Value("${my.content}")private String content;@Overridepublic String test() {System.out.println("content = " + content);return content;}
}

4.测试热刷新
使用PostMan工具,发送POST
访问 http://localhost:8080/actuator/refresh 访问改地址,才能刷新注入,实际已经刷新了
再次访问 http://localhost:8080/test

Spring Cloud Bus:消息总线

Spring Cloud Bus集成了市面上常见的RabbitMQ和Kafka等消息代理。其会连接微服务系统中所有拥有Bus总线机制的节点,当有数据变更的时候,会通过消息中间件使用消息广播的方式通知所有的微服务节点同步更新数据。(如:微服务配置更新等)

基于Bus消息总线实现热刷新功能,需要在所有的Eureka Client端应用中增加spring-cloud-starter-bus-amqp依赖,这个依赖是消息总线集成的RabbitMQ消息同步组件。基于消息总线的热刷新同样是通过actuator实现的,所以需要spring-boot-starter-actuator启动器依赖。
1.在Config Client中增加依赖:

<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.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 总线技术中的amqp相关依赖。 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.编辑配置文件: 编辑Config Client中的配置文件。

spring:rabbitmq:host: localhostusername: guestpassword: giest
management:endpoints:enabled-by-default: trueweb:exposure:include: bus-refresh,info,health

3.测试
消息总线Bus基于Actuator对外提供了热刷新服务,服务地址是:http://localhost:8080/actuator/bus-refresh。此服务只能使用POST方式请求,可以使用PostMan测试。


http://www.ppmy.cn/news/53825.html

相关文章

「欧拉定理」[SDOI2008]仪仗队

[SDOI2008]仪仗队 https://ac.nowcoder.com/acm/problem/20313 题目描述 作为体育委员&#xff0c;C君负责这次运动会仪仗队的训练。 仪仗队是由学生组成的N * N的方阵&#xff0c;为了保证队伍在行进中整齐划一&#xff0c;C君会跟在仪仗队的左后方&#xff0c;根据其视线所…

Redis到底是多线程还是单线程?

Redis 是单线程的&#xff01; Redis 是非常的快的&#xff01;Redis 是基于内存操作&#xff0c;CPU 不是 Redis 性能瓶颈&#xff0c;内存和网络带宽&#xff08;因为 IO 时需要使用&#xff09;才是 Redis 的性能瓶颈。 Redis 为什么不使用多线程&#xff1f; 因为在多线程…

Springboot获取jar包中resources资源目录下的文件

阿萨斯多问题现象&#xff1a; 今天在项目中遇到一个业务场景&#xff0c;需要用到resources资源目录下的文件&#xff0c;然后就在思考一个问题&#xff1a; 当项目打成jar后&#xff0c;Springboot要如何获取resources资源目录下的文件呢&#xff1f; 问题分析&#xff1a; 如…

【前端基础知识】Vue中的变量不是响应式的吗?属性赋值后视图不变化的原因是什么?

目录 &#x1f914;问题&#x1f4dd;回答&#x1f3a8;使用场景动态添加属性动态添加数组元素 ❌注意事项$set只能在响应式对象上使用$set不能用于根级别的属性$set的性能问题 &#x1f4c4;总结 &#x1f914;问题 Vue是一款在国内非常流行的框架&#xff0c;采用MVVM架构&a…

ChatGPT原理剖析

文章目录 ChatGPT常见误解1. 罐头回应2. 网络搜寻重组 ChatGPT真正做的事——文字接龙ChatGPT背后的关键技术——预训练&#xff08;Pre-train&#xff09;一般机器是怎样学习的&#xff1f; ChatGPT带来的研究问题1. 如何精准提出需求2. 如何更改错误3. 侦测AI生成的物件4. 不…

如何优雅的写个try catch的方式!

软件开发过程中&#xff0c;不可避免的是需要处理各种异常&#xff0c;就我自己来说&#xff0c;至少有一半以上的时间都是在处理各种异常情况&#xff0c;所以代码中就会出现大量的try {...} catch {...} finally {...} 代码块&#xff0c;不仅有大量的冗余代码&#xff0c;而…

Leetcode——485. 最大连续 1 的个数

&#x1f4af;&#x1f4af;欢迎来到的热爱编程的小K的Leetcode的刷题专栏 文章目录 1、题目2、滑动窗口3、一次遍历(官方题解) 1、题目 题目&#xff1a;给定一个二进制数组 nums &#xff0c; 计算其中最大连续 1 的个数。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,0…

python基础实战4-python基础语法

1、注释&#xff08;Comments&#xff09; 注释用来向用户提示或解释某些代码的作用和功能&#xff0c;它可以出现在代码中的任何位置。 Python解释器在执行代码时会忽略注释&#xff0c;不做任何处理&#xff0c;就好像它不存在一样。 1.1 代码注释介绍 注释就是对代码的解…