惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
IT之家
IT之家
GbyAI
GbyAI
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
量子位
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
Recent Announcements
Recent Announcements
月光博客
月光博客
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
罗磊的独立博客
人人都是产品经理
人人都是产品经理
U
Unit 42
宝玉的分享
宝玉的分享
V
V2EX
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LINUX DO - 热门话题
Engineering at Meta
Engineering at Meta
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
Apple Machine Learning Research
Apple Machine Learning Research
Cyberwarzone
Cyberwarzone
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
爱范儿
爱范儿
V
Vulnerabilities – Threatpost
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
S
Securelist
Google DeepMind News
Google DeepMind News
小众软件
小众软件
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LangChain Blog

博客园 - PPBoy

状态机的轮子 oracle生成path的sql语句 oracle表空间异常大 springboot2集成activiti出错 策略模式2 策略模式1 sql从n月到m月数据汇总,没有数据,当月显示0 jsp下拉列表 dao层取值用List<map<String,Object>>接收有序map 在线预览pdf springboot打jar包,调用webservice出错 导出到word ueditor后台配置项返回格式出错,上传功能将不能正常使用 js控制多层单选,多选按钮,做隐藏操作 js控制全屏及退出全屏 springboot2.0jar包启动异常 第九篇: 高可用的服务注册中心 第八篇: 服务链路追踪(Spring Cloud Sleuth) 第六篇: 分布式配置中心(Spring Cloud Config)
第七篇: 消息总线(Spring Cloud Bus)
PPBoy · 2018-08-02 · via 博客园 - PPBoy

Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。

一、准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq下载,点击erlang下载。上两个下载都是百度网盘的链接,也可以https://download.csdn.net/download/snowwolf9999/10551353帮我加点CSDN分,嘿嘿。

具体安装erlang环境,参考https://www.cnblogs.com/PPBoy/p/9335943.html

二、改造eureka-config-client工程。

在pom文件加上起步依赖spring-cloud-starter-bus-amqp和spring-boot-starter-actuator。

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

完整的配置文件如下:

<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.sun</groupId>
  <artifactId>eureka-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.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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>
        <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>
    </dependencies>
</project>

View Code

在配置文件application.yml中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的三个配置,具体如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:8888/
      bus:
        enabled: true
        trace:
          enabled:true
    discovery:
      enabled: true
      serviceId: config-server
  rabbitmq: 
    host: localhost
    port: 5672
    username: guest
    password: guest
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8881
  
management: 
  endpoints:
    web:
      exposure:
        include: bus-refresh
  

ConfigClientApplication启动类代码如下:

package com.sun;

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

这时我们去代码仓库将foo的值改为“foo version 4”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现config-client会重新读取配置文件:

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888

tip:这里发送post请求可以用postman:

 写地址发送就可以了,这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:

另外,/actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务。

三、分析

此时的架构图: 

当git文件更改的时候,通过pc端用post 向端口为8881的config-client发送请求/bus/refresh/;此时8881端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。

当然,向端口8882的服务发送也可以。

最后说一下http://localhost:8881/actuator/bus-refresh不能成功刷新,基本上有2种可能:

1、查看rabbitmq是否安装完毕,是否运行。查看rabbitmq状态,参考:https://blog.csdn.net/lglgsy456/article/details/79045850

2、必须是post请求。可以由postman模拟发送。

本文绝大部分参考博客:https://blog.csdn.net/forezp/article/details/81041062 再次对博主表示感谢。