spring cloud 配置中心
1. spring cloud配置中心server
1.1 創建git倉庫
首先在github上搭建一個存儲配置中心的倉庫,需要創建兩個分支,一個是master,一個是dev分支。自己學習可以用公開庫,真實環境使用的話,還是需要私庫,或者自己搭建git服務器。
?
?
1.2 搭建server
使用spring cloud搭建服務器,工具使用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>創建springboot啟動類,啟動類要加上@SpringBootApplication 和?@EnableConfigServer 這兩個注解。
@SpringBootApplication: springboot啟動注解
@EnableConfigServer: springcloud config server的注解,必須要加上
@SpringBootApplication@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
創建配置文件bootstrap.yml
spring:cloud:config:server:git:uri: https://github.com/hanggle/ConfigCenter.git #git倉庫地址,就是剛才創建的git倉庫skipSslValidation: true #跳過校驗basedir: d:///myspace///config-center///config #從git倉庫拉取到的文件在本地存儲的位置,可自行修改或刪掉,默認存儲在C盤# bootstrap: true server:port: 8889config-server的目錄結構:
啟動springboot,啟動成功后可以在瀏覽器查看拉取到的配置信息,路徑的訪問有以下幾種:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties我創建的文件名是:application-dev.yml?,application-prod.yml?
路徑中占位符的表示是:
application 對應我文件中的 applicationprofile 對應 dev或prod
label 對應分支 master(默認是master 分支)
訪問示例:
默認master分支
dev分支:
有興趣的同學可以都試試,配置沒問題都可以訪問得到。
到此單機配置服務中心搭建完成了。?
2. spring cloud配置中心client
使用spring cloud搭建服務器,工具使用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>?
創建springboot啟動類
@SpringBootApplication public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);} }?
創建測試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 分支?
項目結構
啟動成功后訪問
我在Github上的dev分支配置
在上面我通過兩種方式讀取配置屬性:
1、直接在java文件中使用(peofile屬性)
2、是在xml文件中陪之后再在java中使用(mydev屬性)
比較推薦第二種,雖然多了一步,但是可以清除的知道來源,方便找問題。
3. 總結
到此初步完成簡單的使用和測試,復雜的使用還需要再研究官方文檔:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html?
單一服務肯定沒辦法保證高可用性,具體方案待續。。。。。。
?
轉載于:https://www.cnblogs.com/oskyhg/p/9615729.html
總結
以上是生活随笔為你收集整理的spring cloud 配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结合自己造的轮子实践按需加载
- 下一篇: 使用Filezilla Server配置