远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将

management.security.enabled 设置为 false,然后访问客户端的 /refresh 端点进行刷新,访问改端点要使用 HTTP 的 POST 方法,客户端的 refresh 在接收到请求后,会重新到配置服务器获取最新的配置,然后用新的配置和旧配置进行对比,最终把有修改的配置 Key 返回给调用者。

在实际应用中,往往不仅是刷新一个配置的值那么简单,由于 Spring 容器中的很多 Bean 都是根据某个属性值来进行初始化的,配置一旦更新,需要重建这个 Bean 的实例,为了解决这个问题,可以使用 @RefreshScope 注解来标注 Bean,但 /refresh 端点被访问时,负责处理刷新的 ContextRefresher 类,会先去远程的配置服务刷新配置,然后再调用 RefreshBean 的 refreshAll 方法来处理实例,容器中使用了 @RefreshScope 注解进行修饰的 Bean,都会在缓存中销毁,当这些 Bean 被再次引用时,就会创建新的实例,从而达到刷新的效果。

 
 

刷新配置示例

  • 增加依赖

    为了支持配置文件的刷新操作,需要增加依赖 spring-boot-starter-actuator ,修改 POM.xml 文件内容如下:

    <?xmlversion="1.0"encoding="UTF-8"?>

    <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

     
     

    <groupId>org.lixue.config</groupId>

    <artifactId>spring-cloud-config-client</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

     
     

    <name>spring-cloud-config-client</name>

     
     

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.12.RELEASE</version>

    <relativePath/><!--lookupparentfromrepository-->

    </parent>

     
     

    <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <java.version>1.8</java.version>

    <spring-cloud.version>Dalston.SR5</spring-cloud.version>

    </properties>

     
     

    <dependencies>

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-config</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

     
     

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-test</artifactId>

    <scope>test</scope>

    </dependency>

    </dependencies>

     
     

    <dependencyManagement>

    <dependencies>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-dependencies</artifactId>

    <version>${spring-cloud.version}</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    </dependencies>

    </dependencyManagement>

     
     

    <build>

    <plugins>

    <plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    </plugin>

    </plugins>

    </build>

    </project>

 
 

  • 增加配置

    修改 src/main/resources 目录下的 bootstrap.yml 配置文件,内容如下:

    #配置应用名称

    spring:

    application:

    name:spring-cloud-config-client

    #配置分布式配置中心地址和相关配置

    cloud:

    config:

    uri:http://localhost:8080

    #表示分支,客户端配置后,会替换到分布式配置中心的default-lable配置

    label:test

    #表示配置文件名称,如果不配置则使用spring.application.name配置项

    name:spring-cloud-config-client

    #表示配置文件的profile,实际获取文件为${spring.cloud.config.name}-${spring.cloud.config.profile}.yml

    profile:dev

    # 关闭管理安全控制

    management:

    security:

    enabled:false

     
     

  • 测试REST服务

    Info 类的 Bean 是根据配置文件的 info.name 和 info.desc 属性来创建的,newInfo 方法被注解 @Bean 和 @RefreshScope 标注,表示刷新时,需要从缓存销毁。

    package org.lixue.config;

     
     

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.cloud.context.config.annotation.RefreshScope;

    import org.springframework.context.annotation.Bean;

    import org.springframework.core.env.Environment;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.bind.annotation.RestController;

     
     

    @RestController

    public class MyRESTController{

    @Autowired

    private Environment environment;

     
     

    @Autowired

    private Info info;

     
     

    @RequestMapping(path="/",method=RequestMethod.GET)

    public String getApplicationName(){

    return environment.getProperty("spring.application.name");

    }

     
     

    @RequestMapping(path="/myInfo",method=RequestMethod.GET)

    public String getInfo(){

    return info.getName()+"-"+info.getDesc();

    }

     
     

    @Bean

    @RefreshScope

    public Info newInfo(){

    Info info=new Info();

    info.setName(environment.getProperty("info.name","null"));

    info.setDesc(environment.getProperty("info.desc","null"));

    return info;

    }

     
     

    static class Info{

    private String name;

    private String desc;

     
     

    public String getName(){

    returnname;

    }

     
     

    public void setName(Stringname){

    this.name=name;

    }

     
     

    public String getDesc(){

    returndesc;

    }

     
     

    public voids etDesc(Stringdesc){

    this.desc=desc;

    }

    }

    }

     
     

  • 测试验证

    首先启动 spring-cloud-config 项目和 SVN 服务,目前 SVN 服务的相关配置文件内容如下:

    spring:

    application:

    name:spring-cloud-config-client-dev

    server:

    info:

    name:refresh

    desc:刷新Bean测试

    访问 http://localhost:8013/myInfo 返回结果为:"refresh-刷新Bean测试",修改 SVN 服务的相关配置文件内容如下:

    spring:

    application:

    name:spring-cloud-config-client-dev

    server:

    info:

    name:refresh

    desc:刷新Bean测试,修改后的

    使用 HTTP 的 POST 方法访问 http://localhost:8013/refresh ,然后再次访问 http://localhost:8013/myInfo ,可以看到返回结果已经返回了新配置文件的内容:"refresh-刷新Bean测试,修改后的"

     
     

     
     

Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  2. Spring Cloud(Dalston.SR5)--Config 集群配置中心-加解密

    实际应用中会涉及很多敏感的数据,这些数据会被加密保存到 SVN 仓库中,最常见的就是数据库密码.Spring Cloud Config 为这类敏感数据提供了加密和解密的功能,加密后的密文在传输给客户端 ...

  3. Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群

    通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...

  4. Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置

    Spring Cloud 在 Zuul 的 routing 阶段实现了几个过滤器,这些过滤器决定如何进行路由工作. 简单路由(SimpleHostRoutingFilter) 该过滤器运行后,会将 H ...

  5. Spring Cloud(Dalston.SR5)--Eureka 常用配置

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  6. Spring Cloud之Zuul网关集群

    Nginx+Zuul 一主一备 或者 轮训多个 在微服务中,所有服务请求都会统一到Zuul网关上. Nginx 配置: #user nobody; worker_processes 1; #error ...

  7. Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退

    当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...

  8. Spring Cloud(Dalston.SR5)--Zuul 网关

    我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...

  9. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...

随机推荐

  1. spring的历史和设计科学

    Spring的起源 要谈Spring的历史,就要先谈J2EE.J2EE应用程序的广泛实现是在1999年和2000年开始的,它的出现带来了诸如事务管理之类的核心中间层概念的标准化,但是在实践中并没有获得 ...

  2. jenkins git ftp 发布.net 项目

    一次搞这个,在其他文章的基础上 添加下自己的 笔记,方便自己查看, -------需要准备的东西--------------- 下载jenkins https://jenkins.io/downloa ...

  3. ie9上传后下载json

    1.保持后台控制器返回的数据为字符串格式 2.js:dataType类型保持为html格式 dataType: 'html',//默认就是html类型,不写对火狐有影响 3.将上传后后台返回的字符串转 ...

  4. nginx 添加代理

    1 确认安装路径 ps aux | grep nginx 2.进入配置目录 3.使用vi编辑配置文件 如果是新增,可以参考其他配置,5yy复制相应行,p粘贴,然后修改内容后:wq保存退出 4.验证配置 ...

  5. 理解JavaScript中的属性描述符

    我们把描述JavaScript中定义内部特性的属性叫做属性描述符 分为两大类:数据描述符和存取描述符 数据描述符是一个拥有可写或不可写的属性(Writable); 存取描述符不包含数据值,是一组拥有g ...

  6. VO、AO、执行环境和作用域链

    1.变量对象(variable object) 原文:Every execution context has associated with it a variable object. Variabl ...

  7. 在Java中用 . 深层访问JSON数据

    本文介绍Java中解析JSON的一种方法,可以让我们在Java程序中也用x.x.x的形式访问JSON数据中的值. 代码大部分来源非本人,本人在源代码基础上加以修改以使正常运行. 代码: // 将提取方 ...

  8. Java 8 Lambda 表达式(二)

    lambdas 实现 Runnable 接口 下面是使用 lambdas 来实现 Runnable 接口的示例: // 1.1使用匿名内部类 new Thread(new Runnable() { @ ...

  9. Pick the Right Shoes

    shoe-->shoes pointed shoes ballet shoes high heel wedged boots strappy sandals绑带凉鞋 t-straps丁字鞋 co ...

  10. Debian系统 + XFCE桌面初识,基础环境搭建

    有幸分享个人的Linux下的习惯配置,具体操作可能阐述得比较粗糙. 在图形化界面进行配置操作,十分简便舒心. Linux发行版:Debian9.5(Stretch) 桌面Sesion:XFCE4 一. ...