springcloud(8)-消息总线

1.在view端pom.xml增加如下。

<!--用于访问路径/actuator/bus-refresh-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!--用于访问RabbitMQ-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.bootstrap.yml增加

spring:
    bus:
      enabled: true
      trace:
        enabled: true
rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

3.application.yml增加

management:
  endpoints:
    web:
      exposure:
        include:  "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

4.新增端口检测,在启动类中

//判断 rabiitMQ 是否启动
int rabbitMQPort = 5672;
if(NetUtil.isUsableLocalPort(rabbitMQPort)) {
    System.err.printf("未在端口%d 发现 rabbitMQ服务,请检查rabbitMQ 是否启动", rabbitMQPort );
    System.exit(1);
}

5.FreshConfigUtil刷新

public class FreshConfigUtil {

    public static void main(String[] args) {
        HashMap<String,String> headers =new HashMap<>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        System.out.println("因为要去git获取,还要刷新config-server, 会比较卡,所以一般会要好几秒才能完成,请耐心等待");

        String result = HttpUtil.createPost("http://localhost:8012/actuator/bus-refresh").addHeaders(headers).execute().body();
        System.out.println("result:"+result);
        System.out.println("refresh 完成");
    }
}

6.打开微服务http://127.0.0.1:8012/products

image-20201116141238167

git修改后,运行FreshConfigUtil,刷新http://127.0.0.1:8012/products

image-20201116141804283
posted @ 2020-11-17 14:54  脑袋有点大  阅读(66)  评论(0编辑  收藏  举报