最近看《深入了解Spring Cloud微服務建構》是一書之中,發現作者使用的springboot是1.x.版本之中,書中的代碼示例在2.x的版本之中,有一點差別,在此讀者總結一下,實作2.0之後,用spring cloud bus 自動重新整理配置
首先說一下筆者用到的版本springBoot 2.1.4 ,springcloud 版本 Greenwich ,首先看下筆者的demo目錄結構
eureka 為注冊中心 目的是為了搭建一個高可用的配置中心config ,config-client和config-server 分别向eureka裡面去注冊服務,config-client通過向eureka 讀取到config-server的配置檔案,而config-server配置檔案實際内容存入了gitee倉庫。
注意一下 pom.xml依賴僅僅是貼出來了相關重要依賴, web依賴請自行加入
這裡 總線采用是 rabbitMQ做為總線代理,需要首先開啟rabbitMQ 如果不會安裝和配置rabbitMQ
請拜訪官網 https://www.rabbitmq.com
1 eureka - server
eureka-server 在pom.xml檔案 隻需要引入 netflix-eureka-server的相關依賴,springBoot 以上的2.x依賴和spring1.x 一點點差別,主要加入了netflix (一個公司,springcloud 很好的結合的該公司的旗下用的很多元件)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在該項目的resource下面建立application.yml檔案
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#關閉表示 eureka不向自己注冊自己,也不注冊到别的eureka裡面,在eureka叢集下面要設定true 預設是true
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在啟動類上面加上 @EnableEurekaServer 注解 開啟eurekaServer服務
2 config-server
該子產品是配置中心服務端 ,同樣在pom.xml引入相關依賴,版本号缺不預設都可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
同樣resource下面建立application.yml檔案,添加相關配置 ,這裡預設把config-client的配置檔案存入了gitee倉庫裡面,
search-paths 表示倉庫下面存放的搜尋的檔案夾,配置檔案放入檔案夾裡面
server:
port: 8769
spring:
application:
name: config-server
cloud:
config:
#config-server相關配置
server:
git:
uri: https://gitee.com/NaNiZhenShiYiJiBang/cloud-config.git
search-paths: respo
username: [email protected]
password:
default-label: master
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
gitee倉庫:
在啟動類上面加上 @EnableConfigServer 注解 開啟config服務 ,在加上 @EnableEurekaClient 開啟eureka用戶端服務
3 config-client
該子產品是config用戶端子產品,它需要向config-server子產品裡面去讀取相關配置
同理添加依賴
<!-- 注冊到eureka server裡面建構高可用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- 消息總線 spring cloud bus 可選的消息代理總線 RabbitMQ,AMQP,Kafka 可以重新整理配置
如果有幾十個微服務,而每 個服務又是多執行個體,當更改配置時,需要重新啟動多個微服
務執行個體,會非常麻煩。 Spring Cloud Bus 個功能就是讓這個過程變得簡單,當遠端 Git
庫的配置更改後,隻需要向某 個微服務執行個體發送 Post 請求,通過消息元件通知其他微
服務執行個體重新拉取配置檔案。 當遠端 Git 倉庫的配置更改後,通過發送
“ /bus-refresh ” Post 請求給某 個微服務執行個體,通過消息元件,通知其他微服務執行個體,更新配置
檔案。
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- 2.0需要加上這個監控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在該子產品的resource裡面建立 bootstrap.yml (bootstrap.yml比application)加載優先級更高
config-client啟動之時,會讀取resource配置檔案,在配置之中 讀取config discovery 發現位址 service-id 對應eureka裡面的 服務注冊名,則client是在eureka裡面去讀取config-sever注冊的服務 ,而且該子產品的name 是config-client ,profiles 是dev 則配置檔案的名詞是 config-client-dev.yml
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
profiles:
active: dev
#通過消息總線更改配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#開啟監控中心 springboot2.0 必要操作 * 表示監控所有資訊
management:
endpoints:
web:
exposure:
include: "*"
同理在啟動類加上如下注解 ,懶得打字了 ,見下圖,下面這個請求接口僅僅為了測試demo使用,項目中千萬别這麼寫
4 啟動測試
先依次開啟rabbitMQ eureka-server config-server config-client 最後啟動config-client之後會發現雖然配置檔案設定的8080端口,但是讀取到的git 配置檔案裡面的位址,端口是 8762
見圖
此時,在浏覽器上面通路 localhost:8762/foo
說明已經加載了git倉庫下面的配置檔案,現在來測試spring cloud Bus 實作不用重新開機來重新整理修改一下git配置檔案裡面的foo 的值
這裡書中由于版本 問題,在2.X并不适用 2.x所有的都是在監控裡面
http://localhost:8762/actuator/bus-refresh post請求
會發現在cosole裡面輸入一些資訊 此時再次重新整理
好了,這就實作了 項目示例代碼 https://github.com/xzjayx/spring-cloud-config