SpringCloud学习(四)---Spring Cloud Bus

Spring Cloud Bus将轻量级消息代理程序链接到分布式系统的节点。 然后可以将其用于广播状态更改(例如配置更改)或其他管理指令。 该项目包括AMQP和Kafka经纪人实施。 另外,在类路径上找到的任何Spring Cloud Stream绑定程序都可以作为传输工具使用。
 

简单来讲spring cloud bus其实是一种概念, 当一个分布式节点发生改变,通过mq的方式广播或者其他方式通知整个集群或者局部. 他的建议是amqp或者kafka,当时rabbit,rocket其实也都是一样的.

Spring Cloud Bus提供了两个端点/ actuator / bus-refresh和/ actuator / bus-env,它们分别对应于Spring Cloud Commons中的各个执行器端点/ actuator / refresh和/ actuator / env。

/ actuator / bus-refresh端点清除RefreshScope缓存并重新绑定@ConfigurationProperties。 有关更多信息,请参见刷新作用域文档。

management.endpoints.web.exposure.include=bus-env

2.2. Bus Env Endpoint

/ actuator / bus-env端点使用跨多个实例的指定键/值对更新每个实例环境。

management.endpoints.web.exposure.include=bus-env

The /actuator/bus-env endpoint accepts POST requests with the following shape:

{
    "name": "key1",
    "value": "value1"
}

到这里的时候我有点蒙......然后我看了一篇文章

https://www.cnblogs.com/wjx6270/p/13472406.html

大概就懂了, 之前理解的没错,不过他帮我们封装了一下,然后变成了事件驱动的模式.刚才看前面的时候有这阿里巴巴的rocketmq体系.这里我就先不多说了.因为明白了概念和思想就是王道.代码的实现是小功能.

3. Addressing an Instance

应用程序的每个实例都有一个服务ID,可以使用spring.cloud.bus.id设置其值,并且其值应按冒号分隔的标识符列表(从最小到最具体)排列。 默认值是根据环境构造的,它是spring.application.name和server.port(或spring.application.index,如果设置)的组合。 ID的默认值以app:index:id的形式构造,其中:

app是vcap.application.name(如果存在)或spring.application.name

index是vcap.application.instance_index,如果存在,则为spring.application.index,local.server.port,server.port或0(按此顺序)。

id是vcap.application.instance_id(如果存在)或随机值。

HTTP端点接受“目的地”路径参数,例如/ bus-refresh / customers:9000,其中destination是服务ID。 如果该ID由总线上的一个实例拥有,它将处理该消息,而所有其他实例将忽略它。

4. Addressing All Instances of a Service

The “destination” parameter is used in a Spring PathMatcher (with the path separator as a colon — :) to determine if an instance processes the message. Using the example from earlier, /bus-env/customers:** targets all instances of the “customers” service regardless of the rest of the service ID.

这两段说的就是如何配置自己消息是点对点还是广播

5. Service ID Must Be Unique

总线尝试两次以消除对事件的处理-从原始ApplicationEvent一次,从队列一次。 为此,它将对照当前服务ID检查发送服务ID。 如果服务的多个实例具有相同的ID,则不会处理事件。 在本地计算机上运行时,每个服务都在不同的端口上,并且该端口是ID的一部分。 Cloud Foundry提供了一个区分索引。 为了确保ID在Cloud Foundry之外是唯一的,请将spring.application.index设置为每个服务实例唯一的名称。

6. Customizing the Message Broker

Spring Cloud Bus使用Spring Cloud Stream广播消息。 因此,要使消息流动,您只需要在类路径中包括您选择的活页夹实现即可。 带有AMQP(RabbitMQ)和Kafka(spring-cloud-starter-bus- [amqp | kafka])的总线有方便的启动器。 一般来说,Spring Cloud Stream依赖于Spring Boot自动配置约定来配置中间件。 例如,可以使用spring.rabbitmq。*配置属性来更改AMQP代理地址。 Spring Cloud Bus在spring.cloud.bus。*中具有少数本机配置属性(例如,spring.cloud.bus.destination是用作外部中间件的主题的名称)。 通常,默认值就足够了。

要了解有关如何自定义消息代理设置的更多信息,请查阅Spring Cloud Stream文档。

7. Tracing Bus Events

可以通过设置spring.cloud.bus.trace.enabled = true来跟踪总线事件(RemoteApplicationEvent的子类)。 如果这样做,Spring Boot TraceRepository(如果存在)将显示发送的每个事件以及每个服务实例的所有确认。 以下示例来自/ trace端点:

后面是一些标准的启动方法

标签

https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/3.0.0.M1/reference/html/appendix.html

-----------------------谈一下自己的感受吧!

首先bus的事件驱动的理念和思想真的很重要. 这个只能说段位越高越有感触吧. 还有就是三年前做微服务架构的时候, 包括最近在新公司搭建的一套都没有用到bus.(没有那么大的舞台.手里两三个服务的时候直接mq收发一下).原来bus的事件驱动和配置的简洁化如此简单.后续会好好应用一下

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值