Spring Cloud Config 使用本地配置文件方式
一、簡(jiǎn)介
在分布式系統(tǒng)中,由于服務(wù)數(shù)量巨多,為了方便服務(wù)配置文件統(tǒng)一管理,實(shí)時(shí)更新,所以需要分布式配置中心組件。
在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中(即本地),也支持放在遠(yuǎn)程Git倉(cāng)庫(kù)中。
在spring cloud config 組件中,分兩個(gè)角色,一是config server,二是config client。
二、配置
2.1 Spring Cloud Config Server項(xiàng)目
1 pom.xml中導(dǎo)入Config Server需要的包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
2 在Application類(lèi)中添加@EnableConfigServer注解
package com.sunbufu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
3 修改配置文件application.yml,指定本地客戶端配置文件的路徑
spring: profiles: active: native cloud: config: server: native: searchLocations: F:/conf
4 準(zhǔn)備客戶端配置文件
client-dev.yml文件的內(nèi)容:
server: #設(shè)置成0,表示任意未被占用的端口 port: 8081 nickName: world
2.2 Spring Cloud Config Client項(xiàng)目
1 pom.xml中導(dǎo)入Config Client需要的包(注意,此處跟Config Server的配置不同)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
2 在src/main/resources中,新建bootstrap.yml文件
bootstrap文件會(huì)在application文件之前加載,一般是不會(huì)變的。
spring: application: name: client cloud: config: uri: http://127.0.0.1:8888 profile: dev label: master
資源文件映射如下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
3 新建HelloController用來(lái)顯示讀取到的配置
package com.sunbufu.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${nickName}") private String nickName; @RequestMapping("/hello") public String hello() { return "hello " + nickName; } }
三、總結(jié)
源碼地址 :https://github.com/sunbufu/sunbufu-cloud
總覺(jué)的使用svn或者git不如直接修改配置文件方便,特此記錄下來(lái)。
spring cloud config本地讀取配置文件
1、創(chuàng)建maven項(xiàng)目,引入spring boot 起步依賴(lài)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>cloud-config-demo3</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-config-demo3</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
新建config-server 模塊,引入依賴(lài)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wxz</groupId> <artifactId>cloud-config-demo3</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、配置文件:
spring: cloud: config: server: native: search-locations: classpath:/shared profiles: active: native application: name: config-server server: port: 8769
在resources下新建目錄shared,里面新建文件config-client-dev
server: port: 8762 foo: foo version 1
在啟動(dòng)類(lèi)添加
@EnableConfigServer
新建config-client模塊
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wxz</groupId> <artifactId>cloud-config-demo3</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
新建配置文件bootstrap.yml(bootstrap比application具有優(yōu)先的讀取順序)
spring: cloud: config: uri: http://localhost:8769 fail-fast: true application: name: config-client profiles: active: dev
新建一個(gè)controller進(jìn)行測(cè)試:
package com.wxz.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Wangxingze * @date 2019-08-26 12:58 */ @RestController public class Test { @Value("${foo}") public String foo; @GetMapping("/t") public String t(){ return foo; } }
依次啟動(dòng)server client,啟動(dòng)時(shí)可以看到讀取了配置文件和啟動(dòng)黛安克,訪問(wèn):
注意:spring boot 和cloud的版本以及config依賴(lài)的版本
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
我這里好像沒(méi)有指定spring cloud 的版本呀
config的相關(guān)依賴(lài)使用:<version>2.1.2.RELEASE</version>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之集合框架與常用算法詳解
Java集合框架是Java中常用的數(shù)據(jù)結(jié)構(gòu)庫(kù),包括List、Set、Map等多種數(shù)據(jù)結(jié)構(gòu),支持快速的元素添加、刪除、查找等操作,可以用于解決各種實(shí)際問(wèn)題。Java中也有多種常用算法,如排序、查找、遞歸等,在數(shù)據(jù)處理和分析中有廣泛應(yīng)用2023-04-04JavaMail入門(mén)教程之創(chuàng)建郵件(2)
這篇文章主要介紹了JavaMail入門(mén)教程之創(chuàng)建郵件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)
跨域是指一個(gè)域下的文檔或腳本試圖去請(qǐng)求另一個(gè)域下的資源,或者涉及到兩個(gè)不同域名的資源之間的交互,由于同源策略(Same Origin Policy)的限制,瀏覽器不允許跨域請(qǐng)求,本文小編給大家分享了SpringBoot實(shí)現(xiàn)跨域的幾種常用方式,需要的朋友可以參考下2023-09-09java?安全?ysoserial?CommonsCollections6?分析
這篇文章主要介紹了java?安全?ysoserial?CommonsCollections6示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10java實(shí)現(xiàn)可視化界面肯德基(KFC)點(diǎn)餐系統(tǒng)代碼實(shí)例
這篇文章主要介紹了java肯德基點(diǎn)餐系統(tǒng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05java返回前端實(shí)體類(lèi)json數(shù)據(jù)時(shí)忽略某個(gè)屬性方法
這篇文章主要給大家介紹了關(guān)于java返回前端實(shí)體類(lèi)json數(shù)據(jù)時(shí)忽略某個(gè)屬性的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法
這篇文章主要給大家介紹了關(guān)于struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-1025行Java代碼將普通圖片轉(zhuǎn)換為字符畫(huà)圖片和文本的實(shí)現(xiàn)
這篇文章主要介紹了25行Java代碼將普通圖片轉(zhuǎn)換為字符畫(huà)圖片和文本的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java HashSet(散列集),HashMap(散列映射)的簡(jiǎn)單介紹
這篇文章主要介紹了Java HashSet(散列集),HashMap(散列映射)的簡(jiǎn)單介紹,幫助大家更好的理解和學(xué)習(xí)Java集合框架的相關(guān)知識(shí),感興趣的朋友可以了解下2021-01-01