您好,登錄后才能下訂單哦!
本篇內容主要講解“Spring Cloud Config如何使用本地配置文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Spring Cloud Config如何使用本地配置文件”吧!
在分布式系統中,由于服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。
在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。
在spring cloud config 組件中,分兩個角色,一是config server,二是config client。
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 @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 準備客戶端配置文件
client-dev.yml文件的內容:
server: #設置成0,表示任意未被占用的端口 port: 8081 nickName: world
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; @RestController public class HelloController { @Value("${nickName}") private String nickName; @RequestMapping("/hello") public String hello() { return "hello " + nickName; } }
源碼地址 :https://github.com/sunbufu/sunbufu-cloud
總覺的使用svn或者git不如直接修改配置文件方便,特此記錄下來。
<?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>
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
在啟動類添加
@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 */ @RestController public 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>
到此,相信大家對“Spring Cloud Config如何使用本地配置文件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。