The Simplest Spring Cloud Tutorial in History | Part 8: Message Bus (Spring Cloud Bus)

Please indicate the source for reprinting: 
http://blog.csdn.net/forezp/article/details/70148235 
This article is from Fang Zhipeng's blog

Please indicate the source when reprinting:

Spring Cloud Bus connects distributed nodes with lightweight message brokers. It can be used to broadcast configuration file changes or communication between services, and it can also be used for monitoring. This article is about using Spring Cloud Bus to notify the configuration file changes of the microservice architecture.

1. Preparation

This article is based on the previous article. 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.

2. Transform config-client

Add the starter dependency spring-cloud-starter-bus-amqp to the pom file. The complete configuration file is as follows:

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

Add the configuration of RabbitMq to the configuration file application.properties, including the address, port, username, and password of RabbitMq. The code is as follows:


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
# spring.rabbitmq.username=
# spring.rabbitmq.password=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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 the traditional method, the service needs to be restarted to achieve the update of the configuration file. At this point, we only need to send a post request: http://localhost:8881/bus/refresh , you will find that config-client will re-read the configuration file

Paste_Image.png

Reread the configuration file:

Paste_Image.png

At this time, we visit http://localhost:8881/hi  or http://localhost:8882/hi  and the 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

Architecture diagram at this time: 
Paste_Image.png

When the git file is changed, send a request /bus/refresh/ to the config-client with port 8882 through the pc side; at this time, port 8882 will send a message, which will be passed to other services by the message bus, so that the entire microservice Clusters are up to update configuration files.

4. Other extensions (negligible)

It can be used as a custom Message Broker, you only 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": "*:**"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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

5. References

spring_cloud_bus

Guess you like

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