spring-cloud入門之spring-cloud-config(配置中心)
前言
在分布式系統(tǒng)中,由于服務(wù)數(shù)量巨多,為了方便服務(wù)配置文件統(tǒng)一管理,實時更新,所以需要分布式配置中心組件:spring-cloud-config ,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中(即本地),也支持放在遠(yuǎn)程Git倉庫中。
本節(jié)主要演示怎么用Git倉庫作為配置源。
開源地址:https://github.com/bigbeef
創(chuàng)建配置項目
在github中創(chuàng)建一個項目,專門用來保存我們所有項目的配置文件,項目是我的項目結(jié)構(gòu)
配置項目地址: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項目
項目結(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
這個根據(jù)自己實際的git項目修改配置
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倉庫地址
spring.cloud.config.server.git.searchPaths:配置倉庫路徑,以逗號隔開
spring.cloud.config.label:配置倉庫的分支
spring.cloud.config.server.git.username:訪問git倉庫的用戶名
spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼
啟動項目
訪問地址:http://127.0.0.1:8888
http請求地址和資源文件映射如下:
/{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比較重要,可以理解成讀取哪些配置文件,假如我不止一個配置文件,可能會有:
eureka-server.properties(這個是通用配置文件,默認(rèn)都會加載),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......
我們可能會選擇性的加載其中的部分properties配置文件,那我們可以這樣寫:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master
到此,我們的spring-cloud-config-server就簡單搭起來,后面的章節(jié)我會教大家怎么在項目中讀取配置
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis中基于別名typeAliases的設(shè)置
這篇文章主要介紹了MyBatis中基于別名typeAliases的設(shè)置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
spring mvc中@PathVariable / 帶斜杠方式獲取
這篇文章主要介紹了spring mvc中@PathVariable / 帶斜杠方式獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例
這篇文章主要介紹了Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例,PriorityBlockingQueue顧名思義是帶有優(yōu)先級的阻塞隊列,為了實現(xiàn)按優(yōu)先級彈出數(shù)據(jù),存入其中的對象必須實現(xiàn)comparable接口自定義排序方法,需要的朋友可以參考下2023-12-12
基于SpringBoot+FastExcel的百萬級數(shù)據(jù)導(dǎo)入導(dǎo)出完整方案
本文詳細(xì)介紹了在SpringBoot3.5.11+JDK17+MySQL環(huán)境下,使用FastExcel實現(xiàn)百萬級數(shù)據(jù)的導(dǎo)入和導(dǎo)出,內(nèi)容包括數(shù)據(jù)庫表設(shè)計、百萬測試數(shù)據(jù)生成、實體類與Mapper、配置文件、百萬數(shù)據(jù)導(dǎo)入、百萬數(shù)據(jù)導(dǎo)出、線程池、阻塞隊列、CountDownLatch的設(shè)計思路與原理,以及接口測試方法2026-03-03

