十一、SpringCloud之統一配置中心
一、簡介
1、為什麼需要統一配置中心
- 不方便維護(多人修改配置檔案,容易出現問題)
- 配置內容安全與許可權(隔離配置檔案,不對開發公開)
- 更新配置專案需重啟(更新配置檔案重啟專案太麻煩了)
2、統一配置中心總體架構
將配置放在遠端git上,這樣版本控制起來會比較方便,config-server會從遠端的git上把配置拉取下來,然後放在本地的git裡(雙向流動,假如遠端的git訪問不了,config-server會從本地的git上把配置拉取下來),微服務product和order整合config-client元件,就能夠拿到配置。
二、使用統一配置中心
1、config-server
- 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.imooc</groupId> <artifactId>config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.M1</spring-cloud.version> </properties> <!-- 引入依賴 --> <dependencies> <!-- 引入config-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 引入eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 引入eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 版本依賴管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 打包 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 倉庫地址--> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
- application.yml
spring: application: name: config #配置服務名稱 #配置config-server cloud: config: server: git: uri: https://gitee.com/Amyzwj/config-repo #地址 username: XXX # 使用者名稱 password: XXX # 密碼 #配置檔案拉取地址 basedir: /Users/Amy/Documents/ideaproject/config/basedir #配置Eureka eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ #配置端扣號 server: port: 8808
- ConfigApplication
package com.imooc.config;
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 ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
-
設定遠端git
在碼雲上註冊一個賬號:https://gitee.com
新建一個專案
新建幾個配置檔案
- 測試
用.properties,.json,.yml結尾,就轉換成相應格式的配置
http://localhost:8808/order-b.properties
http://localhost:8808/order-b.json
http://localhost:8808/order-b.yml
2、命名規則
- /{name}-{profiles}.yml
- /{label}/{name}-{profiles}.yml
- name 服務名 、 profiles環境(env) 、 label 分支(branch)
http://localhost:8808/test/order-dev.yml
http://localhost:8808/order-dev.yml
3、config-client
- pom.xml
<!-- 引入cloud-config-client 統一配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
- bootstrap.yml
spring:
application:
name: order #配置服務名稱
cloud:
config:
discovery:
enabled: true
service-id: CONFIG #configServer 服務的名字
profile: test
- controller
package com.imooc.order.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/env")
//@RefreshScope
public class EnvController {
@Value("${env}")
private String env;
@GetMapping("/print")
public String print() {
return env;
}
}
-
測試
http://localhost:8803/env/print
4、統一配置中心高可用
config啟動三個例項,埠號分別是8807、8808、8809
重新啟動config-client這個專案,config-client會根據負載均衡規則呼叫config服務獲取配置。
5、更換另一個Eureka 例項
之前用的Eureka埠號是8761,現在要把跟換成用埠號為8762的Eureka
- 1.啟動埠號為8762的Eureka
- 2.config裡配置的Eureka埠號換成8762
spring:
application:
name: config #配置服務名稱
#配置config-server
cloud:
config:
server:
git:
uri: https://gitee.com/Amyzwj/config-repo #地址
username: XXX # 使用者名稱
password: XXX # 密碼
#配置檔案拉取地址
basedir: /Users/Amy/Documents/ideaproject/config/basedir
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/
#配置端扣號
server:
port: 8808
- 3.將遠端git裡面的配置檔案Eureka的配置刪除,在config-client專案裡面加上Eureka的配置,埠號為8762
spring:
application:
name: order #配置服務名稱
cloud:
config:
discovery:
enabled: true
service-id: CONFIG #configServer 服務的名字
profile: dev
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/
- 注意:
order.yml裡的配置是公共的配置,如果訪問order-dev.yml裡面的配置,它會把order.yml和order-dev.yml裡的配置綜合在一起所以把order-dev.yml裡Eureka的配置刪除,訪問這個配置檔案還是有Eureka的配置,那是因為綜合了order.yml裡面的配置,把order.yml裡面的Eureka的配置刪除,再訪問就沒有了。以後公共的配置都可以配置在order.yml這個配置檔案裡面
- 測試