美文网首页程序员
springcloud之bus

springcloud之bus

作者: GG_lyf | 来源:发表于2021-01-10 10:34 被阅读0次

前言

  在使用了配置中心之后,会出现在这种要求:即开发人员想在配置文件中添加一些自定义的东西,配置文件都在云端,肯定好改,但是在修改之后怎么办?重启微服务?但是在这时候有用户在使用这个服务,重启微服务工程中就会出现用户的数据丢失,用户的体验极差。会遭到用户的投诉。这该怎么办呢?bus就是为了解决这个问题的。它可以用消息队列的发布订阅模型,让所有为服务来订阅这个事件,当这个事件发生改变了,就可以通知所有微服务去更新它们的内存中的配置信息。这时你只需要在springcloud Config Server端发出refresh,就可以触发所有微服务更新了。


开搞

1.使用之前创建的config微服务

2.添加依赖

<!--bus监听rabbitMq-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-bus</artifactId>
   </dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

3.添加yml的内容

spring:
  rabbitmq:
    addresses: localhost

management:    #暴露触发消息总线的地址
  endpoints:
    web:
      exposure:
        include: bus-refresh

4.被代理的微服务端添加依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

5.在云端添加监听消息队列的配置

spring:
  rabbitmq:
    host: localhost

6.在云端的配置文件中添加自定义的东西,例如

ip:
  127.0.0.1吃的吃大餐

7.在微服务的controller中使用@Value("${ip}"),获取云端的自定义配置,并在某一个方法中输出进行查看

8.使用postman发送http://127.0.0.1:config的端口/actuator/bus-refresh,并且开始进行端口的访问

发送请求

9.发现想要的自定义配置并没有输出出来,是因为在那个ciontroller上少个@RefreshScope

@RefreshScope
@RestController
@RequestMapping("test")
public class ConsumeController {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${ip}")
  private String ip;

  @GetMapping("/showInfo")
  public String showInfo() {
    System.out.println(ip);
    return restTemplate.getForObject("http://client/home", String.class);//获取某一个客户端的那个方法
  }

}

10.添加之后重启微服务,并且在冲完气之后修改云端的自定义配置,重新访问


云端的 访问的

相关文章

  • springcloud之bus

    前言   在使用了配置中心之后,会出现在这种要求:即开发人员想在配置文件中添加一些自定义的东西,配置文件都在云端,...

  • SpringCloud之Bus组件

    Bus组件的作用,当我们把配置文件保存在Git当中,由Git进行管理,一旦配置字段的值进行改变不需要采用服务器重启...

  • SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了。 言归正传,之前我们聊了...

  • RabbitMq windows版本的配置文件位置问题

    最近现在学习SpringCloud的过程之中,SpringCloud Bus模块,需要使用到Mq,官方默认推荐使用...

  • SpringCloud(第 037 篇)通过bus/refres

    SpringCloud(第 037 篇)通过bus/refresh半自动刷新ConfigClient配置 一、大致...

  • org.springframework.amqp.core.De

    springcloud整合rabbitmq和bus,启动报错, 经检查是springboot版本不对,更换成

  • SpringCloud Bus消息总栈

    分布式配置自动刷新配置功能,springCloud Bus配合Spring Cloud Config使用可以实现配...

  • SpringCloud文章大全

    SpringCloud介绍 SpringCloud组件之Eureka SpringCloud组件之Eureka深入...

  • 消息总线-Springcloud Bus

    什么是消息总线 在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实...

  • SpringCloud Bus 消息总线

    上一篇文章我们介绍了配置中心,但是现在更新配置还是要重启啊!?所以我们本章就来介绍一个不重启的方法。 相关资料以及...

网友评论

    本文标题:springcloud之bus

    本文链接:https://www.haomeiwen.com/subject/qmvboktx.html