免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

SpringCloud學習系列之五-----配置中心(Con-創新互聯

前言
本篇則介紹基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心(SpringCloud Config)的配置刷新和消息總線(RabbitMQ和Kafka)使用教程。

成都創新互聯公司服務項目包括嫩江網站建設、嫩江網站制作、嫩江網頁制作以及嫩江網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,嫩江網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到嫩江省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!

SpringCloud Config Refresh
在上一篇中我們介紹了springcloud配置中心的本地使用和Git使用的用法,但是當重新修改配置文件提交后,客戶端獲取的仍然是修改前的信息,需要客戶端重啟才可以獲取最新的信息。因此我們需要客戶端能夠動態進行更新,幸好springcloud官方已經給出方案,所以我們只需要使用就行了。

開發準備
開發環境

JDK:1.8
SpringBoot:2.0.6.RELEASE
SpringCloud:Finchley.SR2
注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以后,jdk的版本必須是1.8以上!

確認了開發環境之后,我們再來添加相關的pom依賴。

<dependencies>
<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-server</artifactId>
</dependency>
</dependencies>
服務端
服務端以及注冊中心這塊配置和代碼和之前springcloud-config配置基本一樣即可。注冊中心新項目的的名稱為springcloud-config-bus-eureka,服務端新項目的的名稱為springcloud-config-bus-server。

注冊中心pom配置、application.properties配置和代碼如下:

pom:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.properties:

spring.application.name=springcloud-config-bus-eureka
server.port=8006
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
代碼:

12
3
4
5
6
7
8
9
10
@SpringBootApplication
br/>2
3
4
5
6
7
8
9
10
@SpringBootApplication
public class ConfigBusEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigBusEurekaApplication.class, args);
System.out.println("config bus 注冊中心服務啟動...");
}
}
服務端pom配置、application.properties配置和代碼如下:

pom:

<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-server</artifactId>
</dependency>
</dependencies>
application.properties:

spring.application.name=springcloud-config-server
server.port=9005
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/
spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
spring.cloud.config.server.git.username =
spring.cloud.config.server.git.password =
代碼:

12
3
4
5
6
7
8
9
10
11
@EnableDiscoveryClient
br/>2
3
4
5
6
7
8
9
10
11
@EnableDiscoveryClient
br/>@SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
System.out.println("配置中心服務端啟動成功!");
}
}
客戶端
客戶端這邊在之前springcloud-config-client項目中進行改造,新項目的的名稱為springcloud-config-bus-client,在pom文件中新增如下配置:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-boot-starter-actuator表示對該程序進行監控,可以通過http接口得到該程序的各種信息,詳細的使用可以查看我的這個項目springboot-actuator,可以在注釋中查詢各種接口的使用。

然后再到配置文件application.properties中添加如下配置:

management.endpoints.web.exposure.include=refresh
該配置表示暴露刷新的地址為refresh。

注:如果是SpringBoot1.x的版本,那么配置改成management.security.enabled=false即可。

最后在客戶端的Controller增加一個@RefreshScope注解,該注解表示在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。

12
3
4
5
6
7
8
9
10
11
12
13
@RestController
br/>2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class ClientController {

@Value("${word}")
private String word;

@RequestMapping("/hello")
public String index(@RequestParam String name) {
    return name+","+this.word;
}

}
測試
完成上述的代碼開發后,我們來進行測試Spring-Config是否可以進行配置實時更新。
首先依次啟動springcloud-config-bus-eureka、springcloud-config-bus-server和springcloud-config-bus-client這三個項目。其中9005是服務端springcloud-config-bus-server的端口,9006是第一個客戶端springcloud-config-bus-client的端口。
啟動成功之后,在瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world!!
可以正常得到服務端configtest-pro.properties的配置信息。

然后在把configtest-pro.properties的配置更改為:

word=hello
然后我們再瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world!!
可以發現配置并沒有實時的刷新,查閱官方文檔得知,需要客戶端通過POST方法觸發各自的/refresh,所以這里我們就用Postman工具模擬post請求刷新,然后再查看信息。

使用POST請求如下地址:

http://localhost:9006/actuator/refresh

返回:

[
"word"
]
說明完成了word配置的刷新,我們再瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello
發現已經成功實現配置刷新了!

示例圖:
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

SpringCloud Config Bus
上述的示例中,我們客戶端發現每次獲取最新配置都需要手動進行刷新,如果少的的話還可以使用,但是多的話就比較繁瑣了,雖然我們可以使用類似Github的WebHook的工具。

WebHook是當某個事件發生時,通過發送http post請求的方式來通知信息接收方。

但是當客戶端越來越多的時候WebHook已經不好使用了,每次新增客戶端都需要更改WebHook會顯得很麻煩,springcloud官方給出了非常好的解決方案,Spring Cloud Bus。

Spring cloud bus通過輕量消息代理連接各個分布的節點。這會用在廣播狀態的變化(例如配置變化)或者其他的消息指令。Spring bus的一個核心思想是通過分布式的啟動器對spring boot應用進行擴展,也可以用來建立一個多個應用之間的通信頻道。目前唯一實現的方式是用AMQP消息代理作為通道,同樣特性的設置(有些取決于通道的設置)在更多通道的文檔中。

為什么使用Spring Cloud Bus就可以解決這個問題了呢?
我們不一定非要透徹的理解其原理才可以知道,我們只需要知道它的實現步驟,就可以知道了為什么可以解決了。
步驟如下:

首先,在配置中進行更新配置文件信息,它就會自動觸發post發送bus/refresh;
然后服務端就會將更新的配置并且發送給Spring Cloud Bus;
繼而Spring Cloud bus接到消息之后并通知給使用該配置的客戶端;
最后使用該配置的客戶端收到通知后,就會獲取最新的配置進行更新;
這里我也簡單的畫了下使用Spring Cloud Bus之前和之后的流程圖,方便進行理解。

不使用Spring Cloud Bus獲取配置信息流程圖:

在這里插入圖片描述

使用Spring Cloud Bus獲取配置信息流程圖:

在這里插入圖片描述

開發準備
和上述的環境一樣即可。

RabbitMQ 的安裝教程可以看我之前寫的這篇文章:RabbitMQ的環境安裝及配置。

Kafka 的安裝教程可以看我之前寫的這篇文章:kafka安裝使用教程。

服務端
Spring Cloud Bus 主要的使用的MQ主要使用的是,RabbitMQ和Kafka。至于使用的話就可以根據情況來進行選擇,主要使用的是哪個MQ就用哪一個就行了。這里我們就用RabbitMQ作為示例來進行講解,Kafka的使用也差不多,也無在乎配置更改而已。

首先在springcloud-config-bus-server服務端的pom文件添加如下配置:

<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-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

</dependencies>
注: spring-boot-starter-actuator這個是必須的,不然是無法在服務端進行配置刷新請求的。如果是使用的kafka的話,只需將spring-cloud-starter-bus-amqp改成spring-cloud-starter-bus-kafka即可。

然后再到配置文件中添加如下配置:

配置信息:

spring.application.name=springcloud-config-bus-server
server.port=9005
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/
spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
spring.cloud.config.server.git.username =
spring.cloud.config.server.git.password =

management.endpoints.web.exposure.include= bus-refresh
spring.cloud.bus.enabled = true
spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest
配置說明:

spring.application.name : 這個是指定服務名稱。
server.port:服務指定的端口。
eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和注冊服務都需要依賴這個地址。
spring.cloud.config.server.git.uri: 配置的Git長褲的地址。
spring.cloud.config.server.git.search-paths: git倉庫地址下的相對地址 多個用逗號”,”分割。
spring.cloud.config.server.git.username:git倉庫的賬號。
spring.cloud.config.server.git.password:git倉庫的密碼。
management.endpoints.web.exposure.include:SpringBoot2.x之后必須添加次配置,和上述的客戶端springcloud-config-bus-client增加的的配置一樣,名稱不一樣是為了做區分。
spring.cloud.bus.enabled:是否啟用springcloud config bus。
spring.cloud.bus.trace.enabled:開啟跟蹤總線事件。
spring.rabbitmq.host: rabbitmq的地址。
spring.rabbitmq.port: rabbitmq的端口。
spring.rabbitmq.username: rabbitmq的用戶名。
spring.rabbitmq.password: rabbitmq的密碼。
注:如果是kafka的話,添加kafka的配置信息spring.kafka.bootstrap-servers,填寫kafka的地址和端口即可。

服務端代碼和之前一樣即可,在程序主類中,額外添加@EnableConfigServer注解,該注解表示啟用config配置中心功能。代碼如下:

12
3
4
5
6
7
8
9
10
11
@EnableDiscoveryClient
br/>2
3
4
5
6
7
8
9
10
11
@EnableDiscoveryClient
br/>@SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
System.out.println("配置中心服務端啟動成功!");
}
}
完成上述代碼之后,我們的配置中心服務端已經構建完成了。

注冊中心和之前保持一致就可以了。

客戶端
客戶端這邊的變動基本不大,增加一個rabbitmq的jar包和相應的配置文件即可。
在springcloud-config-bus-clinet的pom文件添加如下配置:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
bootstrap.properties文件的配置信息和之前的基本一樣,完整的配置如下:

配置信息:

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-bus-server
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
配置說明:

spring.cloud.config.name: 獲取配置文件的名稱。
spring.cloud.config.profile: 獲取配置的策略。
spring.cloud.config.label:獲取配置文件的分支,默認是master。如果是是本地獲取的話,則無用。
spring.cloud.config.discovery.enabled: 開啟配置信息發現。
spring.cloud.config.discovery.serviceId: 指定配置中心的service-id,便于擴展為高可用配置集群。
eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和注冊服務都需要依賴這個地址。
注:上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部分內容才能被正確加載。因為bootstrap.properties的相關配置會先于application.properties,而bootstrap.properties的加載也是先于application.properties。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.properties,不然客戶端是無法獲取配置中心參數的,會啟動失敗!

application.properties配置文件新增的配置基本和服務端的一樣,完整的配置如下:

spring.application.name=springcloud-config-bus-client
server.port=9006
management.endpoints.web.exposure.include=refresh
spring.cloud.config.failFast=true
spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest
配置說明:

spring.application.name: 這個是指定服務名稱。
server.port:服務指定的端口。
management.endpoints.web.exposure.include:暴露刷新的地址。
spring.cloud.bus.enabled:是否啟用springcloud config bus。
spring.cloud.bus.trace.enabled:開啟跟蹤總線事件。
spring.rabbitmq.host: rabbitmq的地址。
spring.rabbitmq.port: rabbitmq的端口。
spring.rabbitmq.username: rabbitmq的用戶名。
spring.rabbitmq.password: rabbitmq的密碼。
程序主類代碼,和之前的基本一致。代碼如下:

主程序代碼示例:

12
3
4
5
6
7
8
9
10
@EnableDiscoveryClient
br/>2
3
4
5
6
7
8
9
10
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
System.out.println("配置中心客戶端啟動成功!");
}
}
控制層代碼:

12
3
4
5
6
7
8
9
10
11
12
13
@RestController
br/>2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class ClientController {

@Value("${word}")
private String word;

@RequestMapping("/hello")
public String index(@RequestParam String name) {
    return name+","+this.word;
}

}
完成上述的項目開發之后,我們把上面的項目復制一下,項目名稱為springcloud-config-bus-client2,然后把它的端口改為9007即可。

到此,客戶端的項目也就構建完成了。

功能測試
完成如上的工程開發之后,我們來進行測試。

我們首先啟動RabbitMQ服務,然后再依次啟動springcloud-config-bus-eureka、springcloud-config-bus-server、springcloud-config-bus-client和springcloud-config-bus-client2這四個項目。其中9005是服務端springcloud-config-bus-server的端口,9006是第一個客戶端springcloud-config-bus-client的端口,9007是是第二個客戶端springcloud-config-bus-client2的端口。

全局刷新測試
啟動成功之后,在瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello
在瀏覽器輸入:

http://localhost:9007//hello?name=xuwujing

界面返回:

xuwujing,hello
可以正常得到服務端configtest-pro.properties的配置信息。

然后在把configtest-pro.properties的配置更改為:

word=hello!!
然后在使用Postman工具進行發起POST請求,只不過這次的地址是服務端的地址和端口。

使用POST請求如下地址:

http://localhost:9005/actuator/bus-refresh

然后我們再瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello!!
瀏覽器輸入:

http://localhost:9007//hello?name=pancm

界面返回:

xuwujing,hello!!
示例圖:

在這里插入圖片描述
在這里插入圖片描述

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

局部刷新測試
完成上述全局刷新測試之后,有時我們只想刷新部分微服務的配置,那么便可以使用/actuator/bus-refresh/{destination}端點的 destination 參數來定位要刷新的應用程序。

我們繼續更改configtest-pro.properties的配置為:

word=hello!!!
然后依舊使用Postman工具發送post請求,地址為:

http://localhost:9005/actuator/bus-refresh/springcloud-config-bus-client2

然后我們再瀏覽器輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello!!
瀏覽器輸入:

http://localhost:9007//hello?name=pancm

界面返回:

xuwujing,hello!!!
發現只有springcloud-config-bus-client2客戶端的配置更新,另一個springcloud-config-bus-client沒有進行刷新,達到了我們的目的。

示例圖:

在這里插入圖片描述
在這里插入圖片描述

上述示例完成之后,我們把SpringCloud Config Refresh的測試在進行一遍,發現依舊可以實現,因此我們發現只要開啟Spring Cloud Bus 后,不管是對服務端還是客戶端,執行/actuator/bus-refresh都是可以更新配置的。

如果我們想進行跟蹤總線事件的話,只需要在刷新配置之后,在地址后面添加/trace或/actuator/httptrace即可。

其他
項目地址

另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前名稱:SpringCloud學習系列之五-----配置中心(Con-創新互聯
文章來源:http://m.newbst.com/article6/dcioig.html

成都網站建設公司_創新互聯,為您提供服務器托管網站設計定制網站網站導航標簽優化響應式網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司