上篇文章介紹了高可用的配置中心,如何在不重啓服務器的狀況下對配置文件進行更新呢?請看下面的介紹html
http://www.rabbitmq.com/downl...java
輸入http://localhost:15672,出下如下界面,說明安裝成功git
config 服務端:commonservice-config 添加以下依賴:github
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
在config 客戶端:bussnessservice-user添加web
<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.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
修改兩個項目的yml文件,加入如下配置:spring
spring: rabbitmq: host: localhost port: 5672 username: guest password: guest management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
配置說明:數據庫
在客戶端bussnessservice-user的配置文件中添加一個屬性server.time=update version1,用來測試屬性的變動服務器
package com.mayi.springcloud.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; 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 public class UserManagementController { @Value("${server.port}") String serverPort; @Value("${server.time}") String testValue; @GetMapping("/listUsers") public String ListUsers(){ /** * 模擬從數據庫查詢 */ List<Map<String, Object>> users = new ArrayList<Map<String, Object>>(); for(int i=1; i< 5; i++){ Map<String, Object> user = new HashMap<String, Object>(); user.put("id", i); user.put("name", "小明" + i); users.add(user); } return "服務器端口號: " + serverPort + " | 用戶信息: " + users.toString(); } @GetMapping("/getTestValue") public String getTestValue(){ return testValue; } }
依次啓動eureka server、config server 和 config client(啓動兩個端口,以測試批量更新配置文件)三個項目
啓動配置中心時能夠看到以下日誌:微信
啓動完成:架構
使用postman測試:
發送Post請求 http://localhost:8801/getTestValue 和 http://localhost:8802/getTestValue
修改配置文件:
server: port: 8802 time: update version100
發送post請求 http://localhost:8801/actuator/bus-refresh
注意:添加 spring-boot-starter-amqp 依賴有時會報錯,這是由於maven裏面有多個版本,須要刪除,再從新maven - update project
github: https://github.com/tianyana/s...
接下來,我會依次更新文章,直至整個架構完成,若有興趣的朋友關注做者 或 加我微信 拉你進入spring cloud社區羣
微信公衆號:java架構師修行
本公衆號從2018-5.1日 - 2019.5.1日期間,將要按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習