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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Cloud配置–外部化应用程序配置

發布時間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud配置–外部化应用程序配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本教程是關于Spring Cloud Config的,這里我們將研究如何在不同的應用程序中針對不同的環境(例如開發人員,本地,產品等)使用Spring Cloud Config來管理和存儲分布式外部配置屬性。開發一個簡單的云應用程序以使用云配置外部化應用程序配置屬性,然后將同一應用程序擴展為使用發現服務器注冊該應用程序,在運行時更新配置以及對敏感屬性進行加密和解密。

在分布式云系統中,我們有許多較小的系統,它們共同構成一個較大的系統,因此,我們有多個配置文件。 舉例來說,如果我們使用微服務,則每個微服務都將擁有自己的配置文件,并且由于可能運行多個實例并且這些配置管理面向部署,因此在該應用程序中管理該配置變得很麻煩,即使不錯過它也具有挑戰性。某些實例的任何配置更改。

為此,Spring Cloud團隊提供了易于實施的Spring Cloud配置,它為分布式系統中的外部化配置提供了服務器和客戶端支持。 使用Config Server,您可以集中管理所有環境中應用程序的外部屬性

Spring cloud config是一個Web應用程序,它公開REST端點以訪問配置屬性,它支持JSON,屬性和yaml等多種輸出格式,它支持的不同后備存儲為git(default),SVN,文件系統。 在我們的示例中,我們將使用git作為配置屬性的后備存儲。

設置Git支持的商店

首先,讓我們建立我們的后備商店。 我們將使用github來存儲我們的屬性,為此,我在這里創建了一個簡單的github項目來存儲配置。 它基本上有3個.properties文件。 application.properties用于存儲全局屬性, spring-cloud-config-client.properties用于存儲應用彈簧云配置客戶端的全球性,同樣我們有spring-cloud-config-client-local.properties存儲本地屬性對于應用程序spring-cloud-config-client

spring-cloud-config-client.properties

server.contextPath=spring-cloud-config-client test.property=property from cloud config

spring-cloud-config-client-local.properties

test.local.property=test local property

本地屬性文件將具有配置屬性,以使用本地配置文件運行spring boot應用程序;如果要在本地環境中覆蓋全局配置文件,例如DB屬性,則可以定義全局配置文件的現有屬性。

Spring Cloud Config Server實施

這將是一個簡單的spring boot應用程序。 對于此實現,請首先從start.spring.io使用以下配置下載一個演示spring boot應用程序,我們將在本教程后面的部分中使用發現服務器配置。

現在將其導入IDE,您可以找到以下Maven配置。

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>

讓我們定義此應用程序的應用程序配置。 為了使我們的示例簡單,我們現在沒有與發現服務器相關的配置。以下是我們在上一節中討論的git URL。

application.properties

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

以下是我們主要的Spring Boot應用程序的實現。 在簡單注釋上– @EnableConfigServer將啟用spring cloud config所需的配置。 注意: –在運行此類之前,您可以在pom.xml中注釋eureka依賴關系,以避免不必要的錯誤日志,因為我們現在尚未進行任何與發現服務器相關的配置。

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);} }

在類之上作為Java應用程序運行將暴露以下REST端點。

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

在這里,application是應用程序的名稱。 例如,如果我們將客戶端應用程序名稱命名為spring-cloud-config-client,則端點URL變為spring-cloud-config-client-dev.properties,其中dev是spring boot活動配置文件。這里的標簽是git brnach是可選參數。

Spring Cloud Config客戶端實施

對于云配置客戶端,我們需要以下依賴項。稍后需要doscovery客戶端進行服務發現。 現在,spring-cloud-starter-config就足夠了。

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>

要使用客戶端應用程序引導我們的Spring Cloud配置配置,我們需要bootstrap.yml以下條目。 以下配置將為應用名稱spring-cloud-config-client和本地配置文件調用屬性配置文件,并且我們的云配置服務器在http:// localhost:8888上運行

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

現在讓我們定義我們的spring boot應用程序類。

SpringCloudConfigClientApplication.java

package com.devglan.springcloudconfigclient;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringCloudConfigClientApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudConfigClientApplication.class, args);} }

現在,讓我們定義我們的控制器類,并使用@Value注釋通過Spring Cloud config使用外部屬性。

DemoController.java

package com.devglan.springcloudconfigclient.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class DemoController {@Value("${test.property}")private String testProperty;@Value("${test.local.property}")private String localTestProperty;@RequestMapping("/")public String test() {StringBuilder builder = new StringBuilder();builder.append("test property - ").append(testProperty).append(" ").append("local property - ").append(localTestProperty);return builder.toString();} }

在屬性文件中,我們定義了test.property和test.local.property屬性,將其注入到控制器中。在屬性文件中,我們將server.contextPath定義為spring-cloud-config-client,因此我們的客戶端應用程序將是可從http:// localhost:8080 / spring-cloud-config-client /訪問

將服務發現與Spring Cloud Config集成

在上一篇文章中,我們使用spring-cloud-netflix-eureka創建了一個服務發現應用程序 。 我們將使用在默認端口8761上運行的同一發現服務器。要與發現服務器集成,讓我們首先編輯服務應用程序的application.properties文件,以將自身注冊為該服務器的服務。使用發現服務器將該應用程序注冊為應用程序名稱– spring-cloud-config-example

application.properties

spring.application.name=spring-cloud-config-example eureka.client.service-url.defaultZone=http://localhost:8761/eureka

使用@EnableDiscoveryClient注釋SpringCloudConfigExampleApplication.java ,以便此應用程序向發現客戶端注冊自己。

此外,我們需要在客戶端應用程序中配置相同的內容以使用發現服務器來發現配置服務器。為此,使用SpringCloudConfigClientApplication.java注釋SpringCloudConfigClientApplication.java并在bootstrap.properties文件中進行以下條目以自動發現云配置服務。默認情況下,云配置客戶端會在發現服務器中使用任何發現配置服務器查找名稱為configserver的應用程序,但在我們的情況下,云配置服務器的應用名稱為spring-cloud-config-example,因此在客戶端中使用它來覆蓋它屬性spring.cloud.config.discovery.serviceId

bootstrap.properties

spring.application.name=spring-cloud-config-client spring.profiles.active=local #spring.cloud.config.uri=http://localhost:8888 spring.cloud.config.discovery.enabled=true eureka.client.service-url.defaultZone=http://localhost:8761/eureka spring.cloud.config.discovery.serviceId=spring-cloud-config-example

現在啟動發現服務器,然后啟動云配置服務器,再啟動客戶端應用程序,然后訪問http:// localhost:8080 / spring-cloud-config-client / ,您可以期望獲得與上述相同的結果。

因此,總之,首先啟動發現服務器,這會將端點公開為http:// localhost:8761 / eureka以注冊服務。 現在,當云配置服務器啟動時,它將使用服務ID spring-cloud-config-example注冊其自身,并將端點公開為http://192.168.1.6:8888/。 現在,當客戶端啟動時,它首先嘗試解析配置屬性。 為此,它使用發現服務器來發現服務ID為spring-cloud-config-example的配置服務器。 此后,解析基本URL,然后將/{application}-{profile}.properties附加到該URL并獲取配置屬性。 最終網址變為– http:// localhost:8888 / spring-cloud-config-client-local.properties

在運行時更新云配置

這是spring cloud config的一項很酷的功能,它可以在運行時更新配置屬性,而無需重新啟動應用程序。 例如,您可以更改日志級別。要在運行時更新云配置,可以在git項目中更改配置屬性,然后推送到存儲庫。 然后,我們可以將Spring Boot執行器/refresh端點或/bus/refresh與spring cloud總線一起使用,或者將VCS + / monitor與spring-cloud-config-monitor和spring-cloud-bus一起使用。 但是這樣做不會刷新用@Value或@Bean注釋的屬性,因為這些屬性是在應用程序啟動期間初始化的。 為了刷新這些屬性,spring提供了@RefreshScope批注。我們將在下一篇文章中通過一個示例實現這一點– 運行時的Spring cloud config refresh屬性

加密和解密敏感配置

這是spring cloud config所提供的另一個有用功能。數據庫密碼,用戶名等配置是敏感配置,為此加密和解密spring提供了很多功能,例如REST或飛行中的加密配置。使用對稱和非對稱密鑰進行加密和解密。 我們將在下一個教程中創建一個示例應用程序,其中包含有關此主題的示例。以下是一個示例應用程序.properties具有加密的配置。這是加密和解密敏感配置的完整配置

application.properties

spring.datasource.username=root spring.datasource.password={cipher}ABCFGVH75858GFHDRT

結論

在本教程中,我們了解了Spring Cloud配置。 我們創建了我們的云配置服務器,客戶端和發現服務器以注冊該服務。可以從此處下載源。如果您有任何要添加或共享的內容,請在下面的評論部分中進行共享。

翻譯自: https://www.javacodegeeks.com/2018/03/spring-cloud-configuration-externalize-application-configuration.html

總結

以上是生活随笔為你收集整理的Spring Cloud配置–外部化应用程序配置的全部內容,希望文章能夠幫你解決所遇到的問題。

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