Spring Cloud Bus消息总线+rabbitmq+Gradle(Greenwich.RELEASE版本)

简介: Spring Cloud Bus消息总线+rabbitmq+Gradle(Greenwich.RELEASE版本)

准备工作


环境以及版本:


  • SpringBoot 2.1.3.RELEASE
  • SpringCloud Greenwich.RELEASE
  • Jdk 1.8
  • Gradle 5.2.1+

说明


以下改造均在子项目进行操作,父项目的github地址,下述需下载父项目用来管理公共依赖:


https://github.com/cuifuan/springcloud-tools


1.改造你的 config-server


1.1 构建配置文件 build.gradle


dependencies {
    implementation "org.springframework.cloud:spring-cloud-config-server"
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}

1.2 配置文件 application.yml


server:
  port: 7001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: git地址 #例如https://github.com/cuifuan/springcloud-configuration.git
          search-paths: 仓库文件下文件夹
          default-label: master
          username: git账号
          password: git密码
    bus:
      trace:
        enabled: true
  rabbitmq:
    host: rabbitmq地址
    port: 5672
    username: rabbit账号【默认:guest】
    password: rabbit密码【默认:guest】
    virtual-host: /
eureka:
# 修改在服务中心的地址status为 ip+端口 【例如:10.0.0.100:88】
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://springcloud-tools:8761/eureka/
info:
  app:
    description: This is Spring Cloud remote Registration center.
    name: tools-config-server
    version: 0.0
management:
  endpoint:
    bus-refresh:
      enabled: true
  endpoints:
    web:
      exposure:
        include: refresh,bus-refresh

1.3 启动类 ToolsConfigServerAppliaction.java


package store.zabbix.config;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableConfigServer
@SpringCloudApplication
@RestController
public class ToolsConfigServerAppliaction {
    public static void main(String[] args) {
        SpringApplication.run(ToolsConfigServerAppliaction.class, args);
    }
    @RequestMapping("/")
    public String home() {
        return "Hello World! My name is configserver.";
    }
}


2. 改造你的 config-client (客户端)


用来读取配置文件的


2.1 构建配置文件 build.gradle


dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}

2.2 配置文件 bootstrap.yml


spring:
  cloud:
    config:
      name: tools-config-client     #对应{application}部分
      profile: dev                  #对应{profile}部分
      #uri: http://localhost:8888/  #配置中心的具体地址
      label: master                 #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
      discovery:
        enabled: true                 #开启Config服务发现支持
        service-id: config-server   #指定配置中心的service-id,便于扩展为高可用配置集群。
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://springcloud-tools:8761/eureka/

2.2 配置文件 application.yml


server:
  port: 7003
spring:
  application:
    name: tools-config-client
  cloud:
    config:
  #配置重试机制
      retry:
        initial-interval: 2000
        max-attempts: 2000
        max-interval: 2000
        multiplier: 1.2
      fail-fast: true
    bus:
      #动态刷新配置
      refresh:
        enabled: true
      #跟踪总线事件
      trace:
        enabled: true
  rabbitmq:
    host: rabbitmq地址
    port: 5672
    username: rabbit账号【默认:guest】
    password: rabbit密码【默认:guest】
#配置actuator
# 1.X版本的springboot 配置: management.security.enabled=false  已经作废
#关闭安全认证
management:
  endpoint:
    bus-refresh:
      enabled: true
  #refresh接入点显式暴露出来
  endpoints:
    web:
      exposure:
        include: refresh,bus-refresh

2.3 启动类 ConfigClientApplication.java


注意:一定不要忘了加@RefreshScop注解


package store.zabbix.configreader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RefreshScope
@EnableDiscoveryClient
public class ConfigClientApplication {
    /**     * http://localhost:8881/actuator/bus-refresh     */
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
    @Value("${message}")
    String message;
    @GetMapping("/hello")
    public String getMessage(){
        return message;
    }
}


3.远程配置文件


image.png


4.操作流程



这个时候的读取的配置文件已发生变化


5.需要注意的坑


  • 请确定你的rabbitmq是通的,guest账户无法通过远程ip访问
  • 注意上面的ip地址,我是这边测试使用,请更改成个人正确或者本地
  • springboot2.x之后bus刷新地址更改为 /actuator/bus-refresh
  • springboot和springcloud版本要一致,其他版本没试过
相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
15天前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
115 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
6月前
|
消息中间件 Linux API
centos7 安装rabbitmq自定义版本及配置
centos7 安装rabbitmq自定义版本及配置
|
6月前
|
消息中间件 Java 网络架构
|
2月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
47 6
|
4月前
|
Java Spring
Gradle 如何确保依赖的版本兼容性
Gradle 通过依赖管理机制确保版本兼容性,使用声明式配置定义项目依赖,自动解析和下载所需库。它支持版本范围和动态版本,通过冲突解决策略选择最佳版本,保证构建的一致性和可靠性。
334 3
|
6月前
|
消息中间件 Java 测试技术
消息中间件RabbitMQ---SpringBoot整合RabbitMQ【三】
这篇文章是关于如何在SpringBoot应用中整合RabbitMQ的消息中间件。内容包括了在SpringBoot项目中添加RabbitMQ的依赖、配置文件设置、启动类注解,以及如何通过单元测试来创建交换器、队列、绑定,并发送和接收消息。文章还介绍了如何配置消息转换器以支持对象的序列化和反序列化,以及如何使用注解`@RabbitListener`来接收消息。
消息中间件RabbitMQ---SpringBoot整合RabbitMQ【三】
|
6月前
|
消息中间件 SQL 监控
RocketMQ 5.3.0 版本中 Broker IP 配置为 IPv6 的情况
【8月更文第28天】RocketMQ 是一款分布式消息中间件,支持多种消息发布和订阅模式。在 RocketMQ 5.3.0 版本中,Broker 的配置文件 `broker.conf` 允许配置 IPv6 地址。当 Broker 的 `brokerIP1` 配置为 IPv6 地址时,会对 Broker 的启动、消息推送和状态监控等方面产生影响。本文将探讨如何在 RocketMQ 中配置 IPv6 地址,并检查 Broker 的状态。
361 0
|
6月前
|
Java Spring
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
|
6月前
|
IDE API 开发工具
与Android Gradle Plugin对应的Gradle版本和Android Studio版本
与Android Gradle Plugin对应的Gradle版本和Android Studio版本
623 0
|
8月前
|
消息中间件 Java Kafka
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)

推荐镜像

更多