1. 程式人生 > >cloud配置中心遇到的坑

cloud配置中心遇到的坑

https://blog.csdn.net/z960339491/article/details/80593982
分散式配置中心
為什麼要有用分散式配置中心這玩意兒?現在這微服務大軍已經覆蓋了各種大小型企業,每個服務的粒度相對較小,因此係統中會出現大量的服務,每個服務都要有自己都一些配置資訊,或者相同的配置資訊,可能不同環境每個服務也有單獨的一套配置,這種情況配置檔案數量比較龐大,維護起來相當費勁,舉個栗子:
在開發的過程中,一般資料庫是開發環境資料庫,所有服務DB的IP配置為:92.168.0.1,突然老大說,開發環境換了,DB的IP要修改,這下可不好受了,所有模組挨個修改DB的配置,就問你難受不難受?
這個時候分散式配置中心就發揮了很大的優勢,只需要修改配置中心配置,所有服務即可自動生效,爽不爽!

Spring Cloud Config
官網地址:http://cloud.spring.io/spring-cloud-config/

簡介
Spring Cloud Config為服務端和客戶端提供了分散式系統的外部化配置支援。配置伺服器為各應用的所有環境提供了一箇中心化的外部配置。它實現了對服務端和客戶端對Spring Environment和PropertySource抽象的對映,所以它除了適用於Spring構建的應用程式,也可以在任何其他語言執行的應用程式中使用。作為一個應用可以通過部署管道來進行測試或者投入生產,我們可以分別為這些環境建立配置,並且在需要遷移環境的時候獲取對應環境的配置來執行。

置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。當然他也提供本地化檔案系統的儲存方式。

使用 spring Cloud 進行集中式配置管理,將以往的配置檔案從專案中摘除後放到Git 或svn中集中管理,並在需要變更的時候,可以通知到各應用程式,應用程式重新整理配置不需要重啟。

實現原理
其實這個實現原理相對比較簡單一些,基於git的互動操作。

我們把配置檔案存放到git上面
Spring Cloud Config配置中心服務連線git
客戶端需要配置配置資訊從配置中心服務獲取
當客戶端啟動,會從配置中心獲取git上面的配置資訊
配置中心服務端
pom.xml新增依賴

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


Application啟動類添加註解

新增@EnableConfigServer註解,啟用配置中心:

package com.qianxunclub;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


/**
* @author chihiro.zhang
*/
@SpringBootApplication
@EnableConfigServer
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
1
2
3
配置檔案

在application.yml或者application.properties新增配置資訊:

spring:
cloud:
config:
server:
git:
uri: https://gitee.com/qianxunclub/spring-boot-config-repo
default-label: master
search-paths: /**
basedir: target/config

spring.cloud.config.server.git.uri:配置git倉庫地址
spring.cloud.config.server.git.search-paths:倉庫資料夾目錄,如果是/**,就是所有目錄所有檔案
spring.cloud.config.server.git.default-label:配置倉庫的分支
spring.cloud.config.server.git.basedir:配置檔案拉去到本地的目錄位置
啟動測試

首先在git裡面新增一個application-dev.yml配置檔案,內容如此下:

test: 我是配置中心配置資訊
1
已經配置完成了,啟動一波試試,看效果咋樣,正常情況下是可以正常啟動的,然後獲取配置檔案試試
訪問地址:http://localhost:8888/test/dev
如果返回如下,就是成功了:

{
"name":"test",
"profiles":[
"dev"
],
"label":null,
"version":"64e7882a8f280641724e454a2db5a3da7b44d3d4",
"state":null,
"propertySources":[
{
"name":"https://gitee.com/qianxunclub/spring-boot-config-repo/application-dev.yml",
"source":{
"test":"配置中心的配置資訊"
}
}
]
}


http請求地址和資原始檔對映如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
配置中心客戶端使用
pom.xml新增依賴

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


配置檔案

建立bootstrap.yml檔案,切記,是bootstrap.yml檔案bootstrap.yml檔案,我就因為寫到了application.yml這個裡面,各種出現問題啊,新增如下配置:

spring:
cloud:
config:
name: application
profile: dev
label: master
uri: http://localhost:8888/

 


spring.cloud.config.label:指明遠端倉庫的分支
spring.cloud.config.profile:指定不同環境配置檔案,和git倉庫的 application-dev.yml對應
spring.cloud.config.name:配置名稱,一般和git倉庫的application-dev.yml對應
spring.cloud.config.uri:上面的配置中心服務地址
啟動測試

先新增一個獲取配置資訊的類:

 

/**
* @author chihiro.zhang
*/
@Configuration
@EnableAutoConfiguration
public class DemoConfiguration {

@Value("${test}")
private String test;
}

 

 


找個地方隨便呼叫一下,輸出這個test,就會列印上面git裡面配置的資訊了,爽不!

說說中間遇到的坑
服務端git配置死活獲取不了git倉庫配置檔案
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/qianxunclub/spring-boot-config-repo
default-label: master
search-paths: /**
basedir: target/config

 


當時這個uri配置的是公司的git倉庫,公司的git倉庫訪問是需要開代理才能有許可權訪問的,代理也開了,可是一直報錯:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jun 06 11:10:56 CST 2018
There was an unexpected error (type=Not Found, status=404).
Cannot clone or checkout repository: http://xxx.com:5080/framework/config-repo

 


很鬱悶,不知道為啥,可是就在剛剛,就剛剛,寫部落格的時候,有測試了一下,通了。。。。日了狗了,不知道啥原因,等研究出來了再來補充。

客戶端配置一定要配置在bootstrap.yml裡面
uri預設會呼叫埠為8888的地址http://localhost:8888/
啟動的時候,會載入label和uri,profile配置,profile可以在啟動引數新增,profile也可以加在application.yml新增
name也可以加在application.yml新增
demo
配置中心服務端:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置git倉庫:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置客戶端使用:https://gitee.com/qianxunclub/qianxunclub-starter-demo
客戶端主要配置在:https://gitee.com/qianxunclub/qianxunclub-starter-parent/tree/master/qianxunclub-starter-config