一: 什麼是SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
翻譯如下:
Spring Cloud爲開發人員提供了快速構建分佈式系統中的一些常見模式的工具(例如配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、領導層選舉、分佈式會話、集羣狀態)。分佈式系統的協調導致了鍋爐板模式,使用Spring Cloud開發人員可以快速建立實現這些模式的服務和應用程序。它們在任何分佈式環境中都能很好地工作,包括開發人員自己的筆記本電腦、裸機數據中心和雲計算等託管平臺。
SpringCloud主要框架:
- 服務發現——Netflix Eureka
- 服務調用——Netflix Feign
- 熔斷器——Netflix Hystrix
- 服務網關——Netflix Zuul
- 分佈式配置——Spring Cloud Config
- 消息總線 —— Spring Cloud Bus
二:Config配置中心
隨着項目的越來越大,我們發現微服務中的配置信息及其繁雜,每一個微服務都有三個配置文件(dev
,test
,pro
),管控起來比較麻煩,分佈於各個微服務中,不便維護。SpringCloud
就爲此引出了SpringCloud Config
,用來統一管理配置信息。
SpringCloud Config
提供了git
,svn
,本地
等方式。本文就主要git方式的配置
及本地方式的配置
,至於svn
配置方式用的不多,後面再補充。。。
三:三種配置方式
-
git
: config 默認Git
加載通過spring.cloud.config.server.git.uri指定配置信息存儲的git地址,比如:https://github.com/xxx/config
-
local
(本地配置
)spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/configspring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:E:\Java\config -
svn
spring.cloud.config.server.svn.uri=http://localhost:8080/store/trunk
spring.cloud.config.server.svn.username=xxx
spring.cloud.config.server.svn.password=xxx
spring.profiles.active=subversion #svn
default-label: trunk #默認SVN分支
四:準備工作
遠程配置文件
:我就放在git中了eureka
:註冊中心config-server
:配置中心服務端config-client
:配置中心客戶端
五:搭建Config
1.1 在碼雲
或者github
上創建個Repository
,我就以github
爲例了,創建一個config-demo
倉庫,創建springcloud-config-dev.yml
配置,配置內容如下:
config: I am a config
2.1 創建eureka
項目,(eureka
的創建就不多講了,不會的同學翻下第一期)引入一下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 在啓動類上加上註解@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3.1 創建config-server
項目,pom如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.take</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</project>
注意要有spring-cloud-config-server
的依賴,至於爲什麼把spring-cloud-starter-netflix-eureka-client
引入,因爲後面要搭建config-server集羣
,註冊到eureka
註冊中心上,實現高可用
。否則一旦config-server
這個微服務宕機
了,會影響其它所有的微服務。
3.2 在啓動類上添加註解@EnableConfigServer
,用來啓用config-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3.3 創建bootstrap.yml
,加入以下配置。
server:
port: 8762
spring:
application:
name: config-server #微服務名稱
cloud:
config:
server:
git:
uri: https://github.com/ml0206/config-demo.git #git遠程倉庫的地址
search-paths: config-demo #git遠程倉庫的名稱,掃描該倉庫下的文件
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #指定註冊中心
3.4 啓動eureka
,再啓動config-server
,訪問:http://localhost:8762/springcloud-config/dev
調用成功,說明config-server
配置成功
當然你也可以直接訪問該配置:http://localhost:8762/springcloud-config-dev.yml
倉庫中的配置文件會被轉換成web
接口,訪問可以參照以下的規則:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
以springcloud-config-dev.yml
爲例,它的application
是springcloud-config
,profile
是dev
,label
是分支
的意思,如果只有一個主分支,可以不寫,默認會訪問master
分支,client
會根據填寫的參數來選擇讀取對應的配置。
4.1 創建config-client
項目,pom
如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.take</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</project>
注意引入spring-cloud-starter-config
依賴
4.2 啓動類不需要修改,創建bootstrap.yml
,加入以下配置,兩種配置均可。
4.2.1 uri
指定配置中心地址
server:
port: 8010
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8762/ #指定配置中心地址,如果配置中心註冊在eureka上了,可以直接通過discovery.service-id獲取配置中心
profile: dev #遠程配置文件的環境 例如:springcloud-config-dev.yml,即dev
name: springcloud-config #遠程配置文件的前綴 例如:springcloud-config-dev.yml,前綴就是springcloud-config
fail-fast: true # 快速失敗,優先保證config-server服務正常
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4.2.2 discovery
server:
port: 8010
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:8762/ #指定配置中心地址,如果配置中心註冊在eureka上了,可以直接通過discovery.service-id獲取配置中心
discovery:
enabled: true # 開啓服務發現
service-id: config-server #配置中心的微服務名稱
profile: dev #遠程配置文件的環境 例如:springcloud-config-dev.yml,即dev
name: springcloud-config #遠程配置文件的前綴 例如:springcloud-config-dev.yml,前綴就是springcloud-config
fail-fast: true # 快速失敗,優先保證config-server服務正常
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
如果config-server
服務註冊在eureka
中,可以用discovery
方式
4.3 創建controller
包,包下創建configController
package com.take.configclient.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
public class ConfigController {
@Value("${config}")
private String config;
@RequestMapping("config")
public String config(){
return this.config;
}
}
4.4 啓動config-client
,訪問:http://localhost:8010/config
5.1 如果這時候把git
上springcloud-config-dev.yml
中的配置I am a config
修改成I am a config 222
,獲取的還是I am a config
就不能獲取最新配置了,因爲config-client
只在啓動時獲取一次。springcloud已經給我們提供瞭解決方案,每個客戶端通過POST方法觸發各自的/refresh
。
修改config-client
項目已到達可以refresh
的功能。
5.2 在config-client
中添加新的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加了spring-boot-starter-actuator
包,spring-boot-starter-actuator
是一套監控的功能,可以監控程序在運行時狀態,其中就包括/refresh
的功能。
5.3 開啓更新機制
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 // 使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。
public class ConfigController {
@Value("${config}")
private String config;
@RequestMapping("config")
public String config(){
return this.config;
}
}
5.4 修改bootstrap.yml,加入以下配置
server:
port: 8010
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:8762/ #指定配置中心地址,如果配置中心註冊在eureka上了,可以直接通過discovery.service-id獲取配置中心
discovery:
enabled: true # 開啓服務發現
service-id: config-server #配置中心的微服務名稱
fail-fast: true
profile: dev #遠程配置文件的環境 例如:springcloud-config-dev.yml,即dev
name: springcloud-config #遠程配置文件的前綴 例如:springcloud-config-dev.yml,前綴就是springcloud-config
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*"
management.security.enabled
: springboot 1.5.X 以上默認開通了安全認證,所以需要添加這個配置
management.endpoints.web.exposure.include
: springboot 2.x 默認只開啓了info、health的訪問,*代表開啓所有訪問
5.5 再次修改springcloud-config-dev.yml爲I am a config 333
,通過cmd
命令行執行:curl -X POST http://localhost:8010/actuator/refresh
,可以看到命令行上有顯示:["config.client.version","config"]
意味着config
這個配置已經刷新,這時,我們再去刷新一下頁面,可以看到,頁面上得到的信息已經變爲了:I am a config 333
,這時我們refresh
成功。
6.1 本地配置方式
修改config-server
中的bootstrap.yml
server:
port: 8762
spring:
profiles:
active: native #本地配置(默認是git,可以設置:subversion(SVN),native(本地))
application:
name: config-server
cloud:
config:
server:
native:
search-locations: classpath:/share
bootstrap: true
eureka:
client:
service-url:
defaultzone: http://localhost:8761/eureka
profiles.active=native
本地讀取配置
search-locations: classpath:/share
掃描share
文件夾,會從這個文件夾中讀取配置
6.2 在resources
中創建share
文件夾,並創建config-client-dev.yml
,內容如下:
word: Hello World
6.3 修改config-client
的bootstrap.yml
如下:
server:
port: 8010
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:8762/ #指定配置中心地址,如果配置中心註冊在eureka上了,可以直接通過discovery.service-id獲取配置中心
discovery:
enabled: true # 開啓服務發現
service-id: config-server #配置中心的微服務名稱
fail-fast: true
profile: dev #遠程配置文件的環境 例如:springcloud-config-dev.yml,即dev
name: config-client #遠程配置文件的前綴 例如:springcloud-config-dev.yml,前綴就是springcloud-config
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*"
只需要修改profile
和name
,此處遠程配置文件爲config-client-dev.yml
,name
就是confit-client
,profile
就是dev
6.4 分別啓動config-server
,config-client
。訪問:http://localhost:8010/config
說明config-client
已經獲取到config-server
中的config-client-dev.yml
配置信息了
7.1 svn
配置方式,需要在config-server
加入svn
依賴,這種方式用的不多,有興趣的朋友,可以配置,只需要改config-server
中的yml
配置,配置信息已經在上方列出來了
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
六:集羣
配置完成後,基本上沒有多大問題了。但是對於大型的微服務項目來說,有幾十個甚至幾百個微服務,那麼每個微服務都會依賴配置中心config-server
,一旦config-server宕機
,將會影響所有的微服務
,甚是可怕。那我們就要搭建配置中心集羣、多節點
。搭建起來也比較簡單,直接把config-server
項目,複製一份,改個端口
,微服務名稱不要改(spring.application.name
)。重新依次啓動eureka
,config-server
,config-server(複製版)
,config-client
,當config-server微服務掛掉
,我們還有config-server(複製版)
,從而實現高可用
。
七:項目實例
參考鏈接:
https://www.cnblogs.com/zuowj/p/10432445.html#s-1.3
https://www.cnblogs.com/babycomeon/p/11134717.html
https://www.cnblogs.com/babycomeon/p/11135926.html
開心一刻
今天我那個90後的表弟問我:“大哥,我遇到一個很大的難題,你比我年長十幾歲,以你過來人的經驗幫我解答解答唄。”
我大手一揮:“沒問題!”
表弟想了想:“我被我媽逼婚了,可我不想這麼早結婚,你是如何做到這麼大了還是一個人的呢?”
我呡了一口茶,然後猛地啐他一臉,憤怒地說:“你給我滾!”
如果覺得不錯,幫忙點個贊,您的點贊將是我的動力!