Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
通過spring cloud 的消息總線,將配置github 等源代碼倉庫的變更通知到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.編寫測(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 類描述 * @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 開放的monitor(monitor 為spring 自帶地址) ,如果在內(nèi)網(wǎng),可以搜索內(nèi)網(wǎng)穿透工具配置
修改倉庫配置后,訪問地址:http://localhost:8081/config-demo/get-env 地址也會(huì)發(fā)生改變
完整代碼訪問:https://github.com/halouprogramer/spring-cloud-demo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringCloud之動(dòng)態(tài)刷新、重試、服務(wù)化的實(shí)現(xiàn)
- spring boot 配置動(dòng)態(tài)刷新實(shí)現(xiàn)詳解
- Spring Cloud 動(dòng)態(tài)刷新配置信息教程詳解
- SpringCloud配置刷新原理解析
- springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁方法
- Spring實(shí)現(xiàn)上拉刷新和下拉加載效果
- SpringMVC結(jié)合ajaxfileupload實(shí)現(xiàn)文件無刷新上傳代碼
- SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳
相關(guān)文章
IKAnalyzer使用不同版本中文分詞的切詞方式實(shí)現(xiàn)相同功能效果
今天小編就為大家分享一篇關(guān)于IKAnalyzer使用不同版本中文分詞的切詞方式實(shí)現(xiàn)相同功能效果,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Ubuntu安裝jenkins完成自動(dòng)化構(gòu)建詳細(xì)步驟
Jenkins是一個(gè)開源的自動(dòng)化服務(wù)器,可以用來輕松地建立持續(xù)集成和持續(xù)交付(CI/CD)管道,這篇文章主要給大家介紹了關(guān)于Ubuntu安裝jenkins完成自動(dòng)化構(gòu)建的相關(guān)資料,需要的朋友可以參考下2024-03-03Java對(duì)象布局(JOL)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java對(duì)象布局(JOL)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java編程中使用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計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法舉例
最近在工作中遇見一個(gè)小需求,要求計(jì)算兩個(gè)日期之間相差幾天,下面這篇文章主要給大家介紹了關(guān)于java計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06Java編寫時(shí)間工具類ZTDateTimeUtil的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫時(shí)間工具類ZTDateTimeUtil,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01