spring cloud 配置中心
1. spring cloud配置中心server
1.1 創(chuàng)建git倉(cāng)庫(kù)
首先在github上搭建一個(gè)存儲(chǔ)配置中心的倉(cāng)庫(kù),需要?jiǎng)?chuàng)建兩個(gè)分支,一個(gè)是master,一個(gè)是dev分支。自己學(xué)習(xí)可以用公開庫(kù),真實(shí)環(huán)境使用的話,還是需要私庫(kù),或者自己搭建git服務(wù)器。
?
?
1.2 搭建server
使用spring cloud搭建服務(wù)器,工具使用idea,新建maven工程,配置pom.xml文件
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies>創(chuàng)建springboot啟動(dòng)類,啟動(dòng)類要加上@SpringBootApplication 和?@EnableConfigServer 這兩個(gè)注解。
@SpringBootApplication: springboot啟動(dòng)注解
@EnableConfigServer: springcloud config server的注解,必須要加上
@SpringBootApplication@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
創(chuàng)建配置文件bootstrap.yml
spring:cloud:config:server:git:uri: https://github.com/hanggle/ConfigCenter.git #git倉(cāng)庫(kù)地址,就是剛才創(chuàng)建的git倉(cāng)庫(kù)skipSslValidation: true #跳過校驗(yàn)basedir: d:///myspace///config-center///config #從git倉(cāng)庫(kù)拉取到的文件在本地存儲(chǔ)的位置,可自行修改或刪掉,默認(rèn)存儲(chǔ)在C盤# bootstrap: true server:port: 8889config-server的目錄結(jié)構(gòu):
啟動(dòng)springboot,啟動(dòng)成功后可以在瀏覽器查看拉取到的配置信息,路徑的訪問有以下幾種:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties我創(chuàng)建的文件名是:application-dev.yml?,application-prod.yml?
路徑中占位符的表示是:
application 對(duì)應(yīng)我文件中的 applicationprofile 對(duì)應(yīng) dev或prod
label 對(duì)應(yīng)分支 master(默認(rèn)是master 分支)
訪問示例:
默認(rèn)master分支
dev分支:
有興趣的同學(xué)可以都試試,配置沒問題都可以訪問得到。
到此單機(jī)配置服務(wù)中心搭建完成了。?
2. spring cloud配置中心client
使用spring cloud搭建服務(wù)器,工具使用idea,新建maven工程,配置pom.xml文件
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><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></dependency></dependencies>?
創(chuàng)建springboot啟動(dòng)類
@SpringBootApplication public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);} }?
創(chuàng)建測(cè)試controller
@RestController public class TestController {@Value("${mydev}")private String userName;@Value("${profile}")private String profile;@Value("${name}")private String name;@GetMapping("/test")public String home() {return "mydev: " + userName +" profile:" + profile + " name:" + name;}}?
配置文件
application.yml
server:port: 8081name: config-client mydev: ${profile}bootstrap.yml
spring:application:name: application # 指的是application-dev.xml中的applicationcloud:config:uri: http://localhost:8889 # config-server 地址profile: dev # 后綴 指的是application-dev.xml中的devlabel: dev # git 分支?
項(xiàng)目結(jié)構(gòu)
啟動(dòng)成功后訪問
我在Github上的dev分支配置
在上面我通過兩種方式讀取配置屬性:
1、直接在java文件中使用(peofile屬性)
2、是在xml文件中陪之后再在java中使用(mydev屬性)
比較推薦第二種,雖然多了一步,但是可以清除的知道來源,方便找問題。
3. 總結(jié)
到此初步完成簡(jiǎn)單的使用和測(cè)試,復(fù)雜的使用還需要再研究官方文檔:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html?
單一服務(wù)肯定沒辦法保證高可用性,具體方案待續(xù)。。。。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/oskyhg/p/9615729.html
總結(jié)
以上是生活随笔為你收集整理的spring cloud 配置中心的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结合自己造的轮子实践按需加载
- 下一篇: 使用Filezilla Server配置