54 消息总线bus

作者: 木子教程 | 来源:发表于2022-01-19 22:15 被阅读0次

    在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它 广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。 SpringCloud中也有对应的解决方案,SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来, 可以很容易搭建消息总线,配合SpringCloud config 实现微服务应用配置信息的动态更新。


    image-20220102211404297.png

    根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:

    • 提交代码触发post请求给bus/refresh
    • server端接收到请求并发送给Spring Cloud Bus
    • Spring Cloud bus接到消息并通知给其它客户端
    • 其它客户端接收到通知,请求Server端获取最新配置
    • 全部客户端均获取到最新的配置

    消息总线整合配置中心

    (1) 引入依赖

    <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>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-bus</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    
    

    (2)服务端配置

    server:
     port: 10000 #服务端口
    spring:
     application:
       name: config-server #指定服务名
     cloud:
       config:
         server:
           git:
             uri: https://gitee.com/it-lemon/config-repo.git
     rabbitmq:
       host: 127.0.0.1
       port: 5672
       username: guest
       password: guest
    management:
     endpoints:
       web:
         exposure:
           include: bus-refresh
    eureka:
     client:
       serviceUrl:
         defaultZone: http://127.0.0.1:8761/eureka/
     instance:
       preferIpAddress: true
       instance-id: ${spring.cloud.client.ip-address}:${server.port}
    #spring.cloud.client.ip-address:获取ip地址
    

    (3)微服务客户端配置

    server:
     port: 9002
    eureka:
     client:
       serviceUrl:
         defaultZone: http://127.0.0.1:8761/eureka/
    spring:
     cloud:
       config:
         name: product
         profile: dev
         label: master
         discovery:
           enabled: true
           service-id: config-server
    
    

    需要在码云对应的配置文件中添加rabbitmq的配置信息

    image-20220102211635504.png

    重新启动对应的eureka-server , config-server , product-service。配置信息刷新后,只需要向配置 中心发送对应的请求,即可刷新每个客户端的配置

    相关文章

      网友评论

        本文标题:54 消息总线bus

        本文链接:https://www.haomeiwen.com/subject/cosdhrtx.html