Spring Cloud Config分布式配置中心使用介紹詳解
1、分布式配置中心應(yīng)用場景
往往,我們使用配置文件管理?些配置信息,比如application.yml
單體應(yīng)用架構(gòu):配置信息的管理、維護(hù)并不會顯得特別麻煩,手動操作就可以,因?yàn)榫鸵粋€工程;
微服務(wù)架構(gòu):因?yàn)槲覀兊姆植际郊涵h(huán)境中可能有很多個微服務(wù),我們不可能一個一個去修改配置然后重啟生效,在一定場景下我們還需要在運(yùn)行期間動態(tài)調(diào)整配置信息,比如:根據(jù)各個微服務(wù)的負(fù)載情況,動態(tài)調(diào)整數(shù)據(jù)源連接池大小,我們希望配置內(nèi)容發(fā)生變化的時候,微服務(wù)可以自動更新。
場景總結(jié)如下:
- 集中配置管理,一個微服務(wù)架構(gòu)中可能有成百上千個微服務(wù),所以集中配置管理是很重要的(一次修改、到處生效)
- 不同環(huán)境不同配置,比如數(shù)據(jù)源配置在不同環(huán)境(開發(fā)dev,測試test,?產(chǎn)prod)中是不同的
- 運(yùn)行期間可動態(tài)調(diào)整。例如,可根據(jù)各個微服務(wù)的負(fù)載情況,動態(tài)調(diào)整數(shù)據(jù)源連接池大小等配置修改后可自動更新
- 如配置內(nèi)容發(fā)生變化,微服務(wù)可以自動更新配置
那么,我們就需要對配置文件進(jìn)行集中式管理(相同配置),這也是分布式配置中心的作用。
2、Spring Cloud Config
2.1、Config簡介
Spring Cloud Config是一個分布式配置管理方案,包含了 Server端和 Client端兩個部分。
- Server 端:提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,通過使用@EnableConfigServer注解在 Spring boot 應(yīng)用中非常簡單的嵌?
- Client 端:通過接口獲取配置數(shù)據(jù)并初始化自己的應(yīng)用
2.2、Config分布式配置應(yīng)用
說明:Config Server是集中式的配置服務(wù),用于集中管理應(yīng)用程序各個環(huán)境下的配置。 默認(rèn)使用Git存儲配置文件內(nèi)容,也可以SVN。
比如,我們要對“簡歷微服務(wù)”的application.yml進(jìn)行管理(區(qū)分開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境)
登錄Github,創(chuàng)建項(xiàng)目lagou-config-repo
上傳yml配置文件,命名規(guī)則如下:
- {application}-{profile}.yml 或者 {application}-{profile}.properties
- 其中,application為應(yīng)用名稱,profile指的是環(huán)境(用于區(qū)分開發(fā)環(huán)境,測試環(huán)境、生產(chǎn)環(huán)境等)
- 示例:lagou-service-resume-dev.yml、lagou-service-resume-test.yml、lagouservice-resume-prod.yml
2.3、構(gòu)建Config Server統(tǒng)一配置中心
新建SpringBoot工程,引入依賴坐標(biāo)(需要注冊自己到Eureka)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>lagou-parent</artifactId> <groupId>com.lagou.edu</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lagou-cloud-configserver-9006</artifactId> <dependencies> <!--eureka client 客戶端依賴引入--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--config配置中心服務(wù)端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
配置啟動類,使用注解@EnableConfigServer開啟配置中心服務(wù)器功能
package com.lagou.edu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer // 開啟配置中心功能 public class ConfigServerApplication9006 { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication9006.class,args); } }
application.yml配置
server:
port: 9006
#注冊到Eureka服務(wù)中心
eureka:
client:
service-url:
# 注冊到集群,就把多個Eurekaserver地址使用逗號連接起來即可;注冊到單實(shí)例(非集群模式),那就寫一個就ok
defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
instance:
prefer-ip-address: true #服務(wù)實(shí)例中顯示ip,而不是顯示主機(jī)名(兼容老的eureka版本)
# 實(shí)例名稱: 192.168.1.103:lagou-service-resume:8080,我們可以自定義它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: lagou-cloud-configserver
# =================config核心配置==============
cloud:
config:
server:
git:
uri: https://github.com/5173098004/lagou-config-repo.git #配置git服務(wù)地址
username: 517309804@qq.com #配置git用戶名
password: yingdian12341 #配置git密碼
search-paths:
- lagou-config-repo
# 讀取分支
label: master
# =================config核心配置==============
#針對的被調(diào)用方微服務(wù)名稱,不加就是全局生效
#lagou-service-resume:
# ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #負(fù)載策略調(diào)整
# springboot中暴露健康檢查等斷點(diǎn)接口
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細(xì)節(jié)
endpoint:
health:
show-details: always
測試訪問:http://localhost:9006/master/lagou-service-resume-dev.yml,查看到配置文件內(nèi)容
2.4、構(gòu)建Client客戶端(在已有簡歷微服務(wù)基礎(chǔ)上)
已有工程中添加依賴坐標(biāo)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
application.yml修改為bootstrap.yml配置文件
bootstrap.yml是系統(tǒng)級別的,優(yōu)先級比application.yml高,應(yīng)用啟動時會檢查這個配置文件,在這個配置文件中指定配置中心的服務(wù)地址,會自動拉取所有應(yīng)用配置并且啟用。
(主要是把與統(tǒng)?配置中心連接的配置信息放到bootstrap.yml)
注意:需要統(tǒng)一讀取的配置信息,從集中配置中心獲取
bootstrap.yml
server:
port: 8080
spring:
application:
name: lagou-service-resume
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免將駝峰命名轉(zhuǎn)換為下劃線命名
# ===========核心配置==========
cloud:
# config客戶端配置,和ConfigServer通信,并告知ConfigServer希望獲取的配置信息在哪個文件中
config:
name: lagou-service-resume #配置文件名稱
profile: dev #后綴名稱
label: master #分支名稱
uri: http://localhost:9006 #ConfigServer配置中心地址
# ===========核心配置==========
#注冊到Eureka服務(wù)中心
eureka:
client:
service-url:
# 注冊到集群,就把多個Eurekaserver地址使用逗號連接起來即可;注冊到單實(shí)例(非集群模式),那就寫一個就ok
defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
instance:
prefer-ip-address: true #服務(wù)實(shí)例中顯示ip,而不是顯示主機(jī)名(兼容老的eureka版本)
# 實(shí)例名稱: 192.168.1.103:lagou-service-resume:8080,我們可以自定義它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
# 自定義Eureka元數(shù)據(jù)
metadata-map:
cluster: cl1
region: rn1
management:
endpoints:
web:
exposure:
include: "*"
這種配置完成后,當(dāng)我們啟動項(xiàng)目后,config配置會自動根據(jù)我們配置的url,借助于url會去github上,將我們所配置在github上的文件加載到本地配置yml文件中,我們可以定義一個類來嘗試獲取這個文件。
# Github上文件內(nèi)容如下
mysql:
url:dev-http://localhost:3306/db600
lagou:
message: hello lagou,600
package com.lagou.edu.controller; 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.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 該類用于模擬,我們要使用共享的那些配置信息做一些事情 */ @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { // 和取本地配置信息一樣 @Value("${lagou.message}") private String lagouMessage; @Value("${mysql.url}") private String mysqlUrl; // 內(nèi)存級別的配置信息 // 數(shù)據(jù)庫,redis配置信息 @GetMapping("/viewconfig") public String viewconfig() { return "lagouMessage==>" + lagouMessage + " mysqlUrl=>" + mysqlUrl; } }
到此這篇關(guān)于Spring Cloud Config分布式配置中心使用介紹詳解的文章就介紹到這了,更多相關(guān)Spring Cloud Config分布式配置中心內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目集成Swagger和swagger-bootstrap-ui及常用注解解讀
這篇文章主要介紹了SpringBoot項(xiàng)目集成Swagger和swagger-bootstrap-ui及常用注解解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過程
幾十萬上百萬行的數(shù)據(jù)是很常見的。本文主要介紹了Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端
本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02java開發(fā)分布式服務(wù)框架Dubbo調(diào)用過程
這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo調(diào)用過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟
這篇文章主要介紹了Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02基于String和List<String>間的相互轉(zhuǎn)換方式
這篇文章主要介紹了基于String和List間的相互轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05