把原先得設定檔改成依靠 Config-Server 提供的
增加 spring-cloud-starter-config 跟 spring-boot-starter-actuator
build.gradle
記得更新一下依賴
把原本的application.properties重新命名為bootstrap.properties並改成以下內容
bootstrap.properties
spring.application.name 應用自己的名稱,到時候可以從介面上看到,也必須對應到設定檔的名稱
spring.cloud.config.uri Config-Server的位置
增加個控制器可以顯示從Condif-Server得到的資料
只要啟動後可以在 http://localhost:8000/message 取得 HELLO world! 的資料
注意 Port 變了喔,因為一開始就從 Config-Server 取得 reservation-service.properties 的內容,也取得了 message=HELLO world! 的內容來呈現。
加上 @RefreshScope 用意是當設定檔有變更時,你可以透過 URL 來觸發更新
閱讀更多 Java高級開發工程師 的文章