在本系列Spring Cloud Config的教程系列中,我们将讨论在运行时刷新属性配置的过程,我们将使用Spring Boot致动器/refresh端点进行/refresh 。 此外,我们还将研究使用@RefreshScope注释刷新@Value属性。

在我的Spring Cloud Config的上一教程中 ,我们使用发现服务器和发现客户端建立了一个云配置服务,并成功创建了一个示例,以在具有GIT支持的存储的分布式环境中读取应用程序配置属性。在这里,我们将继续进行演示运行时在Spring Cloud Config中刷新属性配置的功能。

在本文中,我们将只专注于刷新配置属性。 因此,我们将不会使用与发现服务器相关的配置。 我们将有一个配置服务器,用于从GIT存储库和带有执行器项目的配置客户端中加载属性。

刷新属性的不同方法

刷新配置属性的一种简单方法是使用spring boot促动器提供的/refresh端点,但这是一个手动过程,需要针对所有实例进行触发。另一种方法是/bus/refresh与spring-cloud-bus和在这种情况下,所有实例都订阅一个事件,并且一旦触发该事件,所有配置属性都将通过Spring Cloud Bus广播自动刷新。刷新这些属性的第三种方法是通过连接VCS。 在本文中,我们将讨论弹簧启动执行器的刷新端点。

Spring Cloud Config Server实施

在上一篇文章中,我们已经为该实现做好了准备。 在这里,让我们简要地讨论一下。 我们在配置服务器和Spring Boot主应用程序中定义了以下application.properties,它将REST端点公开为http:// localhost:8888,以供客户端获取配置属性。

application.properties

server.port=8888
spring.cloud.config.server.git.uri=https://github.com/only2dhir/config-repo.git

SpringCloudConfigExampleApplication.java

package com.devglan.springcloudconfigexample;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigExampleApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudConfigExampleApplication.class, args);}
}

我们在https://github.com/only2dhir/config-repo.git上定义了外部配置属性。在这里,我们为活动配置文件的本地和全局属性定义了属性。

Spring Cloud Config客户端实施

对于客户,我们有以下bootstrap.properties defined.This是我们在以前的应用程序定义的相同文件在这里

bootstrap.properties

spring.application.name=spring-cloud-config-client
spring.profiles.active=local
#spring.cloud.config.uri=http://localhost:8888

使用/ refresh端点刷新配置属性

/refresh端点仅刷新使用@ConfigurationProperties注释的那些属性,这意味着它不刷新在应用程序初始化期间初始化的那些属性。 例如,我们定义了以下配置类,该类读取具有随机前缀的属性

package com.devglan.springcloudconfigclient;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix="random")
public class PropertyConfiguration {private String property;public String getProperty() {return property;}public void setProperty(String property) {this.property = property;}
}

我们有以下控制器类,该类使用以random为前缀的属性,并且还读取以@Value注释的属性

@RestController
public class DemoController {@Value("${test.property}")private String testProperty;@Value("${test.local.property}")private String localTestProperty;@Autowiredprivate PropertyConfiguration propertyConfiguration;@RequestMapping("/")public String test() {StringBuilder builder = new StringBuilder();builder.append("global property - ").append(testProperty).append(" || ").append("local property - ").append(localTestProperty).append(" || ").append("property configuration value - ").append(propertyConfiguration.getProperty());return builder.toString();}
}

对于端点http:// localhost:8080 / spring-cloud-config-client /,将输出以下内容。

现在让我们更改定义的配置根据企业的性质spring-cloud-config-client-local.properties如下。

test.local.property=test local property changed
random.property=random property changed

现在,我们将调用执行器的http:// localhost:8080 / spring-cloud-config-client / refresh POST方法来刷新属性。 以下是具有更新属性的响应。

现在,如果我们点击http:// localhost:8080 / spring-cloud-config-client /,我们可以看到来自带有@ConfigurationProperties注释的类的属性已经更新,但是带有@Value注释的属性尚未更新,因为这是初始化的在应用程序启动期间

要更新使用@Value注释的属性,我们需要使用@RefreshScope注释该类。 因此,这里我们将使用@RefreshScope注释控制器类并重新启动客户端应用程序。再次重新启动后,我们将在属性文件中进行更改并将更改推送到git。 这次,我们两次向属性值附加了字符串,然后再次调用了刷新端点。 现在,如果我们访问URL http:// localhost:8080 / spring-cloud-config-client /,我们可以发现用@Value和@ConfigurationProperties注释的配置属性都已更新。

翻译自: https://www.javacodegeeks.com/2018/03/refresh-property-config-at-runtime-in-spring-cloud-config.html

在运行时在Spring Cloud Config中刷新属性配置相关推荐

  1. Spring Cloud Config入门(本地配置)

    转载自 https://www.cnblogs.com/zcr3108346262/p/7602314.html spring cloud config 简介 Spring Cloud Config为 ...

  2. 使用Spring Cloud Config作为外部化配置

    关于连续交付 ,最重要的实践之一是只构建一次二进制文件,并在不同的环境(开发,测试,验收等)中使用该二进制文件 . 这意味着最有可能需要外部化应用程序的配置 . 对于Spring Boot应用程序,将 ...

  3. Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh

    上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 ...

  4. spring cloud config将配置存储在数据库中

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库, ...

  5. spring cloud config将配置存储在数据库中 1

    转载请标明出处: https://blog.csdn.net/forezp/... 本文出自方志朋的博客 Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓 ...

  6. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  7. Spring Cloud Kubernetes 中文文档

    本参考指南介绍了如何使用Spring Cloud Kubernetes. 1.为什么需要Spring Cloud Kubernetes? Spring Cloud Kubernetes提供了使用Kub ...

  8. 主流配置中心的比较 Spring Cloud Config、Apollo、Nacos

    为什么需要配置中心 配置实时生效: 传统的静态配置方式要想修改某个配置只能修改之后重新发布应用,要实现动态性,可以选择使用数据库,通过定时轮询访问数据库来感知配置的变化.轮询频率低感知配置变化的延时就 ...

  9. Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务

    上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...

最新文章

  1. 面向B端市场,Mana VR团队将推出VR交互产品
  2. ML之xgboost :xgboost.plot_importance()函数的解读
  3. Eclipse Memory Analyzer 安装(Update Site: http://download.eclipse.org/mat/1.3.1/update-site/ )
  4. 又一大波笑到肾抽筋,笑出六块腹肌的段子
  5. re.containerbase.startinternal 子容器启动失败_微服务架构:基于微服务和Docker容器技术的PaaS云平台架构设计(微服务架构实施原理)...
  6. php 旋转图片 保存,如何在PHP中旋转并保存图像
  7. 12_统计学习方法总结
  8. 显示所有大写字母python_python 输出所有大小写字母的方法
  9. 华为鸿蒙原生app,华为鸿蒙开发者大赛颁奖典礼临近,鸿蒙原生应用来了
  10. 单片机与嵌入式的关系,单片机是嵌入式的子类
  11. JAVA 反编译方法
  12. 只需8招,搞定Pandas数据筛选与查询
  13. 基于zynq的千兆网udp项目_AC6102开发板千兆以太网UDP传输实验2
  14. 串口线接法是什么 详细步骤介绍
  15. ceph存储 PG的状态机和peering过程
  16. 电子警察技术原理分析
  17. java中如何插入表格_Java如何向Word文档中添加表格?
  18. stm32F407hal+AD9854dds配置+源程序
  19. 从计算机内部提取型号和mac,Mac计算机型号这么多 我该选哪一个?
  20. 华为系统wifi服务器失败是怎么回事儿,wifi 用云服务器异常

热门文章

  1. 35、JAVA_WEB开发基础之过滤器
  2. Hadoop入门(七)Mapreduce高级Shuffle
  3. JTA 深度历险 - 原理与实现
  4. ByteBuffer的使用
  5. String.format()方法的使用
  6. 使用Java 8 Stream像操作SQL一样处理数据(上)
  7. hibernate的Configuration和配置文件
  8. 2016蓝桥杯省赛---java---B---7(剪邮票)
  9. ListView条目中有CheckBox点击事件失效问题
  10. Hibernate框架(1)