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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从零开始搭建spring-cloud(5) ----config

發(fā)布時間:2024/9/16 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从零开始搭建spring-cloud(5) ----config 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring Boot profile

在Spring Boot應(yīng)用中,我們可以將配置內(nèi)容寫入application.yml,設(shè)置多個profile,也可以用多個application-{profile}.properties文件配置,并在啟動時指定–spring.profiles.active={profile}來加載不同環(huán)境下的配置。
在Spring Cloud微服務(wù)架構(gòu)中,這種方式未必適用,微服務(wù)架構(gòu)對配置管理有著更高的要求,如:

  • 集中管理:成百上千(可能沒這么多)個微服務(wù)需要集中管理配置,否則維護困難、容易出錯;
  • 運行期動態(tài)調(diào)整:某些參數(shù)需要在應(yīng)用運行時動態(tài)調(diào)整(如連接池大小、熔斷閾值等),并且調(diào)整時不停止服務(wù);
  • 自動更新配置:微服務(wù)能夠在配置發(fā)生變化是自動更新配置。

以上這些要求,傳統(tǒng)方式是無法實現(xiàn)的,所以有必要借助一個通用的配置管理機制,通常使用配置服務(wù)器來管理配置。

Spring Cloud Config

Spring Cloud Config 為分布式系統(tǒng)的外部配置提供了服務(wù)端和客戶端的支持方案。在配置的服務(wù)端您可以在所有環(huán)境中為應(yīng)用程序管理外部屬性的中心位置。客戶端和服務(wù)端概念上的Spring Environment 和 PropertySource 抽象保持同步, 它們非常適合Spring應(yīng)用程序,但是可以與任何語言中運行的應(yīng)用程序一起使用。當(dāng)應(yīng)用程序在部署管道中從一個開發(fā)到測試直至進入生產(chǎn)時,您可以管理這些環(huán)境之間的配置,并確保應(yīng)用程序在遷移時具有它們需要運行的所有內(nèi)容。服務(wù)器存儲后端的默認(rèn)實現(xiàn)使用git,因此它很容易支持標(biāo)記版本的配置環(huán)境,并且能夠被管理內(nèi)容的各種工具訪問。很容易添加替代的實現(xiàn),并用Spring配置將它們插入。
Spring Cloud Config 包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用。Spring cloud使用git或svn存放配置文件,默認(rèn)情況下使用git,我們先以git為例做一套示例。
Spring Cloud Config同樣分為server端和client端,一般將配置都放在Git上,這樣版本管理比較方便。

Spring Cloud Config Server

新建項目config-server

pom.xml文件內(nèi)容如下:

<?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>cn.ac.iie</groupId><artifactId>config</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.1.4.RELEASE</version><scope>import</scope><type>pom</type></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.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-netflix-eureka-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.4.RELEASE</version><configuration><mainClass>cn.ac.iie.App</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins></build> </project>

添加App.java,內(nèi)容如下:

@SpringBootApplication @EnableEurekaClient @EnableConfigServer public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }

修改application.properties,內(nèi)容如下:

spring.application.name=config eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/ spring.cloud.config.server.git.uri=http://swarm-worker1/duandingyang/config-repo.git spring.cloud.config.server.git.username=duandingyang spring.cloud.config.server.git.password=12345678 server.port=8085

在gitlab上新建一個項目config-repo,然后在新建一個文件elasticsearch.properties,文件內(nèi)容跟之前的elasticsearch項目中的配置文件內(nèi)容一樣,如下所示:

management.endpoints.web.exposure.include=health,info,env,metrics spring.elasticsearch.rest.uris=swarm-manager:9200 management.endpoint.health.show-details=always logging.level.root=INFO logging.file=user.log es.ips[0]=swarm-manager es.port=9300 es.clusterName=my-applicationeureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/spring.application.name=es-service server.port=8082

啟動項目,在Eureka中可以看到Config服務(wù)已經(jīng)成功啟動

然后我們訪問http://localhost:8085/elasticsearch-a.properties:

如果訪問:http://localhost:8085/elasticsearch-b.properties也可以訪問:

可以看到正確輸出了我們在gitlab上的配置文件。
為什么要在文件名后加一個a?,如果不寫就會報錯

如果在瀏覽器中輸入:http://localhost:8085/elasticsearch-a.yml:

自動會對文件格式進行轉(zhuǎn)換。此外還可以轉(zhuǎn)換json:

瀏覽器地址欄中的命名格式寫法

  • /{name}-{profiles}.yml:
  • /{label}/{name}-{profiles}.yml
    name表示文件名(或者服務(wù)名),profiles表示環(huán)境,label表示分支(如果不寫,默認(rèn)是master分支)
  • /{name}-{profiles}.yml方式

    在gitlab上面新建一個文件elasticsearch-test.properties,內(nèi)容如下:

    management.endpoints.web.exposure.include=health,info,env,metrics spring.elasticsearch.rest.uris=swarm-manager:9200 management.endpoint.health.show-details=always logging.level.root=INFO logging.file=user.log es.ips[0]=swarm-manager es.port=9300 es.clusterName=my-application eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/ spring.application.name=es-serviceenv=test

    跟之前的內(nèi)容是一樣的,加了一行env=test,然后在瀏覽器中輸入:http://localhost:8085/elasticsearch-test.json,同樣可以訪問配置文件:

    /{label}/{name}-{profiles}.yml

    新建分支release

    release分支會從master中拷貝一份,修改elasticsearch-test.json,添加內(nèi)容:label=release
    然后通過訪問http://localhost:8085/release/elasticsearch-test.json,可以得到release分支下的配置文件

    可以看到多了一個label=release

    spring-cloud config會從遠(yuǎn)端的git拉取到本地的git

    在控制臺可以查看本地的git路徑:

    可以修改配置:spring.cloud.config.server.git.basedir=E:/git/neimeng/config/basedir來修改git的路徑.

    Spring-config client

    上面介紹了config server的使用,現(xiàn)在介紹client的使用。首先修改elasticsearch服務(wù)的依賴,添加spring cloud config依賴:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency>

    替換application.properties文件:

    server.port=8082spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=config spring.cloud.config.profile=test spring.application.name=elasticsearch

    如果僅僅是這樣啟動的話,會報錯。理由是不知道這個配置文件中的配飾項的先后順序。因此需要先加載這個配置文件中的內(nèi)容。
    因此需要修改application.properties為bootstrap.properties,此外如果eureka的端口不是默認(rèn)的8761,程序啟動時默認(rèn)從localhost:8761獲取eureka中的service-id,當(dāng)修改端口后,獲取不到service-id,因此會從默認(rèn)的localhost:8888獲取,便會報錯找不到service-id。因此需要將git上面的配置eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/拿出來,寫到bootstrap.properties中。這樣就會首先讀取eureka,然后在eureka中找服務(wù)。因為我們的配置文件中有方括號,程序無法識別,需要將這個配置放在bootstrap.properties中。
    bootstrap.properties內(nèi)容如下:

    spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=config spring.cloud.config.profile=test eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/ es.ips[0]=swarm-manager

    application.properties內(nèi)容如下:

    server.port=8082 spring.application.name=elasticsearch

    遠(yuǎn)端git的elasticsearch-test.properties配置文件內(nèi)容如下:

    management.endpoints.web.exposure.include=health,info,env,metrics spring.elasticsearch.rest.uris=swarm-manager:9200 management.endpoint.health.show-details=always logging.level.root=INFO logging.file=user.log es.port=9300 es.clusterName=my-applicationeureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/spring.application.name=es-service server.port=8082

    這樣就配置成功了。

    動態(tài)刷新配置SpringCloud Config Bus

    當(dāng)我們修改git上的配置時,我們的項目中的配置并沒有修改。
    我們需要在config server端添加SpringCloud Config Bus依賴:

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

    然后在application.properties中添加:

    spring.rabbitmq.host=swarm-manager spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest

    在RabbitMQ中就會出現(xiàn)下面的隊列:

    在config client中引入config bus依賴:

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

    然后在application.properties中添加:

    spring.rabbitmq.host=swarm-manager spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest

    啟動client,查看RabbitMQ管理頁面:

    可以看到多了一個隊列,說明注冊成功。
    然后需要在config server中暴露api,修改application.properties:

    management.endpoints.web.exposure.include=*

    啟動服務(wù),然后發(fā)送請求:

    查看RabbitMQ:

    應(yīng)用

    在git上面elasticsearch-test.properties中添加內(nèi)容:

    girl.name=Jenny girl.age=18

    在elasticsearch項目中新建GirlConfig.java,內(nèi)容如下:

    package cn.ac.iie.bean;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component;@Component @ConfigurationProperties("girl") @RefreshScope public class GirlConfig {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} }

    然后在新建GirlController.java,內(nèi)容如下:

    @RestController public class GirlController {@Autowiredprivate GirlConfig girlConfig;@GetMapping("/girl/print")public String print() {return "" + girlConfig.getName() + "," + girlConfig.getAge();}}

    這樣訪問http://localhost:8082/girl/print

    當(dāng)我們修改配置文件之后:

    girl.name=Jenny girl.age=30

    首先發(fā)送請求:

    再次發(fā)送請求:

    就可以看到更新后的配置了。
    每次我們修改配置之后,都需要手動刷新http://localhost:8085/actuator/bus-refresh,如何避免這種方式?

    webhooks

    使用webhooks可以實現(xiàn)此功能。

    總結(jié)

    以上是生活随笔為你收集整理的从零开始搭建spring-cloud(5) ----config的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。