Spring Cloud中配置客户端示例详解

 更新时间:2023年09月19日 12:10:45   作者:进击的猿小白  
这篇文章主要介绍了Spring Cloud中配置客户端的相关知识,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

数据来源: EnvironmentEndpoint

Controller 来源: EnvironmentMvcEndpoint

Bootstrap 配置

参考 BootstrapApplicationListener 实现

注:程序启动参数的加载逻辑:

SpringApplication#configurePropertySources()

Bootstrap 配置文件

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

spring.cloud.bootstrap.name 存在时,使用该配置项,否则,使用 "bootstrap" 作为默认

## application.properties
## 通过调整 spring.cloud.bootstrap.enabled = false,尝试关闭 bootstrap 上下文
## 实际测试结果,没有效果
spring.cloud.bootstrap.enabled = false

注意: BootstrapApplicationListener 加载实际早于 ConfigFileApplicationListener

原因是:

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

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

如果需要调整 控制 Bootstrap 上下文行为配置,需要更高优先级,也就是说 Order 需要 < Ordered.HIGHEST_PRECEDENCE + 5 (越小越优先),比如使用程序启动参数:

--spring.cloud.bootstrap.enabld = true

调整 Bootstrap 配置

调整 Bootstrap 配置文件名称

调整程序启动参数

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

bootstrap 配置文件名称发生了改变"spring-cloud",现有三个文件:

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

运行结果(部分):

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

调整 Bootstrap 配置文件路径

保留 Bootstrap 配置文件名称 程序启动参数:

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

调整 Bootstrap 配置文件路径 程序启动参数:

--spring.cloud.bootstrap.location=config

现有四个文件:

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

实际结果:

  "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

通过程序启动参数,调整这个值为"false"

--spring.cloud.config.allowOverride=false

启动后,重新Postman 发送 POST 请求,调整 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 配置

  • 创建 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 文件,关联Key org.springframework.cloud.bootstrap.BootstrapConfiguration

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

自定义 Bootstrap 配置属性源

实现 PropertySourceLocator

 
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;
​
import java.util.HashMap;
import java.util.Map;
​
/**
 * 自定义 {@link PropertySourceLocator} 实现
 *
 * @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 程序");
        // 设置名称和来源
        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

到此这篇关于Spring Cloud 之配置客户端的文章就介绍到这了,更多相关Spring Cloud 配置客户端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • IDEA 2022 CPU占用100%的问题及解决方法

    IDEA 2022 CPU占用100%的问题及解决方法

    这篇文章主要介绍了IDEA 2022 CPU占用100%问题及解决方法,其实解决方法很简单,只需要禁用三个插件然后重启idea即可成功解决,需要的朋友可以参考下本文
    2022-08-08
  • Java Swing实现的定制TextField功能示例

    Java Swing实现的定制TextField功能示例

    这篇文章主要介绍了Java Swing实现的定制TextField功能,结合实例形式分析了java swing组件TextField相关属性功能与设置操作技巧,需要的朋友可以参考下
    2018-01-01
  • Struts2实现单文件或多文件上传功能

    Struts2实现单文件或多文件上传功能

    这篇文章主要为大家详细介绍了Struts2实现单文件或多文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • springboot如何配置上传文件的maxRequestSize

    springboot如何配置上传文件的maxRequestSize

    这篇文章主要介绍了springboot如何配置上传文件的maxRequestSize,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 基于Java实现一个复杂关系表达式过滤器

    基于Java实现一个复杂关系表达式过滤器

    这篇文章主要为大家详细介绍了如何基于Java实现一个复杂关系表达式过滤器。文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-07-07
  • 利用Java实现天气预报播报功能

    利用Java实现天气预报播报功能

    这篇文章主要为大家介绍了如何利用Java语言实现天气预报播报功能,文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下
    2022-06-06
  • java 使用过滤器实现登录拦截处理

    java 使用过滤器实现登录拦截处理

    这篇文章主要介绍了java 使用过滤器实现登录拦截处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java 生产者/消费者问题实例详解

    Java 生产者/消费者问题实例详解

    这篇文章主要实例分析了java中生产者消费者问题的方法,需要的朋友可以可以参考下
    2017-04-04
  • MyBatis关于二级缓存问题

    MyBatis关于二级缓存问题

    本篇文章主要介绍了MyBatis关于二级缓存问题,二级缓存是Mapper级别的缓存,多个sqlSession操作同一个Mapper,其二级缓存是可以共享的。
    2017-03-03
  • Java如何将任意类型的Object对象转换为相应的实体对象

    Java如何将任意类型的Object对象转换为相应的实体对象

    这篇文章主要介绍了Java如何将任意类型的Object对象转换为相应的实体对象问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01

最新评论