第七篇: 消息總線(Spring Cloud Bus)


Spring Cloud Bus 將分布式的節點用輕量的消息代理連接起來。它可以用於廣播配置文件的更改或者服務之間的通訊,也可以用於監控。本文要講述的是用Spring Cloud Bus實現通知微服務架構的配置文件的更改。

一、准備工作

本文還是基於上一篇文章來實現。按照官方文檔,我們只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;這就是說我們需要裝rabbitMq,點擊rabbitmq下載,點擊erlang下載。上兩個下載都是百度網盤的鏈接,也可以https://download.csdn.net/download/snowwolf9999/10551353幫我加點CSDN分,嘿嘿。

具體安裝erlang環境,參考https://www.cnblogs.com/PPBoy/p/9335943.html

二、改造eureka-config-client工程。

在pom文件加上起步依賴spring-cloud-starter-bus-amqp和spring-boot-starter-actuator。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

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

完整的配置文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sun</groupId>
  <artifactId>eureka-config-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>
View Code

在配置文件application.yml中加上RabbitMq的配置,包括RabbitMq的地址、端口,用戶名、密碼。並需要加上spring.cloud.bus的三個配置,具體如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:8888/
      bus:
        enabled: true
        trace:
          enabled:true
    discovery:
      enabled: true
      serviceId: config-server
  rabbitmq: 
    host: localhost
    port: 5672
    username: guest
    password: guest
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8881
  
management: 
  endpoints:
    web:
      exposure:
        include: bus-refresh
  

 

ConfigClientApplication啟動類代碼如下:

package com.sun;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableEurekaClient
@RefreshScope //添加注釋
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}

這時我們去代碼倉庫將foo的值改為“foo version 4”,即改變配置文件foo的值。如果是傳統的做法,需要重啟服務,才能達到配置文件的更新。此時,我們只需要發送post請求:http://localhost:8881/actuator/bus-refresh,你會發現config-client會重新讀取配置文件:

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888

tip:這里發送post請求可以用postman:

 

 寫地址發送就可以了,這時我們再訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:

foo version 4

另外,/actuator/bus-refresh接口可以指定服務,即使用”destination”參數,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服務名為customers的所有服務。

三、分析

此時的架構圖: 

當git文件更改的時候,通過pc端用post 向端口為8881的config-client發送請求/bus/refresh/;此時8881端口會發送一個消息,由消息總線向其他服務傳遞,從而使整個微服務集群都達到更新配置文件。

當然,向端口8882的服務發送也可以。

最后說一下http://localhost:8881/actuator/bus-refresh不能成功刷新,基本上有2種可能:

1、查看rabbitmq是否安裝完畢,是否運行。查看rabbitmq狀態,參考:https://blog.csdn.net/lglgsy456/article/details/79045850

2、必須是post請求。可以由postman模擬發送。

本文絕大部分參考博客:https://blog.csdn.net/forezp/article/details/81041062 再次對博主表示感謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM