本文测试使用的spring cloud版本为:

  1. Dalston.SR1

很多朋友只知道spring cloud config可以刷新远程git的配置到内存中,

却不知道spring cloud config的客户端可以脱离服务端使用,

更不知道spring cloud config客户端结合actuator还可以刷新本地的配置文件到内存中。

具体做法如下:

1、pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.liuyx</groupId>
  8. <artifactId>test-config-refresh</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17.  
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-config</artifactId>
  26. </dependency>
  27.  
  28. <!--监控+refresh配置-->
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-actuator</artifactId>
  32. </dependency>
  33. </dependencies>
  34.  
  35. <dependencyManagement>
  36. <dependencies>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-dependencies</artifactId>
  40. <version>Dalston.SR1</version>
  41. <type>pom</type>
  42. <scope>import</scope>
  43. </dependency>
  44. </dependencies>
  45. </dependencyManagement>
  46.  
  47. </project>

单独引入 spring-boot-starter-actuator或者spring-cloud-starter-config(spring cloud config的客户端)  是不会暴露/refresh端点的,两者同时引入之后才能暴露/refresh端点。

2、一般使用spring-cloud-starter-config的文章都会让你在bootstrap里加上配置中心服务端的地址,这里我们要脱离配置中心服务端使用,所以这些配置完全不需要。

3、对需要刷新的属性使用@Value注解,同时将类使用@RefreshScope注解进行标记,示例如下:

  1. package com.liuyx.test;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @SpringBootApplication
  11. @RestController
  12. @RefreshScope
  13. public class Main {
  14. public static void main(String[] args) {
  15. SpringApplication.run(Main.class);
  16. }
  17.  
  18. private static int port;
  19.  
  20. @Value("${server.port}")
  21. public void setPort(int port){
  22. this.port=port;
  23. }
  24.  
  25. @RequestMapping("/port")
  26. public int port(){
  27. return port;
  28. }
  29. }

这里我的变量是一个static变量,所以只能在非static的set方法上加@Value注解,而不是变量定义行的上方。如果不是静态变量则可以直接写作:

  1. @Value("${server.port}")
  2. private int port;

4、application.properties配置

  1. server.port=80
  2. local.test=hello1
  3. management.security.enabled=false

5、测试

  1、启动项目,访问 http://localhost/port 显示 80

  2、修改classpath(注意是classpath,即你编译后的class文件所处的目录)下的application.properties将server.port改为801

  

  3、发送空post(注意是post)请求到 http://localhost:80/refresh

  4、再次访问 http://localhost/port 显示 801 测试成功

最后的补充:

  即使结合配置中心服务端使用,该方法也是有效的,所有有效位置的有效配置文件(如git上的,jar内的,jar外的)都会被扫描,并根据一定的顺序进行覆盖,覆盖顺序传送门

(完毕)

spring boot 配置动态刷新的更多相关文章

  1. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  2. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  3. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  4. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  5. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  6. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  7. [转] Spring Boot配置多个DataSource

    [From]  https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...

  8. Spring boot 配置异步处理执行器

    示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  9. Spring boot配置404、500页面

    Spring boot 配置404页面很简单,如果你访问的url没有找到就会出现spring boot 提示的页面,很明显Spring boot不用配置就可以显示404页面了. 在template下创 ...

随机推荐

  1. win7 卸载vs2010 后无法安装vs2008 提示 Microsoft Visual Studio Web创作组件安装失败 (转)

    解决方法: Visual Studio 2008 安装失败(“Web 创作组件”无法安装)解决方法   Visual Studio 2008 安装失败(“Web 创作组件”无法安装)解决方法(一) 今 ...

  2. vue使用watch 观察路由变化,重新获取内容

    watch: { $route(to) { console.log(to) if (to.path.indexOf('index') != -1) { //路由变化后重新获取帖子列表 this.$ht ...

  3. python对json的操作总结 zz

    Json简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式.Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式.现在也常用于h ...

  4. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. 用Hadoop构建电影推荐系统

    转自:http://blog.fens.me/hadoop-mapreduce-recommend/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, ...

  6. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(六)针对spark2.2.1以yarn方式启动spark-shell抛出异常:ERROR cluster.YarnSchedulerBackend$YarnSchedulerEndpoint: Sending RequestExecutors(0,0,Map(),Set()) to AM was unsuccessful

    Spark以yarn方式运行时抛出异常: [spark@master bin]$ cd /opt/spark--bin-hadoop2./bin [spark@master bin]$ ./spark ...

  7. Eclipse关掉项目SVN的链接

    有时候 svn 会导致 eclipse 反应很慢,可以关掉 svn项目信息展现. 1. 点击项目文件夹,右键出现项目信息 2. 选择team项 3. Disconnect.

  8. Xamarin/Mono IOS Limitations

    http://developer.xamarin.com/guides/ios/advanced_topics/limitations/ Since applications on the iPhon ...

  9. CHtmlEditCtrl(1) : Use CHtmlEditCtrl to Create a Simple HTML Editor

    I needed a lightweight HTML editor to generate "rich text" emails, so I decided to explore ...

  10. 以sb7code为基础创建一个基本的OpenGL项目

      以sb7code为基础创建一个基本的OpenGL项目   从github上面下载sb7code代码: https://github.com/openglsuperbible/sb7code 打开H ...