日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring cloud+dotnet core搭建微服务架构:配置中心续(五)

發布時間:2023/12/4 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring cloud+dotnet core搭建微服务架构:配置中心续(五) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

上一章最后講了,更新配置以后需要重啟客戶端才能生效,這在實際的場景中是不可取的。由于目前Steeltoe配置的重載只能由客戶端發起,沒有實現處理程序偵聽服務器更改事件,所以還沒辦法實現徹底實現這一功能。這一章的例子,客戶端的部分我們采用Java來實現。Steeltoe更新以后我會及時把 .Net Core的實現方式補全。

實際上也并不需要重啟,客戶端調用IConfigurationRoot.Reload()方法也可以實現這個功能,但是去請求客戶端也不是一個好辦法,因為N節點的配置中心客戶端你沒辦法一一去調用。

[HttpGet("/reload")]public string Reload(){_config?.Reload();//刷新配置return _config?["name"]; }

代碼部分

上一章我們一共創建了2個應用程序,一個配置中心服務端和一個配置中心客戶端。在分布式場景中,任何單點都是有問題的,所以我們首先對其優化,兩個配置中心服務端,三個配置中心客戶端,并全部注冊到服務中心。

創建配置中心服務端

首先還是創建一個服務中心的應用程序,參考第一章內容。
訪問http://localhost:5000
打開網站返回表示創建服務中心成功。
然后創建配置中心服務端,參考第四章內容,我們通過修改配置文件來實現兩個服務端。端口分別為5100,5200。
創建兩個配置文件application-s1.properties,application-s2.properties

application-s1.properties
server.port=5100spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/longxianghui/configs.git#git用戶名和密碼#spring.cloud.config.server.git.username=xxx#spring.cloud.config.server.git.password=xxx#git倉庫目錄#spring.cloud.config.server.git.search-paths=xxx,xxx,xxxeureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/
application-s2.properties
server.port=5200spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/longxianghui/configs.git#git用戶名和密碼#spring.cloud.config.server.git.username=xxx#spring.cloud.config.server.git.password=xxx#git倉庫目錄#spring.cloud.config.server.git.search-paths=xxx,xxx,xxxeureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

使用maven打包

然后在終端分別執行命令行

java -jar target/config-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s1 java -jar target/config-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s2

可以開多終端或者直接在Intellij IDEA里面運行

啟動完成后分別執行
http://localhost:5100/demo/prod,
http://localhost:5200/demo/prod,
返回數據啟動成功

創建配置中心客戶端程序

使用IDEA創建spring boot程序

pom.xml
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency>
ConfigClientApplication.java
@SpringBootApplication@EnableDiscoveryClientpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);} }
Demo.java
@Component@ConfigurationProperties()public class Demo {private String name; ?
?private Integer age; ?
??private String env; ? ?//get and set ...}
DemoController.java
@RestControllerpublic class DemoController { ? ?
@Autowiredprivate Demo demo; ? ?@RequestMapping("demo") ? ?public Demo demo() { ? ? ? ?return demo;} }

為了模擬集群效果我們創建3個配置文件

application-c1.properties
spring.application.name=config-client server.port=5101
application-c2.properties
spring.application.name=config-client server.port=5102
application-c3.properties
spring.application.name=config-client server.port=5103

同時要非常注意的是,創建一個bootstrap.properties,這個配置在application配置之前啟動,相關的spring cloud config的配置都需要加到這個配置文件里面,加到application配置文件里面無效

bootstrap.properties
spring.cloud.config.name=demo spring.cloud.config.profile=dev#開啟服務發現功能spring.cloud.config.discovery.enabled=true#服務idspring.cloud.config.discovery.serviceId=config-server#服務中心地址eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

打包代碼分別執行下面3行命令

java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c1 java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c2 java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c2

先訪問http://localhost:5000/,服務都注冊成功。

分別訪問
http://localhost:5101/demo
http://localhost:5102/demo
http://localhost:5103/demo

能夠正常的訪問數據了,但是如果我們修改git的配置信息,配置中心客戶端并不會主動獲取新的配置信息。我們想一下有沒有辦法當我們提交配置信息后通知給所有客戶端呢?說到通知大家馬上就想到了消息隊列,通知多客戶端的模式,是不是就是消息隊列里面的廣播模式?想明白這點我們繼續看下面的內容,繼續改造我們的程序。spring cloud config提供了消息隊列模式,通過調用提供的REST接口來通知到客戶端來更新配置。
首先安裝rabbitmq
我們繼續改造配置中心的服務端和客戶端
服務端和客戶端的配置一樣

pom.xml 添加mq的擴展
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>
application.properties 添加mq的配置信息
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672spring.rabbitmq.username=guest spring.rabbitmq.password=guest management.security.enabled=false

分別啟動這5個服務(2個服務端,3個客戶端)
修改demo-dev.yml
name: mickey2017并提交到github
rabbitmq提供了一個REST地址來刷新mq通知客戶端。我們通過postman或者命令行來模擬post提交:http://localhost:5100/bus/refresh

這里調用5200也是可以的,調用客戶端的3個端口一樣可以 (http://localhost:5101/bus/refresh) ,前面說了調用客戶端是不可取的,所以我們調用服務端的地址。
最后訪問http://localhost:5101/demo

后記

通過這一章內容我們我們成功的改造了配置中心,實際的開發場景,我們總不能更新了配置就手動去模擬post提交吧?所以我們可以借助git的webhook功能,當提交代碼以后就給配置中心服務端發送請求。再想深入一點,配置中心服務端不對外網暴露呀?那么我們可以用過通過Api網關來訪問,同時使用服務發現的方式,又解決了要指定具體配置中心地址的問題。

最近群里面總有同學問授權的問題,那么我們下一章講微服務下的Api授權。

示例代碼

所有代碼均上傳github。代碼按照章節的順序上傳,例如第一章demo1,第二章demo2以此類推。
求推薦,你們的支持是我寫作最大的動力,我的QQ群:328438252,交流微服務。

相關文章:?

  • 手把手教你使用spring cloud+dotnet core搭建微服務架構:服務治理(-)

  • spring cloud+dotnet core搭建微服務架構:服務發現(二)

  • spring cloud+dotnet core搭建微服務架構:Api網關(三)

  • 微服務~Eureka實現的服務注冊與發現及服務之間的調用

  • spring cloud+dotnet core搭建微服務架構:配置中心(四)

原文地址:http://www.cnblogs.com/longxianghui/p/7687825.html


.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

總結

以上是生活随笔為你收集整理的spring cloud+dotnet core搭建微服务架构:配置中心续(五)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。