基於 Consul 的分佈式配置中心(Spring Cloud Config)實例
文章目錄
開發環境及工具
- JDK 1.8
- Maven 3
- SpringBoot 2.0
- SpringCloud Finchley.M7
- Consul
- Eclipse
準備:創建 Maven 父項目 negen-demo-parent
- 配置 pom.xml 引入相關依賴
<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.negen</groupId>
<artifactId>negen-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- SpringBoot依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<!-- 健康檢查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- SpringBoot整合fegnin客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- lombok 依賴 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- hystrix斷路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- eureka/consul二選一且配置文件不同 -->
<!-- SpringBoot整合eureka客戶端 -->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> -->
<!-- consul依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這裏必須要添加, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<modules>
<module>config-server</module>
<module>config-client</module>
</modules>
</project>
- 啓動 Consul ,windows中啓動命令如下:
consul agent -dev
一、創建配置服務中心 config-server
1、創建一個空的 Maven module
2、引入相關的 pom 依賴
<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>
<parent>
<groupId>com.negen</groupId>
<artifactId>negen-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>config-server</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- cloud-config-server依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
3、創建啓動文件 AppConfigServer.java
package com.negen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class AppConfigServer {
public static void main(String[] args) {
SpringApplication.run(AppConfigServer.class, args);
}
}
4、創建啓動配置文件 application.yml
- git.uri:用於存儲配置文件的倉庫地址
- 重點配置:git.uri
spring:
application:
name: config-server
cloud:
consul:
host: 127.0.0.1 #consul地址
port: 8500 #consul端口號
config:
server:
git: #配置文件 git 地址
uri: https://github.com/BBBBigFlyPig/SpringConfigServer-Configs.git
5、git倉庫中創建 cloud-config-dev.yml 文件並添加以下內容
age: 300
name: test
server:
port: 8888
6、啓動 AppConfigServer.java 並在瀏覽器中訪問 http://localhost:8080/cloud-config-dev.yml
效果圖如下:
至此,server端創建完成
一、創建客戶端 config-client
1、創建一個空的 Maven module
2、引入相關的 pom 依賴
<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>
<parent>
<groupId>com.negen</groupId>
<artifactId>negen-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>config-client</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- cloud-config 客戶端依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
3、resource 目錄下創建 bootstrap.yml 文件
server:
port: 8000
spring:
application:
name: cloud-config
cloud:
consul:
host: 127.0.0.1
port: 8500
config:
profile: dev #選擇獲取的配置文件是dev還是 test之內的;git倉庫配置文件命名格式 ${applicatio.name}-dev/test.yml
discovery:
enabled: true
service-id: config-server #config服務器的 ${applicatio.name}
management:
endpoints:
web:
exposure:
include: refresh,health #暴露手動刷新接口
4、創建啓動文件 AppConfigClient.java
package com.negen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class AppConfigClient {
public static void main(String[] args) {
SpringApplication.run(AppConfigClient.class, args);
}
}
5、創建測試類 TestConfigServerController.java
package com.negen.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //在需要刷新配置的地方使用
public class TestConfigServerController {
@Value("${age}")
String age;
@RequestMapping(value = "/getAge")
public String getAge() {
return age;
}
}
6、啓動應用並用瀏覽器訪問 http://localhost:8888/getAge
應用會優先加載從配置中心拉取到的配置,所以訪問的端口就是8888;
7、手動刷新配置信息
用 Post 方式訪問 http://localhost:8888/actuator/refresh 即可獲取最新的配置信息;