Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過(guò)程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
通過(guò)spring cloud 的消息總線,將配置github 等源代碼倉(cāng)庫(kù)的變更通知到spring cloud 的所有組件。
spring-bus 需要用到rabbitmq ,所以需要提前準(zhǔn)備rabbitmq消息隊(duì)列環(huán)境
配置中心調(diào)整
1.配置中心配置引用pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency>
2. 配置中心配置
spring: application: name: spring-config cloud: config: server: git: uri: https://github.com/halouprogramer/spring-config-repository.git # username: *** # password: *** basedir: ~/temp/gitlab rabbitmq: #增加rabbitmq的配置 host: 192.168.114.129 port: 5672 username: admin password: admin eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true server: port: 3636 management: endpoints: web: exposure: include: "*"
業(yè)務(wù)微服務(wù)中需要做的修改
1.添加pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
2. 配置文件
spring: application: name: spring-school cloud: config: discovery: enabled: true service-id: SPRING-CONFIG profile: dev bus: #bus id 不能使用默認(rèn),否則不能刷新 id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value} profiles: active: dev rabbitmq: host: 192.168.114.129 port: 5672 username: admin password: admin eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true #配置超時(shí)時(shí)間 feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 # logger-level: bus
spring cloud bus 會(huì)使用 bus id 去匹配應(yīng)用,匹配上才會(huì)刷新配置
3.編寫(xiě)測(cè)試代碼
package com.lvlvstart.spring.demo.school.service; import com.lvlvstart.spring.demo.school.dao.SchoolRepository; import com.lvlvstart.spring.demo.school.entity.School; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; /** * @author zishu.lv@baodanyun-inc.com * @description 類(lèi)描述 * @create 2019/12/9 15:53 */ @Service @RefreshScope public class SchoolService { @Value("${env}") private String env; @Autowired private SchoolRepository repository; public List<School> findAll(){ return repository.findAll(); } public School findById(String choolId){ Optional<School> school = repository.findById(choolId); if(school.isPresent()){ return school.get(); } return null; } public String getEnv(){ return env; } }
package com.lvlvstart.spring.demo.school.web; import com.lvlvstart.spring.demo.school.service.SchoolService; import org.springframework.beans.factory.annotation.Autowired; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("config-demo") public class ConfigDemoController { @Autowired private SchoolService schoolService; @GetMapping("get-env") private String getEnv(){ return schoolService.getEnv(); } }
@RefreshScope 標(biāo)記上才會(huì)被刷新配置
@RefreshScope 在Controller層使用,取不到值
利用githbu webhook 自動(dòng)實(shí)現(xiàn)刷新配置:
Payload URL 需要添加config server 開(kāi)放的monitor(monitor 為spring 自帶地址) ,如果在內(nèi)網(wǎng),可以搜索內(nèi)網(wǎng)穿透工具配置
修改倉(cāng)庫(kù)配置后,訪問(wèn)地址:http://localhost:8081/config-demo/get-env 地址也會(huì)發(fā)生改變
完整代碼訪問(wèn):https://github.com/halouprogramer/spring-cloud-demo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
欄 目:Java
下一篇:Spring框架如何使用P命名空間進(jìn)行注入
本文標(biāo)題:Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過(guò)程詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8778.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過(guò)程解析
- 01-10SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過(guò)程代碼實(shí)例
- 01-10springmvc級(jí)聯(lián)屬性處理無(wú)法轉(zhuǎn)換異常問(wèn)題解決
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解
- 01-10Spring注解和同步鎖不能同步問(wèn)題解決


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?