最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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}");

當(dāng) 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)文章

  • Java調(diào)用IK分詞器進行分詞方式,封裝工具類

    Java調(diào)用IK分詞器進行分詞方式,封裝工具類

    這篇文章主要介紹了Java調(diào)用IK分詞器進行分詞方式,封裝工具類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • maven三個常用的插件使用介紹

    maven三個常用的插件使用介紹

    大家好,本篇文章主要講的是maven三個常用的插件使用介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 解決版本不兼容Jar包沖突問題

    解決版本不兼容Jar包沖突問題

    在和三方對接的過程中,我們可能會不斷引入一些三方j(luò)ar包,但這個時候就有可能出現(xiàn)一個項目需要依賴兩個版本不同且功能不兼容的jar包,本文主要介紹了解決版本不兼容Jar包沖突問題,感興趣的可以了解一下
    2023-10-10
  • springboot定時任務(wù)SchedulingConfigurer異步多線程實現(xiàn)方式

    springboot定時任務(wù)SchedulingConfigurer異步多線程實現(xiàn)方式

    這篇文章主要介紹了springboot定時任務(wù)SchedulingConfigurer異步多線程實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 深入解析Java編程中的StringBuffer與StringBuider

    深入解析Java編程中的StringBuffer與StringBuider

    這篇文章主要介紹了Java編程中的StringBuffer與StringBuider,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • Java VisualVM監(jiān)控遠程JVM(詳解)

    Java VisualVM監(jiān)控遠程JVM(詳解)

    下面小編就為大家?guī)硪黄狫ava VisualVM監(jiān)控遠程JVM(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Netty分布式pipeline管道傳播事件的邏輯總結(jié)分析

    Netty分布式pipeline管道傳播事件的邏輯總結(jié)分析

    這篇文章主要為大家介紹了Netty分布式pipeline管道傳播事件總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java文件上傳與文件下載實現(xiàn)方法詳解

    Java文件上傳與文件下載實現(xiàn)方法詳解

    這篇文章主要介紹了Java文件上傳與文件下載實現(xiàn)方法,結(jié)合實例形式詳細分析了Java文件上傳與文件下載相關(guān)操作原理、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-02-02
  • 最新評論

    庆城县| 卢湾区| 乐至县| 商城县| 安国市| 民丰县| 乌拉特前旗| 尉氏县| 阳江市| 渑池县| 礼泉县| 东城区| 屏南县| 汕尾市| 平南县| 咸宁市| 广丰县| 乌鲁木齐市| 卫辉市| 瑞丽市| 伽师县| 昂仁县| 保亭| 讷河市| 长泰县| 温宿县| 黔江区| 儋州市| 吴桥县| 内江市| 绥江县| 新民市| 马公市| 罗甸县| 上杭县| 吉林省| 青岛市| 雷山县| 峨山| 英山县| 康马县|