一、整合步骤

1)加入Maven坐标

       <!-- actuator监控模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0..RELEASE</version>
</dependency>
<!--amqp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.0..RELEASE</version>
</dependency>
<!--spring cloud bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.0..RELEASE</version>
</dependency>

2) 添加配置

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

# 开放actuator/bus-refresh断点
management:
endpoints:
web:
exposure:
include: bus-refresh

3)程序代码引入配置参数的地方,添加@RefreshScope 注解,如下:

/**
* @author zhangboqing
* @date 2019-09-03
*/
@RestController
@RefreshScope
public class UserController { @Value("${pro1}")
private String pro1; @ApiOperation(value = "用户模块-GET操作")
@GetMapping(value = "get")
public ApiResponseT get() { return ApiResponseT.builder().setData(pro1);
} }

4)启动程序,访问打印参数。然后修改参数,post方式请求http://localhost:10802/actuator/bus-refresh来刷新程序参数值,再访问参数发现值自动就更新来。
在同一个程序多实例的情况下,刷新其中一个实例,其他实例也会自动被刷新。

二、Spring Cloud Bus整合最佳实践

1)上面的整合步骤,在整个架构上如下图所示,这些微服务应用的实例中都引入了Spring Cloud Bus,所以它们都连接到了RabbitMQ的消息总线上。

我们可以从Git仓库中配置的修改到发起actuator/bus-refresh的POST请求这一步可以通过Git仓库的Web Hook来自动触发。由于所有连接到消息总线上的应用都会接收到更新请求,所以在WebHook中就不需要维护所有节点内容来进行更新,这样就不用通过Web Hook来逐个进行刷新每个程序配置的问题。

注意:actuator/bus-refresh断点有提供destination参数,用来定位具体要刷新的应用程序。此时总线上的各应用实例会根据destination属性的值来判断是否为自己的实例名,

,实例的默认命名按此规则生成:${spring.cloud.client.hostname}:${spring.applica-tion.name}:${spring.application.instance_id:${server.port}}};

若符合才进行配置刷新,若不符合就忽略该消息。比如,我们可以请求actuator/bus/refresh?destination=user:9000或者actuator/bus/refresh?destination=user:**

2)基于上面的架构优化及最佳实践,如下图:

相关改动如下:

1. 在Config Server中也引入Spring Cloud Bus,将配置服务端也加入到消息总线中来。

2. actuator/bus/refresh请求不再发送到具体服务实例上,而是发送给Config Server,并通过destination参数来指定需要更新配置的服务或实例。
通过上面的改动,我们的服务实例不需要再承担触发配置更新的职责。同时,对于Git的触发等配置都只需要针对ConfigServer即可,从而简化了集群上的一些维护工作。

【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践的更多相关文章

  1. Spring与web MVC的整合——Spring的应用上下文管理

    问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml.这类web容器会在启动的时候会而且仅会加载如下三种 ...

  2. spring boot使用freemarker模版整合spring Data JPA

    目录结构 第一步:在pom.xml文件中添加依赖 <!--模板依赖--> <dependency> <groupId>org.springframework.boo ...

  3. Spring Data JPA 整合Spring

    1.1   Spring Data JPA 与 JPA和hibernate之间的关系 JPA是一套规范,内部是有接口和抽象类组成的.hibernate是一套成熟的ORM框架,而且Hibernate实现 ...

  4. Spring Boot工程结构推荐程结构(最佳实践)

    工程结构(最佳实践) Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程 ...

  5. 整合SPRING CLOUD云服务架构 - 企业分布式微服务云架构构建

    整合SPRING CLOUD云服务架构 - 企业分布式微服务云架构构建 1.   介绍 Commonservice-system是一个大型分布式.微服务.面向企业的JavaEE体系快速研发平台,基于模 ...

  6. Spring Cloud gateway 五 Sentinel整合

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  7. Spring Cloud 微服务技术整合

    微服务架构风格是一种使用一套小服务来开发单个应用的方式途径,每个服务运行在自己的进程中,并使用轻量级机制通信,通常是HTTP API,这些服务基于业务能力构建,并能够通过自动化部署机制来独立部署,这些 ...

  8. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  9. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

随机推荐

  1. 【数位DP】【P4317】花神的数论题

    [数位DP][P4317]花神的数论题 Description 给定 \(n\),求 \(n\) 以内所有正整数二进制下 \(1\) 的个数的乘积,答案对 \(10^7 + 7\) 取模 Limita ...

  2. parameter与argument,property与attribute

    本文摘自:https://blog.csdn.net/Zhangxichao100/article/details/59484133 parameter与argument,property与attri ...

  3. MongoDB Shell 命令

    更新列名 db.Stores.update({}, {$rename : {"StoreId" : "MetaId"}}, false, true) 查询长度 ...

  4. c语言之函数指针应用

    c语言之函数指针应用 1.函数指针与指针函数 在开始运用函数指针前,我们需要将两个概念即:函数指针与指针函数搞清楚. 函数指针,指明这个一个函数,但返回值为指针类型,语法格式为: 类型名* 函数名A( ...

  5. xcode选项Build Active Architecture Only的作用

    Build Active Architecture Only 设置: 设置为NO的时候,会导致react-native项目启动失败npx react-native run-ios 根据错误信息 bui ...

  6. CentOS7 CPU 降频问题

    CentOS7 系统默认的 CPUPOWER 策略是 powersave 节能模式,Google 了非常多的资料,一直没有找到解决办法,现在分享一下. 执行: tuned-adm profile th ...

  7. CentOS忘记密码修改方案以及centos卡在开机登录界面,命令失效的解决方法

    CentOS忘记密码修改方案 应用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于CentOS7.2环境进行操作的,由于CentOS的版本之间是有差异的,继续之前请先确定 ...

  8. 解决上传文件或图片时选择相同文件无法触发change事件的问题

    昨天在做一个上传文件的模块时遇到了这样的问题:打开文件一上传,上传成功后再次点击文件一,change事件无反应 <input type="file" name="f ...

  9. selenium又一小坑 无法用XPATH直接获取属性值 需要使用.get_attribute(“href”)

    在使用selenium进行抓取url的时候,试图使用find_elements_by_xpath来获取. 因此想当然的直接使用XPATH的语法进行获取属性 事例缩略后xml结构如下 <div c ...

  10. Akka-CQRS(7)- CQRS Reader Actor 示范

    我们在这篇通过一个具体CQRS-Reader-Actor的例子来示范akka-persistence的query端编程和应用.在前面的博客里我们设计了一个CQRS模式POS机程序的操作动作录入过程,并 ...