Spring Cloud Bus 消息总线

1.继承配置的东西,启动两个config-client,注意需要安装rabbitmq

2.改造config-client

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bennytitan</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>2.0.0.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>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <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-config</artifactId>
        </dependency>

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

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

        <!-- 新增依赖 -->
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.4.0</version>
        </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>

    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

application.properties

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8882

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=benny
spring.rabbitmq.password=123456

management.endpoints.web.exposure.include=*
ConfigClientApplication.java
package com.bennytitan.configclient;

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
@EnableEurekaClient
@RestController
@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;
    }
}

3.分别启动8881和8882两个config-client,刷新查看配置,然后修改远程客户端上的版本号,再刷新,发现配置没有更新

然后用postman发送post请求刷新消息总线 http://localhost:8881/actuator/bus-refresh。

然后再访问/hi接口,就看到两个client的配置都刷新好了。

原文地址:https://www.cnblogs.com/BennyTitan/p/9214294.html

时间: 2024-08-03 02:17:11

Spring Cloud Bus 消息总线的相关文章

史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

最新Finchley版本请访问:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/或者http://blog.csdn.net/forezp/article/details/81041062 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改. 本文还是基于

第九章 消息总线: Spring Cloud Bus

在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实例都连接上来, 由于该主题中产生的消息会被所有实例监听和消费, 所以我们称它为消息总线. 在总线上的各个实例都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息, 例如配置信息的变更或者其他一些管理操作等 由于消息总线在微服务架构系统中被广泛使用, 所以它同配置中心一样, 几乎是微服务架构中的必备组件. Spring Cloud 作为微服务架构综合性的解决方案,对此自然也有自己的实现, 这

第八篇:消息总线(Spring Cloud Bus)

前面几篇文章我们聊了Spring Cloud Config配置中心,当我们在更新Git上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用Spring Cloud Bus可以完美解决这一问题. 1.消息总线 Spring cloud bus通过轻量消息代理连接各个分布的节点.这会用在广播状态的变化(例如配置变化)或者其他的消息指令.Spring bus的一个核

SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

一.安装rabbitmq 二.pom父文件 <?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.ap

通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)

通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配置中心的总线服务会将此事件广播给加入总线的所有客户端,客户端收到事件后会从新读取配置中心的内容. 增加POM依赖 配置中心的服务端(spring-cloud-config-server)和客户端(spring-cloud-config-client)都加入Spring Cloud Bus引用包: <

spring cloud 使用spring cloud bus自动刷新配置

Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ.Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化或者其他的管理指令.使用Spring Cloud Bus后的架构如图9-2所示. 图9-2 使用Spring Cloud Bus的架构图 由图可知,微服务A的所有实例通过消息总线连接到了一起,每个实例都会订阅配置更新事件.当其中一个微服务节点的/bus/refresh端点被请求时,该实例就会向消息总

【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践

一.整合步骤 1)加入Maven坐标 <!-- actuator监控模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.3.RELEASE</version> </dependency> &l

SpringCloud(Greenwich版)Bus消息总线

一.Bus简介 是什么: Spring Cloud Bus 使用轻量级的消息代理(目前只支持两种,RabbitMQ 和 Kafka)连接分布式系统的节点,这样就可以广播传播状态的更改(例如配置的更新)或者其他的管理指令.可将 Spring Cloud Bus 想象成一个分布式 Spring Boot Actuator,也可以当做微服务间的通信通道. 为什么被称为消息总线: 在微服务系统架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题(订阅某学校的微信公众号 topic),并让系统中所有

(十六) 整合spring cloud云架构 -使用spring cloud Bus刷新配置

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务. 当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多.配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效. 如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响. 如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯