2021-04-14

学习目标:

掌握spring cloud bus 的配置,并成功配置运行。

学习内容:

1、 最近在学习spring cloud微服务的相关知识,遇到一spring cloud bus的坑真的很坑人,先放一下开发环境。
  1. jdk1.8.0_172
  2. 开发工具idea2019
  3. apache-maven-3.6.1
  4. springBoot2.4.4
  5. spring-cloud2020.0.2

2、遇坑经过,此前已经创建了eureka注册中心,config统一配置中心,普通的springBoot项目order。现在想要将order项目的配置文件改成从gitee中获取,从而达到修改配置文件后,时时更新,不需要重启项目的目的。消息队列RabbitMQ也已经环境也已经搭建好。
图片1
3.经过在统一配置中心config项目pom.xml文件中引入spring-cloud-starter-bus-amqp,spring-boot-starter-actuator,spring-boot-starter-web三个jar,

<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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

以及在config项目的配置文件application.yml中配置相关信息。
application.yml

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xxxx/config-repo
          username: xxxxx
          password: xxxxx
          basedir: D:\dev\Workspaces\springCloud\configgitee\basedir
    bus:
      enabled: true
  rabbitmq:
    host: 192.168.254.130
    port: 5672
    username: guest #rabbitmq默认用户名
    password: guest #rabbitmq默认密码
management:
  endpoints:
    web:
      exposure:
        include: '*' #打开全部请求端点 include: refresh,health,info #打开部分
  endpoint:
    env:
      enabled: true
    bus-refresh:
      enabled: true
    health:
      show-details: always
    mappings:
      enabled: true
  server:
    port: 8089 #自定义spring cloud bus 访问端口

order项目的配置文件bootstrap.yml(注意名字不是application.yml),

spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

order项目的pom.xml文件引入依赖

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

3、依次启动注册中心eureka项目,配置中心config项目,order项目。三个项目启动正常没有报错。其中order项目在启动时会向配置中心config项目请求加载order-dev.xml文件作为order项目启动时的配置文件。
图片

4、现在我想要做的是在gitee中修改order-dev.xml文件中的内容env:
test为env:test5后(http://localhost:8081/env/print),使用PostMan工具发送POST请求访问 http://localhost:8089/actuator/bus-refresh(我本地配置中心项目config的访问路径),让配置中心从gitee中加载修改后的内容后发送广播通知order项目去更新内容。
http://localhost:8081/env/print

5、此前一切都很顺利,直到我使用postman工具发送http://localhost:8089/actuator/bus-refresh请求时报以下错误
报错

{
    "timestamp": "2021-04-14T09:49:44.818+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/actuator/bus-refresh"
}

这个问题搞了一天都没找到是什么问题,也没解决。网上也找不到解决的办法。好抓狂啊!第二天到spring cloud bus 官网去看了一遍官网的东西依然没能解决。还好只是在学习,不是在工作中遇到的,要不真的要被气死。
6、最后想在看看spring could bus 暴露出来的所有访问链接,于是在浏览器中访问了本地config项目的http://localhost:8089/actuator,链接。得到下面的结果
访问结果图片

猛然发现spring cloud bus刷新时,使用的是http://localhost:8089/actuator/busrefresh而不是http://localhost:8089/actuator/bus-refresh,还以为自己搞错了,于是到spring cloud bus 官网确认了一下,官网确实写的是**/actuator/bus-refresh**
图片
代码里的访问路径居然和官网的不一致。
7、于是我改成http://localhost:8089/actuator/busrefresh
成功访问图片
再刷新http://localhost:8081/env/print请求,果然配置文件已经发生变化
成功访问正确结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值