第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)V2.0_dev

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

一、准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq官网下载也可用我的下载好的。至于怎么使用 rabbitmq,可以参考我的操作文档。

Erlang:
链接:https://pan.baidu.com/s/1JevmH4MyAII1tIjzfUKx_Q
提取码:h9g2

Rabbitmq:
链接:https://pan.baidu.com/s/1dbK7PL6FHD4qO2KY9qXigA
提取码:s2bo

Rabbitmq操作手册:
RabbitMQ基本概念(二):windows下安装

创建父工程并引入依赖:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>config-server</module>
        <module>eureka-server</module>
        <module>config-client</module>
    </modules>

    <groupId>com.gblfy</groupId>
    <artifactId>sc-f-chapter8</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>sc-f-chapter8</name>
    <description>Demo project for Spring Boot</description>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

二、改造config-client

第1步:在pom文件加上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>

第2步:在配置文件bootstrap.yml中加上RabbitMq的配置+打开自动刷新

RabbitMq的地址localhost
用户名guest
密码guest
端口5672

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

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
server:
  port: 8881
spring:
  application:
    name: config-client
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      profile: dev
    bus:
      enabled: true
      trace:
        enabled: false
  rabbitmq:
    host: localhost
    username: guest
    password: guest
    port: 5672
#打开自动刷新
management:
 endpoints:
   web:
     exposure:
       include: bus-refresh

第3步:ConfigClientApplication启动类代码如下:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigClientApplication {

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

//    测试链接:curl -v -X POST "http://localhost:8882/actuator/bus-refresh"
//    在命令行执行以上命令即可
}

第4步:在配置类GirlConfig上面添加@RefreshScope注解

@Data
@Component
@ConfigurationProperties(prefix = "girl")
@RefreshScope
public class GirlConfig {

    private String name;

    private Integer age;
}

第5步:web访问类GirlController

@RestController
@RequestMapping("/girl")
public class GirlController {

    @Autowired
    private GirlConfig girlConfig;

    @GetMapping("/info")
    public String getGirlInfo() {
        return "name:" + girlConfig.getName() + ",age:" + girlConfig.getAge();
    }
}

第6步:依次启动

  • eureka-server
  • config-server
  • 启动两个config-client实例
  • 端口为:8881、8882

访问http://localhost:8881/girl/info 或者http://localhost:8882/girl/info

浏览器显示:

name:yuxin age:1

第7步:这时我们登录远程代码仓库将girl.name的值和girl.age的值分别修改为yuxin2,age2( 即改变配置文件name和age的值)。
在这里插入图片描述
如果是传统的做法,需要重启服务,才能达到配置文件的更新。

第8步:我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh或者http://localhost:8882/actuator/bus-refresh
在这里插入图片描述
温馨提示:

  1. 在测试之前请先把config-server和config-client的控制台清空
  2. 通过postman或者命令窗口的方式进行http请求
  3. 你会发现config-client会重新读取配置文件

第9步:这时我们再访问:http://localhost:8881/girl/info或者http://localhost:8882/girl/info

  • 浏览器显示:
name:yuxin2 age:2

在这里插入图片描述

三、此时的架构:

在这里插入图片描述
流程简述:

  1. 当git文件更改的时候
  2. 通过pc端用post 向端口为8882的config-client发送请求/bus/refresh/
  3. 客户端8882接收到请求从Server端更新配置并且发送给Spring Cloud Bus
  4. Spring Cloud bus接到消息并通知给其它客户端
  5. 其它客户端接收到通知,请求Server端获取最新配置
  6. 全部客户端均获取到最新的配置,从而使整个微服务集群都达到更新配置文件。

架构演进优化:
在这里插入图片描述
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:

1.提交代码触发post给客户端A发送bus/refresh
2. 客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus
3. Spring Cloud bus接到消息并通知给其它客户端
4. 其它客户端接收到通知,请求Server端获取最新配置
5. 全部客户端均获取到最新的配置,从而使整个微服务集群都达到更新配置文件。

本文源码下载:

dev分支(最新企业实战版本):
https://github.com/gb-heima/springcloud-practical-column/tree/dev/sc-f-chapter8

master分支(入门版本):
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目录 1.介绍............................................................................................................................................................3 2.基本概念....................................................................................................................................................3 3.报文传输....................................................................................................................................................6 3.1 帧类型..................................................................................................................................................6 3.1.1 数据帧...........................................................................................................................................6 3.1.2 远程帧...........................................................................................................................................9 3.1.3 错误帧.........................................................................................................................................10 3.1.4 过载帧......................................................................................................................................... 11 3.1.5 帧间空间..................................................................................................................................... 11 3.2 发送器/接收器的定义.........................................................................................................................12 4.报文校验..................................................................................................................................................12 5.编码..........................................................................................................................................................13 6.错误处理..................................................................................................................................................13 6.1 错误检测............................................................................................................................................13 6.2 错误标定............................................................................................................................................13 7.故障界定..................................................................................................................................................13 8.位定时要求..............................................................................................................................................15 9 增加CAN 振荡器容差...............................................................................................................................16 9.1 协议修改............................................................................................................................................17

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值