原创

Spring Cloud 学习笔记8 消息总线(Spring Cloud Bus)

Spring Cloud 学习笔记8 消息总线(Spring Cloud Bus)

本次代码都已经提交在github上,点击这里访问,项目当前的构建状态:构建状态

本篇代码基于上篇文章的代码,见Spring Cloud 学习笔记7 高可用分布式配置中心

这里方大佬没有介绍RabbitMQ的安装,我使用k8s安装了一个,见RabbitMq on k8s(Kubernetes) windows 安装手记.

1.改造configclient

1.1修改configclient的pom.xml

增加spring-cloud-starter-bus-amqp依赖

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

此时pom.xml如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>configclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configclient</name>
    <description>Config client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-
starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>

</project>

1.2修改configclient的application.yml

增加


spring:
  rabbitmq:
    #这里按照你环境的ip配置
    host: 192.168.99.102
    port: 30672
    username: guest
    password: guest
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

1.3 修改configclient的ConfigclientApplication.java,路径configclient\src\main\java\cn\djc8\configclient

增加注解@RefreshScope@EnableDiscoveryClient
此时ConfigclientApplication.java内容如下

package cn.djc8.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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
@EnableDiscoveryClient
public class ConfigclientApplication {

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

    @Value("${foo}")
    String foo;

    @RequestMapping(value = "/getgithubconfig")
    public String getgithubconfig(){
        return foo;
    }

}

###2.测试
设置configclient为允许启动多次,见如何在IDEA启动多个Spring Boot工程实例
依次启动项目eurekaserver,configserver,configclient
configclient启动后,修改文件bootstrap.yml,端口改为7882,再启动一次
等启动完毕,查看rabbitmq的web管理页面,会发现有了两个连接了。

1.png

查看http://localhost:7881/getgithubconfig,同时查看http://localhost:7882/getgithubconfig

浏览器显示cn.djc8.

此时到github上,修改config-client-dev.properties,文件,将foo=cn.djc8改为cn.djc8.rabbitTest.

然后使用post发送工具,发送报文到:http://localhost:7881/actuator/bus-refresh或者7882

比如我用fiddle发送的post报文

2.png

此时查看http://localhost:7881/getgithubconfig,同时查看http://localhost:7882/getgithubconfig

浏览器显示cn.djc8.rabbitTest.

参考

  1. 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

本文来自:Spring Cloud 学习笔记8 消息总线(Spring Cloud Bus)-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2022年03月01日,已超过 996 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录