Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
通過spring cloud 的消息總線,將配置github 等源代碼倉庫的變更通知到spring cloud 的所有組件。
spring-bus 需要用到rabbitmq ,所以需要提前準(zhǔn)備rabbitmq消息隊列環(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 #配置超時時間 feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 # logger-level: bus
spring cloud bus 會使用 bus id 去匹配應(yīng)用,匹配上才會刷新配置
3.編寫測試代碼
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 類描述 * @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)記上才會被刷新配置
@RefreshScope 在Controller層使用,取不到值
利用githbu webhook 自動實現(xiàn)刷新配置:
Payload URL 需要添加config server 開放的monitor(monitor 為spring 自帶地址) ,如果在內(nèi)網(wǎng),可以搜索內(nèi)網(wǎng)穿透工具配置
修改倉庫配置后,訪問地址:http://localhost:8081/config-demo/get-env 地址也會發(fā)生改變
完整代碼訪問:https://github.com/halouprogramer/spring-cloud-demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IKAnalyzer使用不同版本中文分詞的切詞方式實現(xiàn)相同功能效果
今天小編就為大家分享一篇關(guān)于IKAnalyzer使用不同版本中文分詞的切詞方式實現(xiàn)相同功能效果,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Ubuntu安裝jenkins完成自動化構(gòu)建詳細步驟
Jenkins是一個開源的自動化服務(wù)器,可以用來輕松地建立持續(xù)集成和持續(xù)交付(CI/CD)管道,這篇文章主要給大家介紹了關(guān)于Ubuntu安裝jenkins完成自動化構(gòu)建的相關(guān)資料,需要的朋友可以參考下2024-03-03Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的方法
這篇文章主要介紹了Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的基本教程,JDBC是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問需要的朋友可以參考下2015-12-12Java編寫時間工具類ZTDateTimeUtil的示例代碼
這篇文章主要為大家詳細介紹了如何利用Java編寫時間工具類ZTDateTimeUtil,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11SpringBoot項目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01