Spring Cloud Config 使用本地配置文件方式
在分布式系統中,由于服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。
在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。
在spring cloud config 組件中,分兩個角色,一是config server,二是config client。
二、配置2.1 Spring Cloud Config Server項目1 pom.xml中導入Config Server需要的包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId></dependency>
2 在Application類中添加@EnableConfigServer注解
package com.sunbufu;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic 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 準備客戶端配置文件
client-dev.yml文件的內容:
server: #設置成0,表示任意未被占用的端口 port: 8081nickName: world2.2 Spring Cloud Config Client項目
1 pom.xml中導入Config Client需要的包(注意,此處跟Config Server的配置不同)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency>
2 在src/main/resources中,新建bootstrap.yml文件
bootstrap文件會在application文件之前加載,一般是不會變的。
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用來顯示讀取到的配置
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;@RestControllerpublic class HelloController { @Value('${nickName}') private String nickName; @RequestMapping('/hello') public String hello() {return 'hello ' + nickName; }}
源碼地址 :https://github.com/sunbufu/sunbufu-cloud
總覺的使用svn或者git不如直接修改配置文件方便,特此記錄下來。
spring cloud config本地讀取配置文件1、創建maven項目,引入spring boot 起步依賴<?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 模塊,引入依賴
<?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-serverserver: port: 8769
在resources下新建目錄shared,里面新建文件config-client-dev
server: port: 8762foo: foo version 1
在啟動類添加
@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具有優先的讀取順序)
spring: cloud: config: uri: http://localhost:8769 fail-fast: true application: name: config-client profiles: active: dev
新建一個controller進行測試:
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 */@RestControllerpublic class Test { @Value('${foo}') public String foo; @GetMapping('/t') public String t(){return foo; }}
依次啟動server client,啟動時可以看到讀取了配置文件和啟動黛安克,訪問:
注意:spring boot 和cloud的版本以及config依賴的版本
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
我這里好像沒有指定spring cloud 的版本呀
config的相關依賴使用:<version>2.1.2.RELEASE</version>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: