您当前的位置: 首页 >  spring cloud

星夜孤帆

暂无认证

  • 5浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Spring Cloud Bus消息总线

星夜孤帆 发布时间:2020-06-19 21:28:04 ,浏览量:5

一、概述

上一篇说到Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用Spring Cloud Bus可以完美的解决这一问题。

1.1 Spring Cloud Bus是什么

Bus支持两种消息代理:RabbitMQ和Kafka

Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新。

Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。Spring Cloud Bus目前支持两种消息代理:RabbitMQ和Kafka。

1.2 Spring Cloud Bus能干嘛

Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。

1.3 为何被称为总线

什么是总线

在微服务架构系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。

基本原理

ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。

二、RabbitMQ环境配置 2.1 安装Erlang

下载地址:http://erlang.org/download/otp_win64_21.3.exe

步骤:

2.2 安装RabbitMQ

下载地址:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe

步骤:

2.3 进入RabbitMQ安装目录下的sbin目录

输入以下命令启动管理功能

rabbitmq-plugins enable rabbitmq_management

2.4 访问地址查看是否安装成功

http://localhost:15672/

输入账号密码并登录:guest guest

 

三、Spring Cloud Bus动态刷新全局广播

先配置好RabbitMQ

3.1 增加3366 3.1.1 新建cloud-config-c lient-3366

3.1.2 POM


    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client-3366


    

        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    




3.1.3 YML
server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.1.4 主启动
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/19
 */
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3366.class, args);
    }
}
3.1.5 controller
package com.atguigu.springcloud.controller;

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("${server.port}")
    private String serverPort;

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


    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
    }


}


3.2 设计思想 3.2.1 利用消息总线触发一个客户端/buc/refresh,而刷新所有客户端的配置

3.2.2 利用消息总线触发一个服务端ConfigServer的/buc/refresh端点,而刷新所有客户端的配置(更加推荐)

 

3.2.3 图二的架构显然更加合适,图一不适合的原因如下
  • 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责。
  • 破坏了微服务各节点的对等性
  • 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。
3.2.4 给cloud-config-center-3344配置中心服务端添加消息总线支持

POM


            org.springframework.cloud
            spring-cloud-starter-bus-amqp

YML

server:
  port: 3344
spring:
  application:
    name: cloud-config-center # 注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri:  https://gitee.com/jakhyd/springcloud-config.git # 填写你自己的github路径
          # 搜索目录
          search-paths:
            - springcloud-config # 仓库名称
      label: master  # 分支名称
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    
# 服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: 'buc-refresh'
3.2.5 给cloud-config-center-3355客户端添加消息总线支持 

POM



    
        com.atguigu.springcloud
        cloud2020
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client-3355

    
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot

            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    



YML

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
3.2.6 给cloud-config-center-3366客户端添加消息总线支持

POM



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client-3366


    
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        

        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    




YML

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.3 运维工程师 3.3.1 修改Github上配置文件增加版本号 3.3.2 发送Post请求

curl -X POST "http://localhost:3344/actuator/bus-refresh"

一次发送,处处生效

3.4 配置中心

http://config-3344.com/config-dev.yml

3.5 客户端
  • http://localhost:3355/configInfo
  • http://localhost:3366/configInfo
  • 获取配置信息,发现都已经刷新了
  • 一次修改,广播通知,处处生效
四、SpringCloud Bus动态刷新定点通知

如果我们不想全部通知,只想定点通知,比如只通知3355,而不通知3366,该如何做?

简单一句话,指定具体某一个实例生效而不是全部。

公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

/buc/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例

4.1 案例

我们这里以刷新运行在3355端口上的config-client为例

只通知3355,不通知3366

curl-X POST "http://localhost:3344/actuator/buc-refresh/config-client:3355"

4.2 通知总结

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.1053s