Part 8: Message Bus (Spring Cloud Bus)

Reprinted from http://blog.csdn.net/forezp/article/details/70148235
This article is from Fang Zhipeng's blog

spring Cloud Bus connects distributed nodes and lightweight message brokers. This can be used to broadcast configuration file changes or other administrative tasks. A key idea is that the message bus can monitor microservices and communicate with each other as applications. This article is about the use of AMQP to notify the configuration file changes of the microservice architecture.

First, the preparation work

This article is based on the previous article to achieve. According to the official documentation, we only need to configure spring-cloud-starter-bus-amqp in the configuration file; that means we need to install rabbitmq, click rabbitmq to download. As for how to use rabbitmq, under the search engine.

<?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.forezp</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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

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


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>


In the configuration file add:

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


If rabbitmq has a username and password, just enter it.

Start eureka-server, confg-cserver in turn, start two config-clients, the ports are: 8881, 8882.

Visit http://localhost:8881/hi or http://localhost:8882/hi and the browser displays:
foo version 3


At this time, we go to the code repository and change the value of foo to "foo version 4", that is, change the value of the configuration file foo. If it is a traditional practice, you may need to restart the service to achieve the update of the configuration file. At this point, we only need to use the post request: http://localhost:8881/bus/refresh:





re-read the configuration file:




then we will visit http://localhost:8881/hi or http://localhost:8882 /hi browser displays:

foo version 4


In addition, the /bus/refresh interface can specify services, that is, use the "destination" parameter, such as "/bus/refresh?destination=customers:**", that is, refresh all services with the service name of customers, regardless of ip.

3.

Analysis of the architecture diagram at this time:




when the Git file is changed, send a request /bus/refresh/ to the config-client whose port is 8882 through the PC side with post; at this time, the 8882 port will send a message, which is sent by the message bus to the Other services are passed so that the entire microservice cluster reaches the updated configuration file.

4. Others

It can be used as a custom Message Broker, just need spring-cloud-starter-bus-amqp, and then write the configuration in the configuration file, same as above.

Tracing Bus Events:
needs to be set: spring.cloud.bus.trace.enabled=true, if you do that, then the Spring Boot TraceRepository (if it exists) will show all events and all acks sent by each service instance, such as: ( from the official website)

{
  "timestamp": "2015-11-26T10:24:44.411+0000",
  "info": {
    "signal": "spring.cloud.bus.ack",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "stores:8081",
    "destination": "*:**"
  }
  },
  {
  "timestamp": "2015-11-26T10:24:41.864+0000",
  "info": {
    "signal": "spring.cloud.bus.sent",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "customers:9000",
    "destination": "*:**"
  }
  },
  {
  "timestamp": "2015-11-26T10:24:41.862+0000",
  "info": {
    "signal": "spring.cloud.bus.ack",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "customers:9000",
    "destination": "*:**"
  }
}

Download the source code of this article:
[url]
https://github.com/forezp/SpringCloudLearning/tree/master/chapter8
[/url]

5. Reference

spring_cloud_bus

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326343201&siteId=291194637