iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring远程加载配置如何实现
  • 503
分享到

Spring远程加载配置如何实现

2023-07-05 17:07:42 503人浏览 安东尼
摘要

本篇内容主要讲解“spring远程加载配置如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring远程加载配置如何实现”吧!前要本文以携程的Apollo和阿里的Nacos为例。pom中

本篇内容主要讲解“spring远程加载配置如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring远程加载配置如何实现”吧!

前要

本文以携程的Apollo和阿里的Nacos为例。

pom中引入一下依赖:

        <dependency>            <groupId>com.ctrip.framework.apollo</groupId>            <artifactId>apollo-client</artifactId>            <version>2.0.1</version>        </dependency>        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>            <version>2021.1</version>        </dependency>

不管是Apollo还是Nacos,实现从远程加载配置都是通过ConfigurableEnvironmentPropertySource完成的,步骤如下:

  • 远程拉取配置,生成PropertySource

  • ConfigurableEnvironment获取聚合类 MutablePropertySources propertySources = ConfigurableEnvironment#getPropertySources();

  • 将拉取的PropertySource添加到从ConfigurableEnvironment获取的聚合类MutablePropertySources#add...(PropertySource<?> propertySource)

至于这个过程是怎么触发和运行的,要看具体实现。

  • 在apollo-client中,使用BeanFactoryPostProcessor。

  • 在spring-cloud-starter-alibaba-nacos-config中,由于 cloud-nacos实现了spring cloud config规范(处于org.springframework.cloud.bootstrap.config包下),nacos实现该规范即可,即实现spring cloud 的PropertySourceLocator接口。

Apollo

关注PropertySourcesProcessor ,该类为一个BeanFactoryPostProcessor,同时为了获取ConfigurableEnvironment,该类实现了EnvironmentAware回调接口。该类何时被加入spring容器?是通过@EnableApolloConfig@Import注解的类ApolloConfigReGIStrar来加入,常规套路。

public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware,    ApplicationEventPublisherAware, PriorityOrdered {// aware回调接口设置private ConfigurableEnvironment environment;@Override  public void setEnvironment(Environment environment) {    //it is safe enough to cast as all known environment is derived from ConfigurableEnvironment    this.environment = (ConfigurableEnvironment) environment;  }@Override  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {  // 获取配置    this.configUtil = ApolloInjector.getInstance(ConfigUtil.class);    // 从远程获取PropertySource    initializePropertySources();    // 为每个ConfigPropertySource注册ConfiGChangeEvent监听器    // 监听器监听到ConfigChangeEvent后publish一个ApolloConfigChangeEvent    // 等于将apollo自定义的ConfigChangeEvent事件机制转化为了spring的ApolloConfigChangeEvent事件    initializeAutoUpdatePropertiesFeature(beanFactory);  }private void initializePropertySources() {// 聚合类,该类也是一个PropertySource,代理了一堆PropertySource// 该类中有一个 Set<PropertySource<?>> 字段CompositePropertySource composite = new ...;...// 从 远程 或 本地缓存 获取配置Config config = ConfigService.getConfig(namespace);// 适配Config到PropertySource,并加入聚合类composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));// 添加到ConfigurableEnvironmentenvironment.getPropertySources().addFirst(composite);} private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFactory beanFactory) {    if (!AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) {      return;    }// 定义监听器,监听器监听到ConfigChangeEvent后发布ApolloConfigChangeEvent    ConfigChangeListener configChangeEventPublisher = changeEvent ->        applicationEventPublisher.publishEvent(new ApolloConfigChangeEvent(changeEvent));// 注册监听器到每个PropertySource    List<ConfigPropertySource> configPropertySources = configPropertySourceFactory.getAllConfigPropertySources();    for (ConfigPropertySource configPropertySource : configPropertySources) {      configPropertySource.addChangeListener(configChangeEventPublisher);   }  }...}

从上面可知初始化时会从ConfigService远程拉取配置,并保存到内部缓存。而后续远程配置中心配置发生变化时本地会拉去最新配置并发布事件,PropertySource根据事件进行更新。

无论是开始从远程拉取配置初始化,还是后续远程配置更新,最终都是通过RemoteConfigRepositoryHttp形式定时获取配置:

public class RemoteConfigRepository extends AbstractConfigRepository implements ConfigRepository{  public RemoteConfigRepository(String namespace) {  ...  // 定时拉取this.schedulePeriodicRefresh();// 长轮询this.scheduleLongPollingRefresh();...  }  private void schedulePeriodicRefresh() {    // 定时线程池    m_executorService.scheduleAtFixedRate(        new Runnable() {          @Override          public void run() {          // 调用父抽象类trySync()          // trySync()调用模版方法sync()            trySync();          }        }, m_configUtil.getRefreshInterval(), m_configUtil.getRefreshInterval(),        m_configUtil.getRefreshIntervalTimeUnit());  }  @Override  protected synchronized void sync() {  // 事务    Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "syncRemoteConfig");    try {      ApolloConfig previous = m_configCache.get();      // http远程拉取配置      ApolloConfig current = loadApolloConfig();      // reference equals means HTTP 304      if (previous != current) {        logger.debug("Remote Config refreshed!");        // 设置缓存        m_configCache.set(current);        // 发布事件,该方法在父抽象类中        this.fireRepositoryChange(m_namespace, this.getConfig());      }      if (current != null) {        Tracer.logEvent(String.fORMat("Apollo.Client.Configs.%s", current.getNamespaceName()),            current.getReleaseKey());      }      transaction.setStatus(Transaction.SUCCESS);    } catch (Throwable ex) {      transaction.setStatus(ex);      throw ex;    } finally {      transaction.complete();    }    ...  }

可以看到,在构造方法中,就执行了 3 个本地方法,其中就包括定时刷新和长轮询刷新。这两个功能在 apollo 的 GitHub 文档中也有介绍:

  • 客户端和服务端保持了一个长连接,从而能第一时间获得配置更新的推送。

  • 客户端还会定时从Apollo配置中心服务端拉取应用的最新配置。

  • 这是一个fallback机制,为了防止推送机制失效导致配置不更新。

  • 客户端定时拉取会上报本地版本,所以一般情况下,对于定时拉取的操作,服务端都会返回304 - Not Modified。

  • 定时频率默认为每5分钟拉取一次,客户端也可以通过在运行时指定System Property: apollo.refreshInterval来覆盖,单位为分钟。

所以,长连接是更新配置的主要手段,然后用定时任务辅助长连接,防止长连接失败。

org.springframework.cloud.bootstrap.config

nacos实现了spring cloud config规范,规范代码的Maven坐标如下:

    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-context</artifactId>      <version>...</version>      <scope>compile</scope>    </dependency>

这里介绍规范内容,nacos的实现略。

PropertySource

PropertySource用于存储k-v键值对,远程或本地的配置最终都转化为PropertySource,放入ConfigurableEnvironment中,通常EnumerablePropertySource中会代理一个PropertySource的list。

Spring远程加载配置如何实现

PropertySourceLocator

规范接口主要为PropertySourceLocator接口,该接口用于定位PropertySource,注释如下:

Strategy for locating (possibly remote) property sources for the Environment. Implementations should not fail unless they intend to prevent the application from starting.

public interface PropertySourceLocator {// 实现类实现该方法PropertySource<?> locate(Environment environment);default Collection<PropertySource<?>> locateCollection(Environment environment) {return locateCollection(this, environment);}static Collection<PropertySource<?>> locateCollection(PropertySourceLocator locator, Environment environment) {// 调用实现类PropertySource<?> propertySource = locator.locate(environment);if (propertySource == null) {return Collections.emptyList();}// 如果该PropertySource是代理了list的CompositePropertySource,提取全部if (CompositePropertySource.class.isInstance(propertySource)) {Collection<PropertySource<?>> sources = ((CompositePropertySource) propertySource).getPropertySources();List<PropertySource<?>> filteredSources = new ArrayList<>();for (PropertySource<?> p : sources) {if (p != null) {filteredSources.add(p);}}return filteredSources;}else {return Arrays.asList(propertySource);}}}

PropertySourceBootstrapConfiguration

调用PropertySourceLocator接口将PropertySource加入ConfigurableEnvironment中。

@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)public class PropertySourceBootstrapConfigurationimplements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {@Autowired(required = false)private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();public void setPropertySourceLocators(Collection<PropertySourceLocator> propertySourceLocators) {this.propertySourceLocators = new ArrayList<>(propertySourceLocators);}@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {List<PropertySource<?>> composite = new ArrayList<>();// 排序AnnotationAwareOrderComparator.sort(this.propertySourceLocators);boolean empty = true;// applicationContext由回调接口提供ConfigurableEnvironment environment = applicationContext.getEnvironment();for (PropertySourceLocator locator : this.propertySourceLocators) {// 调用PropertySourceLocatorCollection<PropertySource<?>> source = locator.locateCollection(environment);...for (PropertySource<?> p : source) {// 是否代理了PropertySource的list做分类if (p instanceof EnumerablePropertySource) {EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) p;sourceList.add(new BootstrapPropertySource<>(enumerable));}else {sourceList.add(new SimpleBootstrapPropertySource(p));}}composite.addAll(sourceList);empty = false;}if (!empty) {// 获取 ConfigurableEnvironment中的MutablePropertySourcesMutablePropertySources propertySources = environment.getPropertySources();...// 执行插入到ConfigurableEnvironment的MutablePropertySourcesinsertPropertySources(propertySources, composite);...}}}

到此,相信大家对“Spring远程加载配置如何实现”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Spring远程加载配置如何实现

本文链接: https://www.lsjlt.com/news/353563.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring远程加载配置如何实现
    本篇内容主要讲解“Spring远程加载配置如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring远程加载配置如何实现”吧!前要本文以携程的Apollo和阿里的Nacos为例。pom中...
    99+
    2023-07-05
  • Spring Cloud Alibaba Nacos Config加载如何配置
    本篇内容介绍了“Spring Cloud Alibaba Nacos Config加载如何配置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些...
    99+
    2023-07-02
  • yml配置文件与properties如何利用Spring Boot实现加载
    本篇文章给大家分享的是有关yml配置文件与properties如何利用Spring Boot实现加载,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、系统启动后注入配置pack...
    99+
    2023-05-31
    spring boot properties yml
  • 如何实现SpringBoot加载配置文件
    这篇文章主要为大家展示了“如何实现SpringBoot加载配置文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何实现SpringBoot加载配置文件”这篇文章吧。一、简介在实际的项目开发过程...
    99+
    2023-06-29
  • 基于Python如何实现配置热加载
    本篇内容介绍了“基于Python如何实现配置热加载”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!背景由于最近工作需求,需要在已有项目添加一个...
    99+
    2023-07-02
  • 使用Spring如何实现加载Bean
    本篇文章给大家分享的是有关使用Spring如何实现加载Bean,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1 定义bean的方式常见的定义Bean的方式有:通过xml的方式,...
    99+
    2023-05-31
    spring bea bean
  • Spring bean加载控制如何实现
    这篇文章主要讲解了“Spring bean加载控制如何实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring bean加载控制如何实现”吧!1. Controll...
    99+
    2023-07-04
  • MyBatis实现配置加载的步骤
    目录前言正文总结前言 在原始MyBatis的使用中,使用MyBatis时会先读取配置文件mybatis-config.xml为字符流或者字节流,然后通过SqlSessionFacto...
    99+
    2023-05-19
    MyBatis 配置加载
  • properties配置文件如何利用Java实现加载
    properties配置文件如何利用Java实现加载?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。java加载properties配置文件的六种方法实现代码如下:packa...
    99+
    2023-05-31
    java properties ava
  • 在Spring中如何实现加载Bean的
    本篇文章给大家分享的是有关在Spring中如何实现加载Bean的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean...
    99+
    2023-05-31
    spring bean bea
  • @ConfigurationProperties加载外部如何配置
    这篇文章主要介绍了@ConfigurationProperties加载外部如何配置的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇@ConfigurationProperties加载外部如何配置文章都会有所收获,...
    99+
    2023-07-05
  • ssl如何实现在spring boot中配置
    ssl如何实现在spring boot中配置 ?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。ssl协议位于tcp/ip协议与各种应用协议之间,为数据通信提供安全支持。ssl...
    99+
    2023-05-31
    springboot spring boo ssl
  • GO语言和Spring如何实现同步加载?
    随着互联网的不断发展,越来越多的应用程序需要在启动时加载大量的数据,这就需要使用同步加载来保证应用程序的正常运行。在本文中,我们将介绍如何使用GO语言和Spring框架来实现同步加载。 一、GO语言实现同步加载 1.1 使用sync.Wa...
    99+
    2023-11-14
    同步 load spring
  • Spring Boot + Mybatis + Spring MVC环境配置中Spring Boot如何实现初始化以及依赖添加
    这篇文章将为大家详细讲解有关Spring Boot + Mybatis + Spring MVC环境配置中Spring Boot如何实现初始化以及依赖添加,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。看一...
    99+
    2023-06-02
  • 如何实现自动配置Spring Boot框架
    本篇文章为大家展示了如何实现自动配置Spring Boot框架,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1、新建starter的Maven项目我这里以IntelliJ IDEA创建Project...
    99+
    2023-05-31
    springboot 自动配置 spring boo
  • spring boot如何实现自定义配置源
    这篇文章给大家分享的是有关spring boot如何实现自定义配置源的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。概述我们知道,在Spring boot中可以通过xml或者@ImportResource 来引入自...
    99+
    2023-05-30
    springboot
  • mysql如何实现rpm编译配置+远程访问
    小编给大家分享一下mysql如何实现rpm编译配置+远程访问,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!首先看看是否已安装过其...
    99+
    2022-10-18
  • SpringBoot加载配置文件的实现方式总结
    目录一、简介二、代码实践2.1、通过@value注解实现参数加载2.2、通过@ConfigurationProperties注解实现参数加载2.3、通过@PropertySource...
    99+
    2022-11-13
  • Spring Boot配置内容加密实现敏感信息保护
    在之前的系列教程中,我们已经介绍了非常多关于Spring Boot配置文件中的各种细节用法,比如:参数间的引用、随机数的应用、命令行参数的使用、多环境的配置管理等等。 为什么要加密?...
    99+
    2022-11-12
  • MyBatis如何实现配置MySql数据库与Spring
    这篇文章将为大家详细讲解有关MyBatis如何实现配置MySql数据库与Spring,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1.1MyBatis简介   ...
    99+
    2023-05-31
    spring mybatis mysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作