摘要:創(chuàng)建統(tǒng)一服務(wù)項(xiàng)目可以使用來初始化項(xiàng)目,選擇自己的以來就好。動(dòng)態(tài)刷新配置目前如果我們修改了上的配置并不能馬上生效,需要我們的客戶端工程重啟才行,現(xiàn)在需要改造成自動(dòng)刷新。
一直使用springboot搭建后端項(xiàng)目,所有的配置都寫到自己的resource目錄下,隨著微服務(wù)的項(xiàng)目越來越多,每個(gè)項(xiàng)目都需要自己的各種配置文件。而且后期一旦想要修改配置文件,就得重新發(fā)布一遍非常的麻煩,現(xiàn)在就來教教大家怎么統(tǒng)一在github上管理 這些配置,并做到一處修改處處生效,不需要重新發(fā)布項(xiàng)目。1 創(chuàng)建統(tǒng)一服務(wù)項(xiàng)目
可以使用STS來初始化項(xiàng)目,選擇自己的以來就好。
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.mike config-server 0.0.1-SNAPSHOT config-server config server 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
創(chuàng)建bootstrap.yml文件,當(dāng)然你可以使用application.yml或application.properties
spring: application: name: config-repo cloud: config: server: git: uri: https://github.com/mike/config-repo.git #github倉庫地址 username: mike # 用戶名 password: 123456 # 密碼
在github上創(chuàng)建一個(gè)config-repo倉庫,并添加配置文件:
兩個(gè)不同環(huán)境的配置
hello-pj-dev.yml
hello: text: hello spring dev
hello-pj-uat.yml
hello: text: hello spring uat
創(chuàng)建啟動(dòng)類:
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
啟動(dòng)應(yīng)用,訪問:
http://localhost:8080/hello-pj-dev.yml
http://localhost:8080/hello-pj-uat.yml
你就可以看到遠(yuǎn)程的配置中心。
pom
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.mike hello-server 0.0.1-SNAPSHOT hello-server hello server 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
創(chuàng)建bootstrap.yml文件,只能是這個(gè)文件,不能是application文件
server: port: 8082 spring: application: name: hello-pj cloud: config: profile: dev uri: http://localhost:8080/
這里面配置了讀取遠(yuǎn)程配置文件的uri,注意application.name必須對(duì)應(yīng)github上的文件名,最終訪問的是application.name+profile
創(chuàng)建測(cè)試controller
@RestController public class TestController { @Value("${hello.text}") private String text; @GetMapping("/say") public String sayHello(){ return text; } }
訪問 http://localhost:8082/say,你就可以看到對(duì)應(yīng)的配置。這樣你只需要在github上管理所有的配置就行了,但是記得"config-server"工程得一直啟動(dòng),通過它我們才能獲取github上的配置。
3 動(dòng)態(tài)刷新配置目前如果我們修改了github上的配置并不能馬上生效,需要我們的客戶端工程重啟才行,現(xiàn)在需要改造成自動(dòng)刷新。
在客戶端工程中加入新的依賴:
??????????? org.springframework.cloud ?? ? ??????spring-cloud-starter-bus-amqp ???????
修改bootstrap.yml文件
server: port: 8082 spring: application: name: hello-pj cloud: config: profile: dev uri: http://localhost:8080/ bus: trace: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest
注意需要借助rabbitmq,所以本地需要啟動(dòng)rabbitmq。
在需要刷新的地方加入注解@RefreshScope
@RestController @RefreshScope public class TestController { @Value("${hello.text}") private String text; @GetMapping("/say") public String sayHello(){ return text; } }
這樣我們既可以隨時(shí)修改github上的配置文件,而不需要重啟應(yīng)用。
如果對(duì)你有幫助,希望可以關(guān)注下我的公眾號(hào):
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/74333.html
摘要:介紹是基于微服務(wù)基礎(chǔ)腳手架對(duì)于日常開發(fā)而言提供基礎(chǔ)權(quán)限控制,動(dòng)態(tài)菜單,才用前后端分離架構(gòu),前臺(tái)采用后臺(tái)使用提供接口。對(duì)于以后開發(fā),只需要在添加業(yè)務(wù)模塊即可,大大減少工作量。 介紹 panda是基于SpringCloud Finchley.SR1 、SpringBoot 2.x、 vue、element-ui 微服務(wù)基礎(chǔ)腳手架對(duì)于日常開發(fā)而言提供基礎(chǔ)權(quán)限控制,動(dòng)態(tài)菜單,才用前后端分離架構(gòu)...
摘要:開公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來越多時(shí),大家搜索起來就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章系列處理登錄請(qǐng)求前后端分離一使用完美處理權(quán)限問題前后端分離二使用完美處理權(quán)限問題前后端分離三中密碼加鹽與中異常統(tǒng)一處理 開公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來越多時(shí),大家搜索起來就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章! Spring Boo...
摘要:而在這個(gè)微服務(wù)下,同樣需要進(jìn)行數(shù)據(jù)操作,我不可能還要在下再一次進(jìn)行集成,這樣大大的增加了代碼量。其次,是將有關(guān)數(shù)據(jù)操作的都單獨(dú)部署成一個(gè)模塊,比如我集成的模塊,集成的模塊,使用作為內(nèi)存緩存模塊。 前言 相對(duì)于 spring 對(duì) mybatis 以及 redis 等的整合所需要的各種配置文件,在 springboot 下,已經(jīng)大大的簡(jiǎn)化了,你可能只是需要增加個(gè)依賴,加個(gè)注解,然后在配置文...
閱讀 562·2021-10-09 09:44
閱讀 2252·2021-09-02 15:41
閱讀 3618·2019-08-30 15:53
閱讀 1882·2019-08-30 15:44
閱讀 1345·2019-08-30 13:10
閱讀 1265·2019-08-30 11:25
閱讀 1538·2019-08-30 10:51
閱讀 3425·2019-08-30 10:49