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

Spring Cloud中配置客戶端示例詳解

 更新時間:2023年09月19日 12:10:45   作者:進擊的猿小白  
這篇文章主要介紹了Spring Cloud中配置客戶端的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Environment

Environment : PropertySources = 1:1

PropertySources : PropertySource = 1:N

[0] PropertySource (Map)

spring.application.name = spring-cloud-config-client

[1] PropertySource (Map)

spring.application.name = spring-cloud-config-client-demo

Spring Boot 配置文件

application.properties 或 application.xml

加載器: PropertiesPropertySourceLoader

application.yml 或者 application.yaml

加載器: YamlPropertySourceLoader

Environment 端點

請求 URI : /env

數(shù)據(jù)來源: EnvironmentEndpoint

Controller 來源: EnvironmentMvcEndpoint

Bootstrap 配置

參考 BootstrapApplicationListener 實現(xiàn)

注:程序啟動參數(shù)的加載邏輯:

SpringApplication#configurePropertySources()

Bootstrap 配置文件

String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");

spring.cloud.bootstrap.name 存在時,使用該配置項,否則,使用 "bootstrap" 作為默認

## application.properties
## 通過調(diào)整 spring.cloud.bootstrap.enabled = false,嘗試關(guān)閉 bootstrap 上下文
## 實際測試結(jié)果,沒有效果
spring.cloud.bootstrap.enabled = false

注意: BootstrapApplicationListener 加載實際早于 ConfigFileApplicationListener

原因是:

ConfigFileApplicationListener 的 Order = Ordered.HIGHEST_PRECEDENCE + 10(第十一位)

BootstrapApplicationListener 的 Order = Ordered.HIGHEST_PRECEDENCE + 5(第六位)

如果需要調(diào)整 控制 Bootstrap 上下文行為配置,需要更高優(yōu)先級,也就是說 Order 需要 < Ordered.HIGHEST_PRECEDENCE + 5 (越小越優(yōu)先),比如使用程序啟動參數(shù):

--spring.cloud.bootstrap.enabld = true

調(diào)整 Bootstrap 配置

調(diào)整 Bootstrap 配置文件名稱

調(diào)整程序啟動參數(shù)

--spring.cloud.bootstrap.name=spring-cloud

bootstrap 配置文件名稱發(fā)生了改變"spring-cloud",現(xiàn)有三個文件:

application.properties

spring.application.name = spring-cloud-config-client

bootstrap.properties

spring.application.name = spring-cloud-config-client-demo

spring-cloud.properties

spring.application.name = spring-cloud

運行結(jié)果(部分):

"applicationConfig: [classpath:/application.properties]": {
    "spring.cloud.bootstrap.enabled": "false",
    "endpoints.env.sensitive": "false",
    "spring.application.name": "spring-cloud-config-client"
  },
  ...
  "applicationConfig: [classpath:/spring-cloud.properties]": {
    "spring.application.name": "spring-cloud-config-client"
  }

調(diào)整 Bootstrap 配置文件路徑

保留 Bootstrap 配置文件名稱 程序啟動參數(shù):

--spring.cloud.bootstrap.name=spring-cloud

調(diào)整 Bootstrap 配置文件路徑 程序啟動參數(shù):

--spring.cloud.bootstrap.location=config

現(xiàn)有四個文件:

application.properties

spring.application.name = spring-cloud-config-client

bootstrap.properties

spring.application.name = spring-cloud-config-client-demo

spring-cloud.properties

spring.application.name = spring-cloud

config/spring-cloud.properties

spring.application.name = spring-cloud-2

實際結(jié)果:

  "applicationConfig: [classpath:/application.properties]": {
    "spring.cloud.bootstrap.enabled": "false",
    "endpoints.env.sensitive": "false",
    "spring.application.name": "spring-cloud-config-client"
  },
  ...
  "applicationConfig: [classpath:/config/spring-cloud.properties]": {
    "spring.application.name": "spring-cloud-config-client"
  },
  "applicationConfig: [classpath:/spring-cloud.properties]": {
    "spring.application.name": "spring-cloud-config-client"
  },

覆蓋遠程配置屬性

默認情況,Spring Cloud 是允許覆蓋的, spring.cloud.config.allowOverride=true

通過程序啟動參數(shù),調(diào)整這個值為"false"

--spring.cloud.config.allowOverride=false

啟動后,重新Postman 發(fā)送 POST 請求,調(diào)整 spring.application.name 值為 "spring-cloud-new"

注意官方文檔的說明:the remote property source has to grant it permission by setting spring.cloud.config.allowOverride=true (it doesn’t work to set this locally).

自定義 Bootstrap 配置

  • 創(chuàng)建 META-INF/spring.factories 文件(類似于 Spring Boot 自定義 Starter)
  • 自定義 Bootstrap 配置 Configuration
 
?import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
?
import java.util.HashMap;
import java.util.Map;
?
/**
 * Bootstrap 配置 Bean
 *
 * 
 * @since Configuration
 */
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {
?
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        // 從 ConfigurableApplicationContext 獲取 ConfigurableEnvironment 實例
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        // 獲取 PropertySources
        MutablePropertySources propertySources = environment.getPropertySources();
        // 定義一個新的 PropertySource,并且放置在首位
        propertySources.addFirst(createPropertySource());
?
    }
?
    private PropertySource createPropertySource() {
?
        Map<String, Object> source = new HashMap<>();
?
        source.put("name", "小馬哥");
?
        PropertySource propertySource = new MapPropertySource("my-property-source", source);
?
        return propertySource;
?
    }
}

配置 META-INF/spring.factories 文件,關(guān)聯(lián)Key org.springframework.cloud.bootstrap.BootstrapConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration= \
com.segmentfault.springcloudlesson2.boostrap.MyConfiguration

自定義 Bootstrap 配置屬性源

實現(xiàn) PropertySourceLocator

 
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;
?
import java.util.HashMap;
import java.util.Map;
?
/**
 * 自定義 {@link PropertySourceLocator} 實現(xiàn)
 *
 * @since PropertySourceLocator
 */
public class MyPropertySourceLocator implements PropertySourceLocator {
?
    @Override
    public PropertySource<?> locate(Environment environment) {
?
        if (environment instanceof ConfigurableEnvironment) {
?
            ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
?
            // 獲取 PropertySources
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            // 定義一個新的 PropertySource,并且放置在首位
            propertySources.addFirst(createPropertySource());
?
        }
        return null;
    }
?
    private PropertySource createPropertySource() {
?
        Map<String, Object> source = new HashMap<>();
?
        source.put("spring.application.name", "小馬哥的 Spring Cloud 程序");
        // 設(shè)置名稱和來源
        PropertySource propertySource = new MapPropertySource("over-bootstrap-property-source", source);
?
        return propertySource;
?
    }
}

配置 META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration= \
com.segmentfault.springcloudlesson2.boostrap.MyConfiguration,\
com.segmentfault.springcloudlesson2.boostrap.MyPropertySourceLocator

到此這篇關(guān)于Spring Cloud 之配置客戶端的文章就介紹到這了,更多相關(guān)Spring Cloud 配置客戶端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring通過jdbc連接數(shù)據(jù)庫

    spring通過jdbc連接數(shù)據(jù)庫

    這篇文章主要為大家詳細介紹了spring通過jdbc連接數(shù)據(jù)庫的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題

    解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題

    這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java編寫日志手機號脫敏工具類

    Java編寫日志手機號脫敏工具類

    在開發(fā)過程中,很容易將用戶敏感信息,例如手機號碼、身份證等,打印在日志平臺,本文將利用Java編寫一個日志手機號脫敏工具類,感興趣的可以了解下
    2024-12-12
  • Java生成動態(tài)版驗證碼的方法實例

    Java生成動態(tài)版驗證碼的方法實例

    這篇文章主要給大家介紹了利用Java生成動態(tài)版驗證碼的方法實例,本文生成的是GIF格式 + 干擾元素,讓驗證碼破解難度又上了一個層次,文中給出了詳細的示例代碼,并在文末給出了完整的實例代碼供大家下載學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-04-04
  • Spring與Redis集成的正確方式流程詳解

    Spring與Redis集成的正確方式流程詳解

    這篇文章主要為大家介紹了Spring與Redis集成的正確方式流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • SpringBoot使用ip2region獲取地理位置信息的方法

    SpringBoot使用ip2region獲取地理位置信息的方法

    這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 基于Java的度分秒坐標轉(zhuǎn)純經(jīng)緯度坐標的漂亮國基地信息管理的方法

    基于Java的度分秒坐標轉(zhuǎn)純經(jīng)緯度坐標的漂亮國基地信息管理的方法

    本文以java語言為例,詳細介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅實的基礎(chǔ),首先介紹如何對數(shù)據(jù)進行去重處理,然后介紹在java當中如何進行度分秒位置的轉(zhuǎn)換,最后結(jié)合實現(xiàn)原型進行詳細的說明,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • 詳解java jinfo命令

    詳解java jinfo命令

    jinfo是jdk自帶的命令,用來查看jvm的配置參數(shù).通常會先使用jps查看java進程的id,然后使用jinfo查看指定pid的jvm信息,需要的朋友可以參考下
    2021-06-06
  • Springboot POI導(dǎo)出Excel(瀏覽器)

    Springboot POI導(dǎo)出Excel(瀏覽器)

    這篇文章主要為大家詳細介紹了Springboot POI導(dǎo)出Excel,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java將對象寫入文件讀出_序列化與反序列化的實例

    Java將對象寫入文件讀出_序列化與反序列化的實例

    下面小編就為大家?guī)硪黄狫ava將對象寫入文件讀出_序列化與反序列化的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論