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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在运行时在Spring Cloud Config中刷新属性配置

發布時間:2023/12/3 javascript 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在运行时在Spring Cloud Config中刷新属性配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在本系列Spring Cloud Config的教程系列中,我們將討論在運行時刷新屬性配置的過程,我們將使用Spring Boot致動器/refresh端點進行/refresh 。 此外,我們還將研究使用@RefreshScope注釋刷新@Value屬性。

在我的Spring Cloud Config的上一教程中 ,我們使用發現服務器和發現客戶端建立了一個云配置服務,并成功創建了一個示例,以在具有GIT支持的存儲的分布式環境中讀取應用程序配置屬性。在這里,我們將繼續進行演示運行時在Spring Cloud Config中刷新屬性配置的功能。

在本文中,我們將只專注于刷新配置屬性。 因此,我們將不會使用與發現服務器相關的配置。 我們將有一個配置服務器,用于從GIT存儲庫和帶有執行器項目的配置客戶端中加載屬性。

刷新屬性的不同方法

刷新配置屬性的一種簡單方法是使用spring boot促動器提供的/refresh端點,但這是一個手動過程,需要針對所有實例進行觸發。另一種方法是/bus/refresh與spring-cloud-bus和在這種情況下,所有實例都訂閱一個事件,并且一旦觸發該事件,所有配置屬性都將通過Spring Cloud Bus廣播自動刷新。刷新這些屬性的第三種方法是通過連接VCS。 在本文中,我們將討論彈簧啟動執行器的刷新端點。

Spring Cloud Config Server實施

在上一篇文章中,我們已經為該實現做好了準備。 在這里,讓我們簡要地討論一下。 我們在配置服務器和Spring Boot主應用程序中定義了以下application.properties,它將REST端點公開為http:// localhost:8888,以供客戶端獲取配置屬性。

application.properties

server.port=8888 spring.cloud.config.server.git.uri=https://github.com/only2dhir/config-repo.git

SpringCloudConfigExampleApplication.java

package com.devglan.springcloudconfigexample;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication @EnableConfigServer public class SpringCloudConfigExampleApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudConfigExampleApplication.class, args);} }

我們在https://github.com/only2dhir/config-repo.git上定義了外部配置屬性。在這里,我們為活動配置文件的本地和全局屬性定義了屬性。

Spring Cloud Config客戶端實施

對于客戶,我們有以下bootstrap.properties defined.This是我們在以前的應用程序定義的相同文件在這里

bootstrap.properties

spring.application.name=spring-cloud-config-client spring.profiles.active=local #spring.cloud.config.uri=http://localhost:8888

使用/ refresh端點刷新配置屬性

/refresh端點僅刷新使用@ConfigurationProperties注釋的那些屬性,這意味著它不刷新在應用程序初始化期間初始化的那些屬性。 例如,我們定義了以下配置類,該類讀取具有隨機前綴的屬性

package com.devglan.springcloudconfigclient;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix="random") public class PropertyConfiguration {private String property;public String getProperty() {return property;}public void setProperty(String property) {this.property = property;} }

我們有以下控制器類,該類使用以random為前綴的屬性,并且還讀取以@Value注釋的屬性

@RestController public class DemoController {@Value("${test.property}")private String testProperty;@Value("${test.local.property}")private String localTestProperty;@Autowiredprivate PropertyConfiguration propertyConfiguration;@RequestMapping("/")public String test() {StringBuilder builder = new StringBuilder();builder.append("global property - ").append(testProperty).append(" || ").append("local property - ").append(localTestProperty).append(" || ").append("property configuration value - ").append(propertyConfiguration.getProperty());return builder.toString();} }

對于端點http:// localhost:8080 / spring-cloud-config-client /,將輸出以下內容。

現在讓我們更改定義的配置根據企業的性質spring-cloud-config-client-local.properties如下。

test.local.property=test local property changed random.property=random property changed

現在,我們將調用執行器的http:// localhost:8080 / spring-cloud-config-client / refresh POST方法來刷新屬性。 以下是具有更新屬性的響應。

現在,如果我們點擊http:// localhost:8080 / spring-cloud-config-client /,我們可以看到來自帶有@ConfigurationProperties注釋的類的屬性已經更新,但是帶有@Value注釋的屬性尚未更新,因為這是初始化的在應用程序啟動期間

要更新使用@Value注釋的屬性,我們需要使用@RefreshScope注釋該類。 因此,這里我們將使用@RefreshScope注釋控制器類并重新啟動客戶端應用程序。再次重新啟動后,我們將在屬性文件中進行更改并將更改推送到git。 這次,我們兩次向屬性值附加了字符串,然后再次調用了刷新端點。 現在,如果我們訪問URL http:// localhost:8080 / spring-cloud-config-client /,我們可以發現用@Value和@ConfigurationProperties注釋的配置屬性都已更新。

翻譯自: https://www.javacodegeeks.com/2018/03/refresh-property-config-at-runtime-in-spring-cloud-config.html

總結

以上是生活随笔為你收集整理的在运行时在Spring Cloud Config中刷新属性配置的全部內容,希望文章能夠幫你解決所遇到的問題。

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