Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置
远程 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 集群配置中心-刷新配置的更多相关文章
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-加解密
实际应用中会涉及很多敏感的数据,这些数据会被加密保存到 SVN 仓库中,最常见的就是数据库密码.Spring Cloud Config 为这类敏感数据提供了加密和解密的功能,加密后的密文在传输给客户端 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置
Spring Cloud 在 Zuul 的 routing 阶段实现了几个过滤器,这些过滤器决定如何进行路由工作. 简单路由(SimpleHostRoutingFilter) 该过滤器运行后,会将 H ...
- Spring Cloud(Dalston.SR5)--Eureka 常用配置
配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...
- Spring Cloud之Zuul网关集群
Nginx+Zuul 一主一备 或者 轮训多个 在微服务中,所有服务请求都会统一到Zuul网关上. Nginx 配置: #user nobody; worker_processes 1; #error ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退
当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关
我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...
- Spring Cloud(Dalston.SR5)--Hystrix 监控
在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...
随机推荐
- in条件后面有多个字段,in后面只能有一个字段 Operand should contain 1 column(s)
今天在sql测试的时候发现了这个错误:Operand should contain 1 column(s). 原因是in条件后面有多个字段,in后面只能有一个字段.
- 2017年5月11日17:43:06 rabbitmq 消费者队列
从昨天开始发现个问题,一个接口在本地调用时大部分正常,一旦在生成者打一个断点调试,并且在promotion也打断点的时候会出现没有返回channel的异常,然后消费者就再也消费不了了 16:57:45 ...
- 使用Bash Bunny从被锁定的系统抓取登陆凭据
在今年早些时候,FB就对Bash Bunny做了相关的报导.这款号称“世界上最先进的USB攻击工具”的Bash Bunny,是否真的像其所说的一样是款渗透神器呢?下面,我将通过实例演示如何利用Bash ...
- 【原创】ACR傻瓜式破解IC芯片卡
1.简介: 智能卡(英语:Smart card 或IC Card),又称智慧卡.聪明卡.集成电路卡及IC卡,是指粘贴或嵌有集成电路芯片的一种便携式卡片塑料.卡片包含了微处理器.I/O接口及存储器,提供 ...
- CentOS7的网卡重启方法
1.centos6的网卡重启方法:service network restartcentos7的网卡重启方法:systemctl restart network 2.DNS配置文件:cat /etc/ ...
- Spring 注入集合
Spring 中,注入集合类型的数值方式 <bean id="javaCollection" class="com.qie_zi.JavaCollection&qu ...
- caog
import pandas as pd#匹配可发库存1. import oslst=os.listdir(r'E:\每日必做\琪琪小象库存')lst1=[]for i in lst: if i[:2] ...
- json模块
dic = {"name":"boke","age":"18"} #字典 data = json.dumps(dic) ...
- Python学习之路基础篇--11-12Python基础,函数的装饰器
对于装饰器来说,就是在不改变函数的调用的情况下,对函数的前后增加了些许功能,这完全符合函数的 开放封闭 原则.装饰器的本质 其实就是一个闭包函数. 这是一个装饰器的步骤图 ret = func(*ar ...
- jmeter中提取json串
https://blog.csdn.net/zha6476003/article/details/80295068