springCloud學習-消息總線(Spring Cloud Bus)

一、簡介git

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

Spring Cloud Bus 可選的消息代理組建包括 RabbitMQ 、 AMQP 和Kafka 等。本節 講述的是用 RabbitMQ 做爲 Spring Cloud 的消息組件去刷新更改微服務的配置文件。 所以須要安裝RabbitMQ,點擊RabbitMQ下載。安裝和使用方式請自行搜索。

二、改造config-client
web

  一、此功能只須要改造config-client工程,首先在pom.xml中引入基於rabbitMQ實現的 spring cloud bus的起步依賴spring-cloud-starter-bus-amqp,代碼以下spring

<?xml version="1.0" encoding="UTF-8"?>
<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.lishun</groupId>
    <artifactId>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.lishun</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <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>

        <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>
    </dependencies>


</project>

  二、在配置文件bootstrap.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用戶名、密碼。並須要加上spring.cloud.bus的三個配置,具體以下:apache

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

spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

  三、最後 須要在更新的配置類上加@ RefreshScope 註解 只有加上了該註解,纔會在不重啓服務的狀況下更新配置 ,代碼以下
bootstrap

@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }


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

  四、依次啓動eureka-server、confg-cserver,啓動兩個config-client,端口爲:888一、8882。瀏覽器

  五、訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:架構

config-test

  六、將github上的配置文件id的值改成config-test-11,若是是傳統的作法,須要重啓服務,才能達到配置文件的更新。們只須要發送post請求:http://localhost:8881/actuator/bus-refresh,在控制檯你會發現config-client會從新讀取配置文件。app

  七、這時咱們再訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:maven

config-test-11

  /actuator/bus-refresh接口能夠指定服務,即便用」destination」參數,好比 「/actuator/bus-refresh?destination=customers:**」 即刷新服務名爲customers的全部服務。

相關文章
相關標籤/搜索