spring-cloud入門之spring-cloud-config(配置中心)
前言
在分布式系統(tǒng)中,由于服務(wù)數(shù)量巨多,為了方便服務(wù)配置文件統(tǒng)一管理,實(shí)時(shí)更新,所以需要分布式配置中心組件:spring-cloud-config ,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中(即本地),也支持放在遠(yuǎn)程Git倉(cāng)庫中。
本節(jié)主要演示怎么用Git倉(cāng)庫作為配置源。
開源地址:https://github.com/bigbeef
創(chuàng)建配置項(xiàng)目
在github中創(chuàng)建一個(gè)項(xiàng)目,專門用來保存我們所有項(xiàng)目的配置文件,項(xiàng)目是我的項(xiàng)目結(jié)構(gòu)
配置項(xiàng)目地址:https://github.com/bigbeef/cppba-config
eureka-server.properties
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false spring.application.name=eureka-server server.port=18761 eureka.instance.hostname=peer1 eureka.client.serviceUrl.defaultZone=http://peer1:18761/eureka/
創(chuàng)建spring-cloud-config-server項(xiàng)目
項(xiàng)目結(jié)構(gòu)如圖:
pom.xml核心代碼
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
SpringCloudConfigServerApplication.java
package com.cppba; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class SpringCloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServerApplication.class, args); } }
application.properties
這個(gè)根據(jù)自己實(shí)際的git項(xiàng)目修改配置
server.port=8888 spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/bigbeef/cppba-config spring.cloud.config.label=master # spring.cloud.config.server.git.username= # spring.cloud.config.server.git.password= spring.cloud.config.server.git.searchPaths=\ cppba-spring-cloud/*,\ cppba-spring-cloud/eureka-client/*
spring.cloud.config.server.git.uri:配置git倉(cāng)庫地址
spring.cloud.config.server.git.searchPaths:配置倉(cāng)庫路徑,以逗號(hào)隔開
spring.cloud.config.label:配置倉(cāng)庫的分支
spring.cloud.config.server.git.username:訪問git倉(cāng)庫的用戶名
spring.cloud.config.server.git.password:訪問git倉(cāng)庫的用戶密碼
啟動(dòng)項(xiàng)目
訪問地址:http://127.0.0.1:8888
http請(qǐng)求地址和資源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
根據(jù)我們自己的配置,我們可以這樣訪問:http://127.0.0.1:8888/eureka-server/default/master
application -> eureka-server (應(yīng)用名)
profile -> default (啟用的配置,通常是后綴,下面解釋)
label -> master (分支)
訪問到的結(jié)果就是:
profile比較重要,可以理解成讀取哪些配置文件,假如我不止一個(gè)配置文件,可能會(huì)有:
eureka-server.properties(這個(gè)是通用配置文件,默認(rèn)都會(huì)加載),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......
我們可能會(huì)選擇性的加載其中的部分properties配置文件,那我們可以這樣寫:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master
到此,我們的spring-cloud-config-server就簡(jiǎn)單搭起來,后面的章節(jié)我會(huì)教大家怎么在項(xiàng)目中讀取配置
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)容器ConcurrentLinkedQueue解析
這篇文章主要介紹了Java并發(fā)容器ConcurrentLinkedQueue解析,2023-12-12解決java 分割字符串成數(shù)組時(shí),小圓點(diǎn)不能直接進(jìn)行分割的問題
這篇文章主要介紹了解決java 分割字符串成數(shù)組時(shí),小圓點(diǎn)不能直接進(jìn)行分割的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例
這篇文章主要介紹了Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Quarkus集成open api接口使用swagger ui展示
這篇文章主要為大家介紹了Quarkus集成open?api接口使用swagger?ui的展示示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認(rèn)值問題
這篇文章主要介紹了解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認(rèn)值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java中Base64字符串出現(xiàn)不合法字符的問題解決
非法的base64數(shù)據(jù)可能導(dǎo)致編碼或解碼過程出錯(cuò),本文主要介紹了java中Base64字符串出現(xiàn)不合法字符的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題的幾種方法
本篇文章主要介紹了解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Java實(shí)現(xiàn)簡(jiǎn)單樹結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單樹結(jié)構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01