1. 程式人生 > >Spring Cloud Config(二):基於Git搭建配置中心

Spring Cloud Config(二):基於Git搭建配置中心

1、簡述

本文選用Git作為配置倉庫,新建兩個環境的配置資料夾,dev 和 test,資料夾中分別存放 Config Client 端的配置檔案,目錄結構如下:

├ ─ ─ dev

└ ─ ─ config-client-dev.properties

├ ─ ─ test

└ ─ ─ config-client-test.properties

2、Config Server 搭建

2.1、Maven依賴

Config Server 是一個基於Spring Boot的web應用,我們首先需要做的就是在pom中引入Spring Cloud Config Server的依賴。

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

2.2、Config Server配置

# 配置中心名稱
spring.application.name=lkf-config-git
# 配置中心埠號
server.port=1000

# 配置git倉庫地址
spring.cloud.config.server.git.uri=https://github.com/liukaifeng/lkf-cloud
# 配置檔案查詢路徑
spring.cloud.config.server.git.search-paths=config-repo/dev
# 配置中心api字首
spring.cloud.config.server.prefix=lkf

2.3、啟用Config Server

最後就是啟用Config Server,只需要加上@EnableConfigServer即可

@SpringBootApplication
@EnableConfigServer
public class GitConfigApplication {

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

配置後完成後啟動應用,Config Server就開始工作了,訪問地址:

http://localhost:1000/lkf/config-client/dev 看到了剛剛配置檔案中的配置資訊,到此配置中心服務端搭建完成。

image

3、Config Client 使用

3.1、Maven依賴

Config Client 是一個基於Spring Boot的web應用,我們首先需要做的就是在pom中引入Spring Cloud Config Client的依賴。

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

3.2、Config Client配置

我們需要做一些配置使Config Client知道Config Server的地址,以及應用自身配置的資訊,如:應用名字,環境,配置的版本等資訊,以下是配置:

#配置環境
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:1000/lkf
#配置檔名稱
spring.cloud.config.name=config-client

配置完成後,啟動應用,Config Client就會自動從Config Server獲取配置,並註冊到註冊中心,訪問註冊中心地址:http://localhost:8888/

image
至此,Config Client 已成功從配置中心拉取並解析成功配置並註冊到了註冊中心。另外在應用啟動日誌中也可以看到拉取配置資訊的日誌:

Fetching config from server at : http://localhost:1000/lkf
2018-11-03 22:48:47.951 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:1000/lkf/config-client/dev"
2018-11-03 22:48:48.015 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
2018-11-03 22:48:49.130 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:1000/lkf/config-client/dev" resulted in 200 (null)
2018-11-03 22:48:49.132 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.springframework.cloud.config.environment.Environment] as "application/json;charset=UTF-8" using [org.springfr[email protected]273842a6]
2018-11-03 22:48:49.150 [main] INFO  o.s.c.c.client.ConfigServicePropertySourceLocator - Located environment: name=config-client, profiles=[dev], label=null, version=0eaed01201a6f2c5fd2b0fd3b637d8384f3c79b9, state=null
2018-11-03 22:48:49.150 [main] DEBUG o.s.c.c.client.ConfigServicePropertySourceLocator - Environment config-client has 1 property sources with 13 properties.
2018-11-03 22:48:49.150 [main] INFO  o.s.c.b.c.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource {name='configService', propertySources=[[email protected] {name='configClient', properties={config.client.version=0eaed01201a6f2c5fd2b0fd3b637d8384f3c79b9}}, [email protected] {name='https://github.com/liukaifeng/lkf-cloud/config-repo/dev/config-client-dev.properties', properties={server.port=8001, spring.application.name=lkf-eureka-client, eureka.client.serviceUrl.defaultZone=${EUREKA_SERVICE_URL:http://localhost:8888}/lb/eureka/, management.endpoint.conditions.enabled=true, eureka.instance.prefer-ip-address=true, eureka.instance.instanceId=${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}, management.endpoints.web.exposure.include=*, management.endpoint.health.show-details=always, management.endpoint.logfile.enabled=true, management.endpoint.auditevents.enabled=true, management.endpoint.loggers.enabled=true, [email protected]@, [email protected]@}}]}
2018-11-03 22:48:49.150 [main] DEBUG o.s.core.env.PropertySourcesPropertyResolver - Could not find key 'logging.config:' in any property source