由于網上資料大多數基于SpringBoot2.0以下版本搭建配置中心,而2.0版本以上的資料較小,是以基于SpringCloud2.0.2.RELEASE搭建了一個手動重新整理的配置中心,在(二)全過程詳解(自動重新整理)WebHooks中将會講解,該如何實作自動重新整理配置。
話不多說,接下來就是如何實作。
一.建立Config-Respoity項目
我是在碼雲上建立的配置庫,在github上也可以,任君選擇,畢竟現在github也可以免費建立私有倉庫了。
demo版本隻需要建立一個倉庫包含幾個配置檔案即可,也可以建立多個子目錄進行存放。
config-client-dev.yml檔案内容: profile: dev
config-client檔案内容 :profile: default
二.建立Config-Server項目
1.所需依賴pom.xml檔案:
<?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.xxx</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.其次需要在ConfigServerApplication類中添加注解
@EnableConfigServer //表示為ConfigServer端
@EnableDiscoveryClient //表示為Eureka用戶端,Eurek服務端的搭建就不在較長的描述了,不會的可以google下如何搭建
3.application.yml檔案的配置:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/xxxx/spring-config-resp #spring-config-resp倉庫
username: xxxx
password: xxxx
server:
port: 1234
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
4.運作
首先需要打開eureka server服務端,否則會報錯,若沒有的話可以自行删除@EnableDiscoveryClient及yml檔案中的eureka配置。
在浏覽器中或者postman工具中打開請求位址,請求位址格式可以為以下幾種:
http://localhost:1234/config-client.yml
http://localhost:1234/config-client-dev.yml
http://localhost:1234/config-client/dev
可以成功的看到config-server成功請求到配置檔案。
三.搭建Config-Client項目
1.所需依賴pom.xml檔案:
<?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.xxx</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--bootstrap.yml檔案生效依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--監控子產品-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.bootstrap.yml配置檔案:config需要配置bootstrap.yml檔案,否則無法正常加載
spring:
application:
name: config-client
cloud:
config:
profile: dev
label: master
uri: http://localhost:1234/ #config-server的位址
discovery:
enabled: true
service-id: config-server
server:
port: 1235
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*" #通路監控設定的端口
這裡有幾點需要特别注意:
1.springcloud2.0以上actuator已經不是全部對外開放了,需要配置,配置*表示所有的都可以通過
2.springcloud2.0以上的手動重新整理位址變了 字尾為/actuator/refresh
3.ConfigClientApplication類中添加注解:@EnableDiscoveryClient
4.Controller層: TestController
@RestController
@RefreshScope //重新整理
public class TestController {
@Value("${profile}")
private String from;//profile需要在config倉庫的配置檔案中有這個字段,不然會報錯
@RequestMapping("/profile")
public String from(){
return this.from;
}
}
5.啟動項目。
(1)通路:http://localhost:1235/profile 檢視配置
(2)進行手動重新整理
首先在碼雲上修改配置檔案,config-client-dev.yml檔案profile: 666
使用postmanf發起post請求,注意是post方式
請求位址:http://localhost:1235/actuator/refresh
再次打開http://localhost:1235/profile ,可以看到配置已經重新整理了。
6.總結
在項目中僅僅是手動重新整理是無法達到要求的,需要我們配置自動重新整理,在接下來的(二)全過程詳解(自動重新整理)WebHooks 将會介紹如何最簡單的搭建一個基于webhooks的自動重新整理的配置中心。