javascript
搭建SpringCloud配置中心 Config Server
項(xiàng)目地址:?鏈接:https://pan.baidu.com/s/1Mxo0ltvZbpz_r8mCU-mSpw?
 提取碼:3j4a?
問題解答:
SpringCloud Config 分布式配置文件中心
 ? ? ?1.為什么要使用分布式配置中心?
 ?? ? ? ? ?產(chǎn)生的背景:在微服務(wù)如果使用傳統(tǒng)的方式管理配置文件,配置文件管理器非常復(fù)雜,如果生產(chǎn)環(huán)境配置文件,可能
 ?? ??? ? ? ? ? ? ? ? 需要發(fā)生改變的時(shí)候,重新打war包,重新讀取配置信息在jvm內(nèi)存中
 ?? ? 2.什么是分布式配置中心?
 ? ? ? ? ? 在微服務(wù)當(dāng)中使用同一個(gè)服務(wù)器管理所有服務(wù)配置文件信息,能夠?qū)崿F(xiàn)后臺可管理,當(dāng)服務(wù)器正在運(yùn)行的時(shí)候,
 ? ? ? ? ? 如果配置文件需要發(fā)生改變,可以實(shí)現(xiàn)不需要重啟服務(wù)器實(shí)時(shí)更改配置文件信息.
 ? ? ?3.有哪些分布式配置中心框架? (推薦使用阿波羅和zk)
 ? ? ? ? ? 1.阿波羅攜程寫分布式配置中心,有圖形界面可管理配置文件信息.配置文件信息存放在數(shù)據(jù)庫里面.
 ? ? ? ? ? 2.SpringCloud Config沒有后臺可管理分布式配置中心,配置文件信息存放在版本控制器里面.
 ? ? ? ? ? 3.使用Zookeeper實(shí)現(xiàn)分布式配置中心,持久節(jié)點(diǎn)+事件通知?? ??? ? ?
 ?? ? 4.分布式配置中心需要哪些組件?
 ? ? ? ? ? 1.Web管理系統(tǒng)--后臺可以使用圖形界面管理配置文件SpringCloud Config沒有圖像化管理配置文件
 ?? ??? ? ?2.存放分布式配置文件服務(wù)器(持久存儲服務(wù)器)--使用版本控制器存放配置文件信息,使用git環(huán)境
 ?? ??? ? ?3.ConfigServer緩存配置文件服務(wù)器(臨時(shí)緩存存放)
 ?? ??? ? ?4.ConfigClient 讀取ConfigServer配置文件信息.
搭建分布式配置中心 SpringCloud Config
 ? ? 1.搭建git環(huán)境, 目的:持久化存儲配置文件信息 采用碼云
 ?? ? ? git環(huán)境上文件夾以項(xiàng)目進(jìn)行區(qū)分
 ?? ? ? member_config會員服務(wù)配置文件
 ?? ? ? order_config訂單服務(wù)配置文件
 ?? ?2.公司項(xiàng)目中環(huán)境是如何區(qū)分?
 ? ? ? ?dev ? 開發(fā)環(huán)境
 ?? ? ? sit ? 測試環(huán)境
 ?? ? ? pre ? 預(yù)發(fā)布環(huán)境
 ?? ? ? prd ? 準(zhǔn)生產(chǎn)環(huán)境
 ?? ? ? uat ? 驗(yàn)收環(huán)境
 ?? ?3.在Git環(huán)境上創(chuàng)建配置文件命名規(guī)范
 ? ? ? 會員服務(wù)---服務(wù)名稱---member: ?服務(wù)名稱-環(huán)境.properties ? ?member-dev.properties
 ?? ?4.默認(rèn)情況下不能及時(shí)的獲取實(shí)時(shí)變更的配置文件信息
 ? ? ? springcloud分布式配置中心,可以采用手動(dòng)刷新或者自動(dòng)刷新
 ? ? ? ? ? ? 手動(dòng)刷新--需要人工調(diào)用接口,讀取最新配置文件(監(jiān)控中心) 人工調(diào)用/actuator/refresh進(jìn)行刷新(post請求)
 ?? ??? ??? ?自動(dòng)刷新--消息總線進(jìn)行實(shí)時(shí)通知---springbus
搭建步驟:
?1.首先本地安裝git (步驟鏈接如下),注冊碼云賬號
? ?步驟:https://blog.csdn.net/zhangshengqiang168/article/details/100561686
? ? ? 1.新建項(xiàng)目(?分布式配置文件? config )? ?
? ? ? 2.創(chuàng)建項(xiàng)目配置文件夾(memberconfig)? ?
? ? ? 3.創(chuàng)建配置文件(test-configClient-prd.properties和test-configClient-sit.properties)
? ? ? ? ? ? ? ? (此處是配置不同環(huán)境的配置文件(測試和準(zhǔn)生產(chǎn)))
? ? ? 4.配置文件中分別添加測試配置信息
? ? ? ? ? ? info=prd.zhang.com? ??info=sit.zhang.com
?
源碼地址 : 鏈接:https://pan.baidu.com/s/17-cFGA3fJFLF8gcnDZdWXg?
 提取碼:1q9z? ?具體見解壓說明文件
2.搭建分布式配置中心服務(wù)端 config-server
? (此時(shí)Eureka注冊中心已配置已啟動(dòng),注冊中心訪問地址? http://localhost:8000/eureka? 此處事前搭好)
? ? ?1.pom.xml依賴
<!--SpringBoot整合Eureka客戶端組件--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--SpringBoot整合config-server--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId> </dependency>? 2.application.properties配置文件配置
#服務(wù)端口 server.port=8888 #服務(wù)名稱 spring.application.name=config-server #eureka注冊中心服務(wù)地址 eureka.client.service-url.defaultZone=http://localhost:8000/eureka#config-server讀取git環(huán)境地址 直接復(fù)制碼云項(xiàng)目地址 spring.cloud.config.server.git.uri=https://gitee.com/zsqzhang/config.git#碼云賬號密碼(公開的話不用配置賬號密碼) spring.cloud.config.server.git.username=****** spring.cloud.config.server.git.password=******#讀取的分支環(huán)境(之前新建的項(xiàng)目文件夾名稱) spring.cloud.config.server.git.search-paths=memberconfig #讀取分支類型 注意看所處碼云分支類型 spring.cloud.config.label=master3.新建啟動(dòng)類AppConfigServer
@SpringBootApplication @EnableEurekaClient @EnableConfigServer //開啟config server服務(wù)器端功能 public class AppConfigServer {public static void main(String[] args) {SpringApplication.run(AppConfigServer.class, args);} }4.啟動(dòng)config-server服務(wù)端項(xiàng)目? ? 訪問 :? ip+端口/碼云新建配置文件名稱 (即可訪問當(dāng)配置文件信息) ,服務(wù)端搭建成功
訪問?http://localhost:8888/test-configClient-sit.properties? ?此接口會根據(jù)碼云修改,實(shí)時(shí)更新
3.搭建分布式配置中心客戶端 config-client
?1.添加pom.xml依賴
<!--SpringBoot整合config-client--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId> </dependency> <!--actuator監(jiān)控中心--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--SpringBoot整合Eureka客戶端組件--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--SpringBoot整合WEB組件--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>? 2.新建配置文件?bootstrap.properties?
#服務(wù)端口 server.port=8882#服務(wù)名稱 碼云配置文件test-configClient-prd.properties 的項(xiàng)目名稱 test-configClient spring.application.name=test-configClient#讀取版本環(huán)境 碼云配置文件test-configClient-prd.properties 的環(huán)境 prd spring.cloud.config.profile=prd#讀取config-server環(huán)境 讀取的配置中心名稱 spring.cloud.config.discovery.service-id=config-server#開啟讀取權(quán)限 spring.cloud.config.discovery.enabled=true ##eureka注冊中心服務(wù)地址 eureka.client.service-url.defaultZone=http://localhost:8000/eureka #監(jiān)控中心開啟所有端點(diǎn) management.endpoints.web.exposure.include=*?3.添加測試類TestController?
@RestController @RefreshScope //刷新 RefreshScope用新的環(huán)境參數(shù)重新生成Bean public class TestController {@Value("${info}")private String info;@RequestMapping("/getinfo")public String getInfo(){return info;} }4.添加啟動(dòng)類APPConfigClient
@SpringBootApplication @EnableEurekaClient public class AppConfigClient {public static void main(String[] args) {SpringApplication.run(AppConfigClient.class,args);} }5.啟動(dòng)項(xiàng)目 調(diào)用接口?http://localhost:8882/getinfo?訪問到配置信息
? ?
6.修改碼云配置文件test-configClient-prd.properties? ? info的內(nèi)容
7.使用postman調(diào)用監(jiān)控接口,手動(dòng)刷新配置,接口地址http://localhost:8882/actuator/refresh? post請求
8.再次調(diào)用??http://localhost:8882/getinfo?訪問到配置信息
?
總結(jié)
以上是生活随笔為你收集整理的搭建SpringCloud配置中心 Config Server的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: CPU FPU DSP MPU的概念介绍
- 下一篇: 《中国迈向新一代人工智能》全文来了。道翰
