Spring Cloud Config實現集群配置中心

来源:https://www.cnblogs.com/gdjlc/archive/2019/12/01/11968129.html
-Advertisement-
Play Games

Spring Cloud Config為分散式系統提供了配置伺服器和配置客戶端,可以管理集群中的配置文件。 使用Git、SVN等版本管理系統存放配置文件,配置伺服器會到版本管理系統獲取配置,集群中的配置客戶端再到配置伺服器中獲取配置。 ...


Spring Cloud Config為分散式系統提供了配置伺服器和配置客戶端,可以管理集群中的配置文件。
使用Git、SVN等版本管理系統存放配置文件,配置伺服器會到版本管理系統獲取配置,集群中的配置客戶端再到配置伺服器中獲取配置。

開發工具:IntelliJ IDEA 2019.2.2

一、創建配置伺服器

1、SVN伺服器添加項目和配置文件

 config-client-dev.yml內容:

server:
  port: 8092
test:
  user:
    name: aa

config-client-test.yml

server:
  port: 8093
test:
  user:
    name: bb

2、創建項目

IDEA中創建一個新的SpringBoot項目,名稱為“spring-config-server”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Spring Cloud Config -> Config Server。
pom.xml會引入spring-cloud-config-server依賴項,再在pom.xml中加入org.tmatesoft.svnkit依賴項,pom.xml完整內容如下:

<?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.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.10.1</version>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

3、修改配置application.yml

spring-cloud-config-server提供了4種配置,可以通過不同名字來激活:
(1)git:預設值,表示去Git倉庫讀取配置文件;
(2)subversion:表示去SVN倉庫讀取配置文件;
(3)native:表示去本地文件系統讀取配置文件;
(4)vault:表示去Vault(一種資源控制工具)中讀取配置文件;

server:
  port: 8091
spring:
  application:
    name: config-server
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://localhost/svn/test-project
          username: abc
          password: 123456
        default-label: default-config

4、修改啟動類代碼

增加註解@EnableConfigServer

package com.example.springconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringConfigServerApplication.class, args);
    }

}
View Code

可以使用Config Server的端點獲取配置文件的內容,端點與配置文件的映射規則如下:

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

{application} 是應用名稱,對應配置文件的名稱部分,本例是config-client。
{profile} 是配置文件的版本,本例是dev和test。
{label} 表示分支,如果是git則預設是master分支。

啟動服務,瀏覽器訪問(把下麵test換為dev,結果類似)下麵地址,分別輸出如下:
http://localhost:8091/config-client/test

{"name":"config-client","profiles":["test"],"label":null,"version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client/test/default-config

{"name":"config-client","profiles":["test"],"label":"default-config","version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client-test.yml

server:
  port: 8093
test:
  user:
    name: bb

http://localhost:8091/default-config/config-client-test.yml

server:
  port: 8093
test:
  user:
    name: bb

二、配置客戶端讀取SVN配置

1、創建項目

IDEA中創建一個新的SpringBoot項目,名稱為“spring-config-client”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Web -> Spring Web,Spring Cloud Config -> Config Client。
pom.xml會引入spring-boot-starter-web和spring-cloud-starter-config依賴項,pom.xml完整內容如下:

<?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.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

    <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.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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

2、修改啟動類代碼

增加測試方法

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringConfigClientApplication {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(SpringConfigClientApplication.class, args);
    }

    @RequestMapping("/")
    public String home(){
        return env.getProperty("test.user.name");
    }
}

3、添加配置bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8091
      profile: dev

設置了應用名稱config-client,使用spring.cloud.config.uri來設置配置伺服器的地址,使用spring.cloud.config.profile來讀取指定的配置。最終,配置客戶端會到SVN伺服器的test-project/default-config目錄下讀取config-client-dev.yml(.properties)。

啟動服務,瀏覽器訪問:http://localhost:8092/(這裡埠8092在config-client-dev.yml中已指定),頁面輸出:aa

也可以使用spring.cloud.config.name代替spring.application.name,結果一樣。

spring:
  cloud:
    config:
      uri: http://localhost:8091
      profile: dev
      name: config-client

如果spring.cloud.config.name和spring.application.name都不提供,則預設讀取application-dev.yml。
在SVN的test-project/default-config目錄下新增文件application-dev.yml,內容 

server:
  port: 8092
test:
  user:
    name: cc

啟動服務,瀏覽器訪問:http://localhost:8092/,頁面輸出:cc

可以設置spring.client.config.label來覆蓋伺服器的default-lable屬性,另外上面profile也可改為下麵寫法。

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8091
      lable: default-config
      name: config-client
  profiles:
    active: dev

三、使用/refresh端點手動刷新配置

1、在上面配置客戶端的pom.xml中添加依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2、application.yml添加配置

management:
  endpoints:
    web:
      exposure:
        include: "*"

3、在Controller上添加註解@RefreshScope

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication {

   @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(SpringConfigClientApplication.class, args);
    }

    @RequestMapping("/")
    public String home(){
        return env.getProperty("test.user.name");
    }
}
View Code

4、修改SVN伺服器上config-client-dev.yml內容

把name的值由aa修改為aa11,提交SVN修改。

5、/refresh只支持POST請求,發送POST請求到http://localhost:8092/actuator/refresh

使用Postman發送POST請求,如果SVN沒有修改,返回[],如果有修改,返回結果如下:

刷新瀏覽器地址:http://localhost:8092/,結果已由aa,變成了aa11。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 技術:Java,Struts,Spring,Hibernate資料庫: MySQLweb伺服器:tomcat集成開發工具: My Eclipse網上手機銷售系統主要實現以下功能。本系統模塊如下:1,前臺系統功能模塊 |--商品的展示模塊 |--新品上架 |--特價商品 |--銷售排行 |--商品的查 ...
  • 安裝grafana,官網提供了ubuntu的安裝包,直接進行安裝 wget https://dl.grafana.com/oss/release/grafana_6.5.1_amd64.deb dpkg -i grafana_6.5.1_amd64.deb update-rc.d grafana-s ...
  • Proc 是Oracle提供的一種資料庫操作的AP。它是基於ESql技術的,需要預編譯後才可以變成普通c代碼,非常不直觀,使用起來不太方便,閱讀也存在困難。 因為這些問題導致程式員平時開發中會出現一些Proc操作存在效率低下的情況,本文介紹一些Proc一些編譯經驗,希望能給大家提供參考。 下麵以一個 ...
  • 1. 單例模式 什麼是單例模式?簡言之就是確保定義為單例模式的類在程式中有且只有一個實例。單例模式的特點: 1. 只有一個實例 (只能有一個對象被創建) 2. 自我實例化(類構造器私有) 3. 對外提供獲取實例的靜態方法 2.單例模式的實現 常見的單例模式實現方式有五種: 2.1. 懶漢式 懶漢式( ...
  • 最近開始使用IDEA,突然發現的比較的實用的幾個快捷鍵 這些快捷鍵用的好的話真的可以提升很多效率 還有一些比較簡單的快捷鍵,相信大家都會 Ctrl+X:剪切 Ctrl+C:複製 Ctrl+V:粘貼 Ctrl+Shift+上下箭頭:註意它和圖片裡面移動代碼快捷鍵相識,自己體會 ...
  • 有興趣的可以看看註釋裡面的題目要求,自己寫寫或者看看下麵的用別的方式完成目標。 ...
  • 基礎入門知識(一) 一、java技術的分類 java按照技術標準和應用場景的不同分為三類,分別是JAVASE、JAVAEE、JAVAME JAVASE : 平臺標準版,用於開發部署桌面,伺服器以及嵌入式設備和實時環境中的java程式。 JAVAEE : 平臺企業版,開發便於組轉,可擴展,健壯,安全的 ...
  • 新聞 "擁抱可空引用類型" "介紹Orleans 3.0" 視頻及幻燈片 "組合的力量" "關於.NET:探索新的用於.NET的Azure .NET SDK" ".NET設計審查:GitHub快速審查" FableConf 2019 "OSS的樂趣" "走進編譯器與工具之旅" "使用響應式MVU釋放 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...