欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Cloud Config與Bus整合實(shí)現(xiàn)微服務(wù)配置自動(dòng)刷新功能

 更新時(shí)間:2024年10月18日 15:01:26   作者:布說(shuō)在見(jiàn)  
通過(guò)整合SpringCloud Config與Spring Cloud Bus,實(shí)現(xiàn)了微服務(wù)配置的自動(dòng)刷新功能,這個(gè)機(jī)制允許一個(gè)微服務(wù)實(shí)例在配置更新時(shí)通過(guò)消息總線通知其他所有實(shí)例同步更新,從而保持配置的一致性并提升系統(tǒng)的運(yùn)維效率

Config與Bus整合自動(dòng)刷新

微服務(wù)A的所有實(shí)例都通過(guò)Spring Cloud Bus消息總線連接到了一起,每個(gè)實(shí)例都會(huì)從Config Server訂閱配置更新事件并獲取配置信息。當(dāng)其中一個(gè)微服務(wù)節(jié)點(diǎn)的/ous/refresh端點(diǎn)被請(qǐng)求時(shí),該實(shí)例就會(huì)向Spring Cloud Bus消息總線發(fā)送一個(gè)配置更新事件,其他實(shí)例通過(guò)Spring Cloud Bus消息總線獲得該事件后也會(huì)從Config Server獲取最新的配置信息并更新配置。

步驟:

1.安裝RabbitMQ并啟動(dòng)

2.添加依賴(lài)

3.修改配置文件

4.改造Config Client

5.測(cè)試運(yùn)行

步驟1:安裝RabbitMQ并啟動(dòng) RabbitMQ的安裝

1.下載erlang,原因在于RabbitMQ服務(wù)端代碼是使用并發(fā)式語(yǔ)言erlang編寫(xiě)的,下載地址:erlang,雙擊.eerlangxe文件進(jìn)行安裝就好,安裝完成之后創(chuàng)建一個(gè)名為ERLANG_HOME的環(huán)境變量,其值指向erlang的安裝目錄,同時(shí)將%ERLANG_HOME%\bin加入到Path中。

最后打開(kāi)cmd命令行,輸入erl,如果出現(xiàn)erlang的版本信息就表示erlang語(yǔ)言環(huán)境安裝成功

2.下載RabbitMQ,下載地址:RabbitMQ,同樣雙擊.exe進(jìn)行安裝就好(注意:默認(rèn)的安裝目錄是C:/Program Files/…,這個(gè)目錄中是存在空格符的,我們需要改變安裝目錄,貌似RabbitMQ安裝目錄中是不允許有空格的)。

3.安裝RabbitMQ-Plugins,這個(gè)相當(dāng)于是一個(gè)管理界面,方便我們?cè)跒g覽器界面查看RabbitMQ各個(gè)消息隊(duì)列以及exchange的工作情況,安裝方法是:打開(kāi)命令行cd進(jìn)入rabbitmq的sbin目錄,輸入:rabbitmq-plugins enable rabbitmq_management命令,稍等會(huì)會(huì)發(fā)現(xiàn)出現(xiàn)plugins安裝成功的提示,默認(rèn)是安裝6個(gè)插件。

4.插件安裝完之后,在瀏覽器輸入http://localhost:15672進(jìn)行驗(yàn)證,你會(huì)看到下面界面,輸入用戶(hù)名:guest,密碼:guest 就可以進(jìn)入管理界面。

步驟2:創(chuàng)建項(xiàng)目

Eureka Server,config-server,config-client

創(chuàng)建Eureka Server

1)使用Spring Initializr方式創(chuàng)建一個(gè)名稱(chēng)為eureka-server的Spring Boot項(xiàng)目,將Artifact命名為eureka-server,在pom.xml文件中添加Eureka Server依賴(lài)。

方便測(cè)試效果,新建一個(gè)eureka-server項(xiàng)目作為Config Server的注冊(cè)中心,
將eureka-server端口號(hào)設(shè)置為8761。

在加載不出來(lái)添加,要與springboot版本相符

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bushuo</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>
                spring-cloud-starter-netflix-eureka-server
            </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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2):添加Eureka的相關(guān)配置

在全局配置文件application.yml中添加Eureka的相關(guān)配置信息。

fetch-registry:
設(shè)置false 時(shí),該Eureka客戶(hù)端不會(huì)從Eureka Server獲取服務(wù)注冊(cè)表的信息。這意味著它不會(huì)嘗試?yán)∑渌?wù)的信息,通常這在自身就是Server的情況下使用。register-with-eureka:
設(shè)置為 false 時(shí),Eureka 客戶(hù)端不注冊(cè)到 Eureka Server。即不將自身信息注冊(cè)到服務(wù)注冊(cè)表。在 Eureka Server 節(jié)點(diǎn)配置中常用,因 Eureka Server 本身無(wú)需注冊(cè)到其他 Eureka Server,單實(shí)例時(shí)是。

3):在項(xiàng)目啟動(dòng)類(lèi)添加@EnableEurekaServer注解

在項(xiàng)目啟動(dòng)類(lèi)EurekaServerApplication上添加@EnableEurekaServer注解開(kāi)啟Eureka Server功能。

4)測(cè)試運(yùn)行 http://localhost:8761

創(chuàng)建config-server

在config-4下創(chuàng)建config-server,添加依賴(lài),添加配置,在啟動(dòng)類(lèi)添加注解

可以參考操作
依賴(lài)

配置bootstrap

添加啟動(dòng)類(lèi)注解

步驟3: 添加依賴(lài)

Config Server和Config Client與Spring Cloud Bus整合實(shí)現(xiàn)配置自動(dòng)刷新。改造Config Server與Config Client,在各自的pom.xml配置文件中添加spring-cloud-starter-bus-amqp依賴(lài)實(shí)現(xiàn)配置自動(dòng)更新與spring-boot-starter-actuator依賴(lài)監(jiān)控系統(tǒng)健康情況的工具。

        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

修改配置文件

改造Config Server和Config Client的配置文件,在配置文件中設(shè)置rabbitmq屬性的相關(guān)配置,包括主機(jī)地址、端口號(hào)、用戶(hù)名和密碼,如下所示。

spring:
  application:
    name: config-client
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/nobyebye/hello.git
          username: #自己的gitee用戶(hù)名
          password: #密碼
          label: master
  profiles:
    active: dev
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
server:
  port: 8001
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

步驟4:Config Client

config-client的bootstrap.yml

添加控制器

Config Client,在需要?jiǎng)討B(tài)刷新配置的類(lèi)ConfigClientApplication上加上注解@RefreshScope。

要在gitee的配置文件添加foo,不然沒(méi)有注解

步驟5:測(cè)試運(yùn)行

依次啟動(dòng)config-server、config-client項(xiàng)目。啟動(dòng)成功后,使用瀏覽器訪問(wèn)http://localhost:8002/foo

打開(kāi)cmd輸入命令:curl -X POST http://localhost:8769/actuator/bus-refresh。

實(shí)現(xiàn)Config與Bus整合自動(dòng)刷新后,系統(tǒng)能做到實(shí)時(shí)檢測(cè)配置變更并自動(dòng)應(yīng)用,無(wú)需額外的人工干預(yù),減少了系統(tǒng)中斷時(shí)間和重啟操作。這不僅提高了系統(tǒng)的穩(wěn)定性,還能顯著提升運(yùn)維和開(kāi)發(fā)的效率。

問(wèn)題一

idea運(yùn)行多個(gè)端口,沒(méi)有services的窗口,點(diǎn)擊edit Configurations

點(diǎn)擊Templates->+ springboot,apply,ok了

窗口出來(lái)了

問(wèn)題二

整合Bus自動(dòng)刷新改進(jìn)了之前的什么問(wèn)題?

手動(dòng)刷新繁瑣:以往配置更新后需要手動(dòng)重啟各服務(wù)實(shí)例,耗時(shí)且麻煩。自動(dòng)刷新可以省去這一過(guò)程,讓更新更簡(jiǎn)單快捷。

減少服務(wù)中斷:手動(dòng)重啟會(huì)導(dǎo)致短暫的服務(wù)中斷。自動(dòng)刷新避免了重啟,提升了服務(wù)的連續(xù)性和可用性。

配置一致性:手動(dòng)更新可能導(dǎo)致不同實(shí)例間配置不一致,帶來(lái)同步問(wèn)題。自動(dòng)刷新確保所有實(shí)例同步更新,保持一致性。

降低運(yùn)維復(fù)雜性:自動(dòng)刷新減少了手動(dòng)操作,降低運(yùn)維負(fù)擔(dān),避免人為失誤。

提高實(shí)時(shí)性:配置更新可實(shí)時(shí)應(yīng)用,服務(wù)能更快地響應(yīng)變化,提高了系統(tǒng)的適應(yīng)性和靈活性。

總結(jié)

通過(guò)整合Spring Cloud Config與Bus實(shí)現(xiàn)配置自動(dòng)刷新,可以在無(wú)需重啟服務(wù)的情況下更新微服務(wù)配置。該機(jī)制減少了運(yùn)維干預(yù),提升了系統(tǒng)的穩(wěn)定性和運(yùn)維效率,尤其在快速變更的環(huán)境中十分實(shí)用。

到此這篇關(guān)于Spring Cloud Config與Bus整合實(shí)現(xiàn)微服務(wù)配置自動(dòng)刷新的文章就介紹到這了,更多相關(guān)Spring Cloud Config微服務(wù)配置自動(dòng)刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論