springcloud动态刷新全局广播 bus中的rabbitmq 的案例

前言:需要的技术 gitee rabbitmq springcloud -bus 的整合
我这里用的是服务器运行的rabbitmq

第一步:
检查rabbitmq是否运行

systemctl status rabbitmq-server

如果显示
在这里插入图片描述
如果显示不是这个 是 auto-statrt 或者 start 表示没有连接成功 解决方法
解决方法

如成功 就在页面访问 看看自己的账号密码是否错误

重点来了

如果初次使用rabbitmq的小伙伴 这里小心了 下面的yml文件可能有坑 我就是在这里错的 一定要设访问权限 最高级的 并且springcloud默认扫苗 “/” 下面的 如果用自己的 用户名密码的话 就要设置虚拟主机

要不然访问不到 后面的会错误一大堆

第二步:在gitee上传自己的配置文件 交给远程服务 如果没有这几个配置文件的话 就去我的gitee
上面去clone 然后自己上传到本地仓库
仓库位置

第三步:写程序
在这里插入图片描述
这里用到了 这几个
7001 7002 服务注册中心 eureka

7001 7002 这里就不重点说了 代码自行 gitee 下载

3344 表示服务主机 用来散发消息的
在这里插入图片描述
这里用到了 这一种 传播方式

3355 3366 是他们的客服端
接下来上程序

3344

pom依赖

<?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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>


    <dependencies>
        <!--    添加消息总线RabbitMQ支持    -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

application.yml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/panmengtao/springcloud-config.git #GitHub上面的git仓库名字
          #搜索目录
          search-paths:
            - springcloud-config
          #git的用户名
          username: ppp
          #git的密码
          password: 2856716665www
          # 指定分支
          default-label: master
          #rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: pan
    password: 12356

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka



management:    #rabbitmq 暴露bus刷新端点
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

这里注意了 rabbitmq 要放到spring下面 并且与cloud 对其 要不然错误

主启动类 ConfigCenterMain3344

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}

同样 3355

<?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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>
    <dependencies>
        <!--添加消息总线RabbitMQ支持-->
        <!--rabbitmq 消息总线-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

bootstrap.yml 这里不能用 application了 应为 bootstrap.yml 是系统级别的 优先级加载
详细请看
优先级

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master #主分支
      name: config #配置文件名称
      profile: dev #读取后缀名称 上面三个 连城 master-config-dev.yml  现在不去连接gitee直接连接3344
      uri: http://localhost:3344

  #rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: pan
    password: 12356


#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka


#暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"

著启动类 ConfigClientMain3355

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}

controller 类

package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope  //让3355  跟着3344  在修改github的时候一块更新
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

3366 同样的 跟上面 这里就不写了

好接下来测试

先启动 7001 7002
然后启动 3344
最后启动 3355 3366

运行3344
http://config-3344.com:3344/master/config-dev.yml
在这里插入图片描述

同样 3355 3366
http://localhost:3355/configInfo

在这里插入图片描述

接下来 就是重点了 与config 的 区别 就体现出来了 config不能修改一个地方其他的都跟着修改
加上了 bus 就可以了
接下来 我们修改配置文件
在这里插入图片描述
找到 config-dev.yml
我们修改以下版本
在这里插入图片描述
改成
在这里插入图片描述
10 改成 11
提交

这里注意了 我们这是并没有从新启动 从新启动
接下来 运行doc 命令

curl -X POST "http://localhost:3344/actuator/bus-refresh"

在这里插入图片描述
要是这里有错误的话 就把 post改成小写
接下来 刷新 3344
在这里插入图片描述
变成了 11

刷新3355 3366
在这里插入图片描述
成功 了

你成功了吗 ?

总结:说几个重点 要是有错误的话 就看看自己空格位置对吗? rabbitmq 权限 给了吗 等

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值