宜宾 网站建设,响应式网站样式图怎么做,工程建设信息网站有哪些,网站如何做微信支付宝支付在本系列Spring Cloud Config的教程系列中#xff0c;我们将讨论在运行时刷新属性配置的过程#xff0c;我们将使用Spring Boot致动器/refresh端点进行/refresh 。 此外#xff0c;我们还将研究使用RefreshScope注释刷新Value属性。 在我的Spring Cloud Config的上一教程中… 在本系列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// localhost8888以供客户端获取配置属性。 application.properties server.port8888
spring.cloud.config.server.git.urihttps://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.namespring-cloud-config-client
spring.profiles.activelocal
#spring.cloud.config.urihttp://localhost:8888使用/ refresh端点刷新配置属性 /refresh端点仅刷新使用ConfigurationProperties注释的那些属性这意味着它不刷新在应用程序初始化期间初始化的那些属性。 例如我们定义了以下配置类该类读取具有随机前缀的属性 package com.devglan.springcloudconfigclient;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefixrandom)
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// localhost8080 / spring-cloud-config-client /将输出以下内容。 现在让我们更改定义的配置根据企业的性质spring-cloud-config-client-local.properties如下。 test.local.propertytest local property changed
random.propertyrandom property changed 现在我们将调用执行器的http// localhost8080 / spring-cloud-config-client / refresh POST方法来刷新属性。 以下是具有更新属性的响应。 现在如果我们点击http// localhost8080 / spring-cloud-config-client /我们可以看到来自带有ConfigurationProperties注释的类的属性已经更新但是带有Value注释的属性尚未更新因为这是初始化的在应用程序启动期间 要更新使用Value注释的属性我们需要使用RefreshScope注释该类。 因此这里我们将使用RefreshScope注释控制器类并重新启动客户端应用程序。再次重新启动后我们将在属性文件中进行更改并将更改推送到git。 这次我们两次向属性值附加了字符串然后再次调用了刷新端点。 现在如果我们访问URL http// localhost8080 / spring-cloud-config-client /我们可以发现用Value和ConfigurationProperties注释的配置属性都已更新。 翻译自: https://www.javacodegeeks.com/2018/03/refresh-property-config-at-runtime-in-spring-cloud-config.html