spring-cloud构架微服务(2)-全局配置二
接上篇,实际项目中,可能会遇到有些配置项,例如:邮件地址、手机号等在服务已经上线之后做了改动(就当会出现这种情况好了)。然后你修改了配置信息,就得一个一个去重启对应的服务。spring-全局配置提供了一种热部署机制,可以在重启工程的前提下改变内存中配置项的值。
还是中client-config项目,首先我们要再添加一个jar引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
这个监控包,包含了/refresh的api接口,接受post方法。
上篇中的UserController类,我们只要在上面加入@RefreshScope这个注解,那么里面的 testProperty 这个属性值就会被动刷新。
package com.bing.User; import com.bing.model.User; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* Created by szt on 2016/11/18.
*/
@RestController
@RefreshScope
public class UserController {
@Value("${bing.for.test}")
private String testProperty; @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getUser(@PathVariable("id") String id) {
User user=new User();
user.setName(testProperty);
user.setId(id);
user.setCreatedTime(new Date());
return user;
}
}
bingyulei007
这个刷新需要手动请求。
当我们启动配置服务器(上一篇中的configserver),然后启动client工程,访问路径http://localhost:8081/user/id1返回:
{
"id": "id1",
"name": "hello-word",
"createdTime": "2016-11-18 15:41:38"
}
bingyulei007
我们更改配置文件中属性值
bing.for.test=new-hello
然后调用地址http://localhost:8081/refresh,之后这个节点上的属性值就会被刷新。验证效果,访问路径http://localhost:8081/user/id1返回:
{
"id": "id1",
"name": "new-hello",
"createdTime": "2016-11-18 16:03:22"
}
bingyulei007
说明属性已经被刷新。但是实际随着项目的完善,我们后端可能会部署多个模块,每个模块也可能采用集群部署,我们不能傻傻的每个节点都去维护一个刷新。还好spring-cloud为我们提供了spring-bus模块,这个模块要依赖于服务发现机制,我们后面再说明。
spring-cloud构架微服务(2)-全局配置二的更多相关文章
- spring-cloud构架微服务(1)-全局配置
使用spring-cloud是基于熟悉springboot基础上进行的.本篇介绍全局配置,spring-boot版本就以1.4.0来做吧.项目地址: https://git.oschina.net/b ...
- Spring Cloud搭建微服务架构----文章汇总
Spring Cloud搭建微服务架构----前言 原文地址:https://my.oschina.net/u/1000241/blog/882929 Spring Cloud搭建微服务架构----使 ...
- Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】
转载:http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ 原创 2017-08-26 翟永超 Spring Cloud 被围观 ...
- Spring Cloud构建微服务架构(一)服务注册与发现
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...
- 【微服务】使用spring cloud搭建微服务框架,整理学习资料
写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
- Spring Cloud与微服务构建:微服务简介
Spring Cloud与微服务构建:微服务简介 单体架构及其不足 1.单体架构简介 在软件设计中,经常提及和使用经典的3曾模型,即表示层.业务逻辑层和数据访问层. 表示层:用于直接和用户交互,也成为 ...
- 第1章 Spring Cloud 构建微服务架构(一)服务注册与发现
一.Spring Cloud 简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总 ...
- Spring Cloud Alibaba | 微服务分布式事务之Seata
Spring Cloud Alibaba | 微服务分布式事务之Seata 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud:Green ...
随机推荐
- P2381 圆圆舞蹈
题意:一个圆,上面有n头牛(卧槽) 给出相邻两头牛顺时针的距离 问两只最远的牛的距离(min(顺时针距离,逆时针距离)) 最远距离一定$\le$距离和/2 先求个前缀和 那么问题转化为:找到 $s_j ...
- USACO 1.1.1 YOUR RIDE IS HERE
众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走.他们为每 ...
- 数据结构8: 双向链表(双向循环链表)的建立及C语言实现
之前接触到的链表都只有一个指针,指向直接后继,整个链表只能单方向从表头访问到表尾,这种结构的链表统称为 “单向链表”或“单链表”. 如果算法中需要频繁地找某结点的前趋结点,单链表的解决方式是遍历整个链 ...
- python内存相关问题
想要弄清楚内存相关的问题,就要理清楚:变量.内存地址.值之间的关系:1.程序里什么时候分配新的内存地址?答:1.定义一个变量,内存就开辟一个内存空间,分配一个内存地址. 特殊: 如:a=687 a=1 ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_8.1实例构造器和类A
public class SomeType { } //等价于 public class SomeType { public SomeType():base(){} } [解释]如果定义的类没有显示定 ...
- PHP7.1新特性一览
PHP7.0的性能是PHP5.6的两倍 http://www.phpchina.com/article-40237-1.html 可空类型 list 的方括号简写 void 返回类型 类常量属性设定 ...
- java中Runtime类和Process类的简单介绍
在java.lang包当中定义了一个Runtime类,在java中对于Runtime类的定义如下: Java code public class Runtime extends Object 每个 J ...
- 搭建element-ui Vue结构
1.安装淘宝镜像 npm install cnpm -g --registry=https://registry.npm.taobao.org 2.安装webpack cnpm install web ...
- 服务器运行两个或两个以上的tomcat
一:解决方法 转载: https://www.cnblogs.com/xiaobai1226/p/7662392.html 二:解决方法 将tocmat下 bin---->tomcat8w.e ...
- HIVE sql取中文
select regexp_replace(str,'[^\\u4e00-\\u9fa5]','') as str1 from ( select 'test测试test' as str ) t ;