當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Cloud Config入门(本地配置)
生活随笔
收集整理的這篇文章主要介紹了
Spring Cloud Config入门(本地配置)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自?https://www.cnblogs.com/zcr3108346262/p/7602314.html
spring cloud config 簡介
- Spring Cloud Config為分布式系統中的外部化配置提供服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。
- Spring Cloud Config 分為兩個部分 ,server端和client端。
????? server端配置服務器,管理配置信息
????? client端獲取配置信息
創建并運行一個Spring Cloud Config Server
- 創建一個名為config-server的應用,并添加spring-cloud-starter-parent,spring-cloud-config-server依賴,pom信息具體如下 <?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"><modelVersion>4.0.0</modelVersion><groupId>com.zcr.test</groupId><artifactId>config-server</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>config-server</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR6</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>
- 創建application主類,并添加@EnableConfigServer注解,代碼如下 package com.zcr.spring;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication @EnableConfigServer public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class,args);} }
- 在本地創建一個配置文件db-service-dev.properties,格式如下
按 Ctrl+C 復制代碼
按 Ctrl+C 復制代碼
- 在resources文件夾下創建application.properties,內容如下 #tomcat端口號 server.port=8888 #配置文件在本地 spring.profiles.active=native #配置文件的目錄 spring.cloud.config.server.native.search-locations=D:/workspace/GitHub/spring-config/config-file ????? 目錄結構:?????
???????????
- 運行應用并打開網址http://localhost:8888/db-service/dev,出現如下頁面,證明配置文件發布成功
?????
創建并運行一個Spring Cloud Config Client
- 創建一個名為config-client的應用,并添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依賴,pom信息具體如下 <?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"><modelVersion>4.0.0</modelVersion><groupId>com.zcr.spring</groupId><artifactId>config-client</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
- 在resources中創建bootstrap.properties(bootstrap.yml會在應用啟動之前讀取),格式如下 #文件名 spring.application.name=db-service #文件模式,默認為default spring.cloud.config.profile=dev #server端ip地址 spring.cloud.config.uri= http://localhost:8888/ #client端ip端口 server.port=8881
- 創建一個controller package com.zcr.spring;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@SpringBootApplication @RestController public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}@Value("${sck.user}")String sckUser;@Value("${sck.password}")String sckPassword;@RequestMapping("/hello")public String showUserAndPassword(){String sb = "username is " + sckUser + "," + "password is " +sckPassword;return sb;} }
- 啟動Application,并訪問http://localhost:8881/hello,出現如下界面,成功
遺留問題:1.初始化時,注入在static代碼塊之后,故靜態代碼塊無法獲取遠程配置信息。
?????????????? 2.對spring了解淺。不懂內部原理,bug調試過程中比較費勁。
?????????????? 3.對spring中的注解不了解。
源碼地址:https://github.com/310834626/spring-cloud-config
參考:http://www.jianshu.com/p/69dea19abf04
????????http://blog.didispace.com/springcloud4/
????????http://tech.lede.com/2017/06/12/rd/server/springCloudConfig/
總結
以上是生活随笔為你收集整理的Spring Cloud Config入门(本地配置)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pom 选用maven仓库
- 下一篇: Spring Cloud中Eureka开