1. 程式人生 > 實用技巧 >springcloud(8)-訊息匯流排

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

git修改後,執行FreshConfigUtil,重新整理http://127.0.0.1:8012/products