微服务篇-C 深入理解第一代微服务(SpringCloud)_VIII 深入理解Bus消息总线

原创作者:田超凡(程序员田宝宝)

版权所有,引用请注明原作者,严禁复制转载

Part 1 理论部分

1 配置文件如何刷新?

在SpringCloud中,配置文件的刷新有手动刷新和实时刷新两种方式。

手动刷新使用Actuator端点实现,

实时刷新使用SpringCloud Bus消息总线实现。

2 什么是SpringCloud Bus消息总线?

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题连接到各个微服务实例,它广播的消息会被注册中心中所有的微服务实例监听和消费,这就是消息总线。

SpringCloud中也有对应的解决方案,SpringCloud Bus将分布式的节点用轻量的消息代理连接起来,可以很容易搭建消息总线,配合SpringCloud Config实现微服务应用配置信息的动态更新。

需要注意的是,消息总线并不属于消息中间件,消息总线只是为了能够整合消息中间件实现服务之间的消息通讯,类似于消息代理,从应用程序中传入消息并执行一些特别的操作,市面上开源的消息中间件有很多,比如ActiveMQ,RabbitMQ,Kafka,RocketMQ等等,目前SpringCloud Bus消息总线仅支持整合RabbitMQ和Kafka消息中间件。

Part 2 实践部分

Git环境搭建

使用码云环境搭建git服务器端 

Git服务器上传配置文件

命名规范 服务名称-版本.yml 例如configclient_dev.yml

搭建Eureka 注册中心

搭建config-server

Maven依赖

     <parent>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-parent</artifactId>

           <version>2.0.2.RELEASE</version>

     </parent>

     <dependencies>

           <!-- SpringBoot整合Web组件 -->

           <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-web</artifactId>

           </dependency>

           <!--spring-cloud 整合 config-server -->

           <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-config-server</artifactId>

                <version>2.0.2.RELEASE</version>

           </dependency>

           <!-- SpringBoot整合eureka客户端 -->

           <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

                <version>2.0.2.RELEASE</version>

           </dependency>

     </dependencies>

     <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

     <repositories>

           <repository>

                <id>spring-milestones</id>

                <name>Spring Milestones</name>

                <url>https://repo.spring.io/libs-milestone</url>

                <snapshots>

                     <enabled>false</enabled>

                </snapshots>

           </repository>

     </repositories>

application.yml

###服务注册到eureka地址

eureka:

  client:

    service-url:

           defaultZone: http://localhost:8100/eureka

spring:

  application:

    ####注册中心应用名称

    name: config-server

  cloud:

    config:

      server:

        git:

          ###git环境地址

          uri: https://gitee.com/ittcf/config_003.git

          ####搜索目录

          search-paths:

            - config 

      ####读取分支     

      label: master

####端口号     

server:

  port: 8888

启动config-server

@SpringBootApplication

@EnableEurekaClient

@EnableConfigServer

public class AppConfigServer {

      public static void main(String[] args) {

            SpringApplication.run(AppConfigServer.class, args);

      }

}

读取配置文件http://127.0.0.1:8888/configclient-dev.yml

搭建config-client

Maven依赖

<parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>2.0.1.RELEASE</version>

      </parent>

      <dependencyManagement>

            <dependencies>

                  <dependency>

                        <groupId>org.springframework.cloud</groupId>

                        <artifactId>spring-cloud-bus-parent</artifactId>

                        <version>2.0.0.RC2</version>

                        <type>pom</type>

                        <scope>import</scope>

                  </dependency>

            </dependencies>

      </dependencyManagement>

      <dependencies>

            <!-- SpringBoot整合Web组件 -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-config-client</artifactId>

                  <version>2.0.2.RELEASE</version>

            </dependency>

            <!-- SpringBoot整合eureka客户端 -->

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

                  <version>2.0.2.RELEASE</version>

            </dependency>

            <!--核心jar -->

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-bus-amqp</artifactId>

            </dependency>

            <!-- actuator监控中心 -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-actuator</artifactId>

            </dependency>

      </dependencies>

      <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

      <repositories>

            <repository>

                  <id>spring-milestones</id>

                  <name>Spring Milestones</name>

                  <url>https://repo.spring.io/libs-milestone</url>

                  <snapshots>

                        <enabled>false</enabled>

                  </snapshots>

            </repository>

      </repositories>

application.yml

spring:

  application:

    ####注册中心应用名称

    name:  configclient

  cloud:

    config:

    ####读取后缀

      profile: dev

      ####读取config-server注册地址

      discovery:

        service-id: config-server

        enabled: true

#####    eureka服务注册地址   

eureka:

  client:

    service-url:

           defaultZone: http://localhost:8100/eureka   

server:

  port: 8882

启动config-client

@RestController

public class ConfigClientController {

      // 年龄

      @Value("${userAge}")

      private String userAge;

      @RequestMapping("/userAge")

      public String userAge() {

            return userAge;

      }

}

@SpringBootApplication

@EnableEurekaClient

public class AppConfigClient {

      public static void main(String[] args) {

            SpringApplication.run(AppConfigClient.class, args);

      }

}

使用消息总线

三个微服务项目中都加上该依赖信息

Maven依赖

      <!--核心jar -->

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-bus-amqp</artifactId>

            </dependency>

            <!-- actuator监控中心 -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-actuator</artifactId>

            </dependency>

配置文件

###开启bus刷新

management:

  endpoints:

    web:

      exposure:

        include: bus-refresh

刷新接口 http://127.0.0.1:8882/actuator/bus-refresh

本文部分素材转载自蚂蚁课堂

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 05:38:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 05:38:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 05:38:07       82 阅读
  4. Python语言-面向对象

    2024-03-31 05:38:07       91 阅读

热门阅读

  1. Redis的基础命令详解

    2024-03-31 05:38:07       38 阅读
  2. IntelliJ IDEA 插件开发中监听用户的保存事件

    2024-03-31 05:38:07       33 阅读
  3. c入门基础题(2)

    2024-03-31 05:38:07       37 阅读
  4. SQL注入(二)

    2024-03-31 05:38:07       42 阅读
  5. shell获取多个oracle库mysql库所有的表主键

    2024-03-31 05:38:07       40 阅读
  6. vue图片压缩

    2024-03-31 05:38:07       43 阅读
  7. RK3588平台开发系列讲解(开发环境搭建)

    2024-03-31 05:38:07       38 阅读
  8. springboot和spring的区别

    2024-03-31 05:38:07       35 阅读
  9. 预处理、编译、汇编、链接过程

    2024-03-31 05:38:07       37 阅读
  10. Superset二次开发之环境搭建Clickhouse(Linux版)

    2024-03-31 05:38:07       44 阅读
  11. npm 常用命令详解 详细

    2024-03-31 05:38:07       33 阅读