Spring Cloud Config 和Spring Cloud Bus實現配置中心

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

Spring Cloud是很多組件的集合,Spring將常用的技術框架進行包裝和整合,如mybatis zookeeper rabbitmq redis等等,還有一些科技公司貢獻出來的一些經過生產環境驗證的組件如奈飛公司貢獻出的eureke(服務發現) Hystrix(監控與隔離)? Feign(聲明式服務調用)? Ribbon(負載均衡) Zuul(網關) 等等,詳情移步官網?SpringCloud

Spring Cloud是目前比較流行的微服務開發框架,可以與容器技術如docker一起使用,提高生產力。但是組件過多也有一定的學習曲線,而且適合大公司的架構不見得適合我們的業務,要根據實際情況靈活運用。

Spring Cloud Config是基于git/svn倉庫來管理配置,然后有一個ConfigServer來負責拉取配置,ConfigClient是使用配置的應用,比如現在有很多微服務應用,如訂單管理,用戶管理,地址管理,庫存管理等等,在這些微服務的pom中增加ConfigClient就可以自動從ConfigServer拉取配置。我們還希望配置更新之后可以實時獲取,這時候需要用到spring cloud中的bus組件,bus其實就是基于amqp的一個消息總線,spring對rabbitmq和kafka支持的比較好。常見的場景是:

1、更改配置

2、push到遠程倉庫

3、push動作觸發一個web hook

4、這個web hook會使用post訪問ConfigServer的一個接口

5、ConfigServer通過rabbitmq發送一個廣播

6、client收到消息,自動拉取并刷新配置

這些不需要任何編碼工作,只需要配置就可以。下面是一個簡單的例子。

1、rabbitmq安裝參照官網,十分簡單,也可以參考俺的博客

2、啟動一個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>spring_cloud</groupId><artifactId>config_demo</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config</artifactId><version>2.0.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-bus</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId><version>2.0.1.RELEASE</version></dependency></dependencies><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot</url><snapshots><enabled>true</enabled></snapshots></repository></repositories>
</project>

入口類:

@EnableConfigServer@SpringBootApplicationpublic?class?Application {public?static?void?main(String[] args) {SpringApplication.run(Application.class, args);}}

配置:application.properties

server.port=8888
#配置倉庫地址
spring.cloud.config.server.git.uri=https://github.com/mychemicalromance/spring_cloud_config
## 配置文件夾
spring.cloud.config.server.git.searchPaths=configs
## 分支
spring.cloud.config.label=master
## 以下是bus相關配置 替換成自己的就行,比如host port name password
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=192.168.137.5
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=rabbit
management.endpoints.web.exposure.include=*

解釋:Spring Cloud只需要上述入口類就啟動了一個web工程,使用的是嵌入的tomcat/jetty。默認讀取application.[properties|yml|json] 使用喜歡的格式就行。

3、Config Client

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>spring_cloud</groupId><artifactId>config_client</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config</artifactId><version>2.0.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-bus</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId><version>2.0.1.RELEASE</version></dependency></dependencies><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot</url><snapshots><enabled>true</enabled></snapshots></repository></repositories>
</project>

入口類:

@Configuration
@EnableAutoConfiguration
@RestController
@RefreshScope
public class Application {@Value("${a}")String name;@RequestMapping("/")public String home() {return "Hello " + name;}public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

配置:application.properties

server.port=8002
## bus相關配置,替換成自己的就行,比如host port name password
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=192.168.137.5
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=rabbit
## 讀取app1的配置
spring.application.name=app1
## profile是dev,可以有測試test,沙箱mirror,線上online等等
spring.cloud.config.profile=dev
management.endpoints.web.exposure.include=*


3、啟動Config Server和Config Client,首先訪問localhost:8888/app1/dev,可以看到所有配置,然后訪問localhost:8002/可以看到獲取了配置。然后本地更改配置文件并push。由于在github上配置web hook來調用本地是不現實的,所以我們用curl來模擬post訪問Config Server,如果是在公司的git上,并且在公司局域網內可以訪問Config Server,可以配置web hook來測試。

curl -X POST "http://localhost:8888/actuator/bus-refresh"

然后刷新Config Client,就能看到最新的配置了。

轉載于:https://my.oschina.net/wuxiaofei/blog/2088395

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/253466.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/253466.shtml
英文地址,請注明出處:http://en.pswp.cn/news/253466.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ARM Linux啟動過程分析

1. 引 言 Linux 最初是由瑞典赫爾辛基大學的學生 Linus Torvalds在1991 年開發出來的&#xff0c;之后在 GNU的支持下&#xff0c;Linux 獲得了巨大的發展。雖然 Linux 在桌面 PC 機上的普及程度遠不及微軟的 Windows 操作系統&#xff0c;但它的發展速度之快、用戶數量的日益…

你有沒有靠譜的基因?一個人靠不靠譜,其實就看這三點:“凡事有交代,件件有著落,事事有回音。”...

你有沒有靠譜的基因&#xff1f;一個人靠不靠譜&#xff0c;其實就看這三點&#xff1a;“凡事有交代&#xff0c;件件有著落&#xff0c;事事有回音。” 故事一、做了就忘了&#xff1f; 一天上班后&#xff0c;我讓小王給上級部門送一個材料。 一個小時過去了&#xff0c;沒…

CMOS圖像傳感器——閃爍(flicker)現象

一、概述 閃爍(Flicker),通常發生在室內場景,曝光時間設置如果不是光源能量周期的整數倍,則圖像不同位置處積累的信號強度不同,并呈周期性變化,這是單幀圖像的情況。在視頻序列上,如果滿足一定條件,視頻會出現條紋模式在垂直方向上緩慢移動。 二、形成原因 1、光源 …

一條命令教你安裝centos下面的pip服務

yum install -y python-pip轉載于:https://blog.51cto.com/12131824/2177874

strcpy,memcpy,memset函數實現

strcpy 實現&#xff0c;只能拷貝字符串 char* strcpy(char* des,const char* source) {char* rdes; assert((des ! NULL) && (source ! NULL));while((*des *source)!\0);return r; } memcpy 實現&#xff0c;注意目的地址和源地址重合的情況&#xff0c;以及強制類…

CMOS圖像傳感器——圖像傳感器噪聲

圖像傳感器噪聲取決于圖像傳感器的制作工藝、內部結構及內部補償技術等原因,噪聲反應了圖像傳感器的內部特性。CMOS圖像傳感器基本原理見: CMOS圖像傳感——概述_滄海一升的博客-CSDN博客_cmos圖像傳感器CMOS圖像傳感器基本介紹https://blog.csdn.net/qq_21842097/article/d…

TI Davinci DM6441嵌入式Linux移植攻略——UBL移植篇

目錄(?)[] 一DM6441的Boot過程簡介二DM6441的UBL移植 CCS文件夾Common文件夾GNU文件夾 移植DDR2移植Nand Flash其它 聲明&#xff1a;本文參考網友zjb_integrated的文章《TI Davinci DM6446開發攻略——UBL移植》和《DAVINCI DM365-DM368開發攻略——U-BOOT-2010.12及UBL的移…

python接口自動化測試(二)-requests.get()

環境搭建好后&#xff0c;接下來我們先來了解一下requests的一些簡單使用&#xff0c;主要包括&#xff1a; requests常用請求方法使用&#xff0c;包括&#xff1a;get&#xff0c;postrequests庫中的Session、Cookie的使用其它高級部分&#xff1a;認證、代理、證書驗證、超時…

從一個Android碼農視角回顧2018GDD大會

兩天的GDD大會結束了&#xff0c;很開心&#xff0c;可以看得出&#xff0c;這次Google真的很用心。不但分享的內容質量很高。而且又有得吃又有得玩&#xff0c;還有許多好看的小姐姐&#xff0c;真不妄我請了兩天年假來參加這個大會。先來幾張圖鎮樓 哈哈&#xff0c;跑題了。…

Python3.x和Python2.x的區別[轉]

Python3.x和Python2.x的區別 1.性能 Py3.0運行 pystone benchmark的速度比Py2.5慢30%。Guido認為Py3.0有極大的優化空間&#xff0c;在字符串和整形操作上可 以取得很好的優化結果。 Py3.1性能比Py2.5慢15%&#xff0c;還有很大的提升空間。 2.編碼 Py3.X源碼文件默認使用utf-8…

數字圖像處理——圖像銳化

圖像增強是圖像處理的一個重要環節,早期的圖像處理就是從圖像增強開始的,人們研究對質量低的圖像進行處理以獲得改善質量后的圖像。現今的圖像增強還為后續的圖像處理,如圖像信息提取、圖像識別等,提供更高識別度的圖像。 從圖像處理技術來看,圖像的攝取、編碼、傳輸和處理…

DAVINCI DM365-DM368開發攻略——U-BOOT-2010.12及UBL的移植

從盛夏走到深秋&#xff0c;我們繼續DAVINCI DM365-DM368的開發。說來慚愧&#xff0c;人家51CTO熱情支持本博客&#xff0c;而本人卻一直沒有像其他博客之星一樣頻繁更新博客&#xff0c;心里確實說不過去。管理公司確實很累&#xff0c;有更急的客戶的項目要做&#xff0c;我…

陳天藝1636050045假設跑步者1小時40分鐘35秒跑了24英里。編寫一個程序顯示每小時以公里為單位的平均速度值...

public class AverageSpeed{ public static void main(String[]args){ double speedkm 60/(45.5/14); double speedm speedkm /1.6; system.out.println&#xff08;“averagespeed ”speedm "m/h"&#xff09; } }轉載于:https://www.cnblogs.com/Archon-Cty/p/7…

EMVA 1288 測試標準

一、概述 如果要對比兩臺相機的性能,我們應該關注哪些參數呢,是焦距、像素、還是光圈大小?這些參數通常廣為人知,并且很容易做出對比。但在一些專業領域,例如機器視覺、自動駕駛等行業,計算機算法對圖像有著獨特的要求,這些標準有些已經跟不上數字成像的發展步伐,而且其…

Spring Boot - 修改Tomcat默認的8080端口

前言 默認情況下&#xff0c;Spring Boot內置的Tomcat服務會使用8080端口啟動&#xff0c;我們可以使用以下任何技巧去更改默認的Tomcat端口&#xff1b; 注&#xff1a;我們可以通過server.port0配置&#xff0c;去自動配置一個未被占用的http端口&#xff0c;由操作系統實現。…

2017年度目標

語言&#xff1a;python&#xff0c;lisp&#xff0c;js&#xff0c;html5技術&#xff1a;android&#xff0c;hadoop數學&#xff1a;復變函數&#xff0c;代數&#xff0c;概率&#xff0c;數論英語&#xff1a;reading其他&#xff1a;金融/經濟/股票&#xff0c;歷史/史記…

嵌入式系統系統升級內核雙備份的實現方式

1.nand flash MTD分區 kernels/linux-2.6.31.1-cavm1/drivers/mtd/maps/xxxxx-flash.c /* MTD partitions: From CNW5602 32MB * mtd0: 0x000C0000 00020000 "bootloader" * mtd1: 0x00040000 00020000 "factory_config" * mt…

SerDes接口——架構與電路

隨著通信技術的飛速發展&#xff0c;高速串行互連以其結構簡單&#xff0c;不需要傳輸同步時鐘&#xff0c;相比并行傳輸有更高數據傳輸效率的優點&#xff0c;成為現代通信和數據傳輸的重要組成部分。隨著對數據傳輸速率要求的不斷提高&#xff0c;SERDES應運而生。它是一種時…

Springboot分模塊開發詳解(2):建立子工程

1.創建base-entity 選中base工程&#xff0c;右鍵創建一個新的maven工程 自動選擇了base這個目錄存放子工程 創建后&#xff0c;pom.xml修改成如下內容&#xff1a; <?xml version"1.0"?> <projectxsi:schemaLocation"http://maven.apache.org/POM/4…

到天宮做客(2017寒假培訓測試壓軸題)

個人QQ&#xff1a;757394026團隊QQ&#xff1a;466373640個人博客&#xff1a;www.doubleq.winc/noi/信息學奧數博客&#xff1a;http://www.cnblogs.com/zwfymqz 題目描述 有一天&#xff0c;我做了個夢&#xff0c;夢見我很榮幸的接到了豬八戒的邀請&#xff0c;到天宮陪他吃…