亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringCloud的Config配置中心詳解

 更新時間:2023年07月18日 10:07:43   作者:七月J  
這篇文章主要介紹了SpringCloud的Config配置中心詳解,SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支持,配置服務器為各個不同微服務應用的所有環(huán)境提供了一個中心化的外部配置,需要的朋友可以參考下

微服務

微服務就意味著要將單體應用中的業(yè)務拆分成一個個子服務,每個服務的粒度相對較小,因此系統(tǒng)中會出現(xiàn)大量的服務。由于每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態(tài)的配置管理設施是必不可少的。SpringCloud提供了ConfigServer 來解決這個問題。

1、概述

定義:SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支持,配置服務器為各個不同微服務應用的所有環(huán)境提供了一個中心化的外部配置。

SpringCloud Config分為服務端和客戶端兩個部分:

  • 服務端稱為分布式配置中心,他是一個獨立的微服務應用,用來連接配置服務器并為客戶端提供獲配置信息,加密/解密信息等訪問接口。
  • 客戶端是通過指定的配置中心來管理應用資源,以及與業(yè)務相關的配置內(nèi)容,并在啟動的時候從配置中心獲取和加載配置信息服務器 默認采用git來存儲配置信息,這樣就有助于環(huán)境配置進行版本配置,而且可以通過git客戶端工具來方便的管理和訪問配置內(nèi)容。

具體能做嘛

  • 集中管理配置文件
  • 不同環(huán)境不同配置,動態(tài)化的配置更新,分環(huán)境部署比如 dev/test/prod/beta/release
  • 運行期間動態(tài)調(diào)整配置,不在需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統(tǒng)一拉去配置自己的信息
  • 當配置發(fā)生變動時,服務不需要重啟即可感知到配置的變化并應用新的配置
  • 將配置信息一REST接口的形式暴露

與GitHub整合配置

由于Spring Cloud Config默認使用GIt來存儲配置文件(也有其他方式,比如支持SVN和本地文件),但是最推薦的還是Git,而且使用的是HTTP/HTTPS訪問的形式

2、Config服務端配置和測試

1.用自己的賬號在GitHub上新建一個名為springcloud-config的新Repository

2.將其倉庫克隆到本地,或者直接在github上進行修改

3.新建module 模塊cloud-config-center-3344,即為Cloud的配置中心模塊

4.配置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-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

5.配置YML

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git@github.com:TNoOne/cloud-config.git #github倉庫上面的git倉庫名字
          ##搜索目錄
          search-paths:
            - cloud-config
      #讀取分支
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka #注冊進eureka

6.編寫主啟動類

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

7.windows下修改hosts文件,添加映射

127.0.0.1 config-3344.com

8.測試通過Config微服務是否可以從GitHub上獲取配置內(nèi)容

啟動7001和3344微服務 訪問://config-3344.com:3344/master/config-dev.yml

9.配置讀取規(guī)則

  • /{label}/{appliaction}-{profile}.yml

例如://config-3344.com:3344/master/config-dev.yml

  • /{application}-{profile}.yml

例如://config-3344.com:3344/config-dev.yml

  • /{application}/{profile}[/{label}]

例如://config-3344.com:3344/config/dev/master

總結:

  • label:分支(branch);
  • application:服務名;
  • profiles:環(huán)境(dev/test/prod)

3、Config客戶端配置和測試

1.新建cloud-config-client-3355

2.配置POM

<dependencies>
    <!--不帶server了,說明是客戶端-->
    <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-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.配置bootstrap.yml

bootstrap.yml是什么?

application. yml是用戶級的資源配置項

bootstrap. yml是系統(tǒng)級的,優(yōu)先級更加高

Spring Cloud會創(chuàng)建一個"Bootstrap Context" ,作為Spring應用的 Application Context 的父上下文。

初始化的時候,Bootstrap Context負責從外部源加載配置屬性并解析配置。這兩個上下文共享一個從外部獲取的Environment。

Bootstrap屬性有高優(yōu)先級,默認情況下,它們不會被本地配置覆蓋。

Bootstrap context和Application Context有著不同的約定 ,所以新增了一個bootstrap.ymI文件, 保證Bootstrap Context和Application Context配置的分離。

要將Client模塊下的application.yml文件改為bootstrap.yml,這是很關鍵的, 因為bootstrap.yml是比application.yml先加載的。

bootstrap.ymI優(yōu)先級高于application.yml

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    #Config客戶端配置
    config:
      label: master #分支名稱
      name: config #配置文件名稱
      profile: dev #讀取后綴名稱 上述3個綜合:master分支上config-dev.yml的配置文件被讀取 http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址
#服務注冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4.修改config-dev.yml配置并提交到GitHub中,比如加個變量age或者版本號version

5.編寫主啟動類

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

6.編寫業(yè)務類

@RestController
//@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;  //要訪問的3344上的信息
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

7.測試

啟動7001,3344,3355微服務,訪問://localhost:3355/configInfo

成功實現(xiàn)了客戶端3355訪問SpringCloud Config3344通過GitHub獲取配置信息

此時出現(xiàn)個問題,分布式配置的動態(tài)刷新問題:

Linux運維修改GitHub上的配置文件內(nèi)容做調(diào)整
刷新3344,發(fā)現(xiàn)ConfigServer配置中心立刻響應
刷新3355,發(fā)現(xiàn)ConfigClient客戶端沒有任何響應
3355沒有變化除非自己重啟或者重新加載,難到每次運維修改配置文件,客戶端都需要重啟?

4、Config客戶端之間動態(tài)刷新

避免每次更新配置都要重啟客戶端微服務3355

動態(tài)刷新步驟:

1.修改3355模塊

2.POM引入actuator監(jiān)控

3.修改YML,暴露監(jiān)控端口

#暴露監(jiān)控端點
management:
  endpoints:
    web:
      exposure:
        include: "*"

4.@RefreshScope業(yè)務Controller修改

5.此時在修改Github -> 訪問3344 -> 訪問3355;發(fā)現(xiàn)3355并沒有變化

6.需要運維人員發(fā)送post請求刷新3355必須是post請求,curl -X POST “http://localhost:3355/actuator/refresh”

這里采用的是手動刷新的方式,但是有多個微服務客戶端的時候呢?還采用手動刷新的方式?所以我們需要采用自動刷新的方式

7.成功實現(xiàn)了客戶端3355刷新到最新配置內(nèi)容,避免了重啟服務 如果有收獲?。?!

到此這篇關于SpringCloud的Config配置中心詳解的文章就介紹到這了,更多相關SpringCloud的Config內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論