接上篇,实际项目中,可能会遇到有些配置项,例如:邮件地址、手机号等在服务已经上线之后做了改动(就当会出现这种情况好了)。然后你修改了配置信息,就得一个一个去重启对应的服务。spring-全局配置提供了一种热部署机制,可以在重启工程的前提下改变内存中配置项的值。

还是中client-config项目,首先我们要再添加一个jar引用:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

这个监控包,包含了/refresh的api接口,接受post方法。

上篇中的UserController类,我们只要在上面加入@RefreshScope这个注解,那么里面的 testProperty 这个属性值就会被动刷新。

  1. package com.bing.User;
  2.  
  3. import com.bing.model.User;
  4.  
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. import java.util.Date;
  13.  
  14. /**
  15. * Created by szt on 2016/11/18.
  16. */
  17. @RestController
  18. @RefreshScope
  19. public class UserController {
  20. @Value("${bing.for.test}")
  21. private String testProperty;
  22.  
  23. @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
  24. public User getUser(@PathVariable("id") String id) {
  25. User user=new User();
  26. user.setName(testProperty);
  27. user.setId(id);
  28. user.setCreatedTime(new Date());
  29. return user;
  30. }
  31. }

bingyulei007

这个刷新需要手动请求。

当我们启动配置服务器(上一篇中的configserver),然后启动client工程,访问路径http://localhost:8081/user/id1返回:

  1. {
  2. "id": "id1",
  3. "name": "hello-word",
  4. "createdTime": "2016-11-18 15:41:38"
  5. }

bingyulei007

我们更改配置文件中属性值

  1. bing.for.test=new-hello
  1. 然后调用地址http://localhost:8081/refresh,之后这个节点上的属性值就会被刷新。验证效果,访问路径http://localhost:8081/user/id1返回:
  1. {
  2. "id": "id1",
  3. "name": "new-hello",
  4. "createdTime": "2016-11-18 16:03:22"
  5. }

bingyulei007

说明属性已经被刷新。但是实际随着项目的完善,我们后端可能会部署多个模块,每个模块也可能采用集群部署,我们不能傻傻的每个节点都去维护一个刷新。还好spring-cloud为我们提供了spring-bus模块,这个模块要依赖于服务发现机制,我们后面再说明。

spring-cloud构架微服务(2)-全局配置二的更多相关文章

  1. spring-cloud构架微服务(1)-全局配置

    使用spring-cloud是基于熟悉springboot基础上进行的.本篇介绍全局配置,spring-boot版本就以1.4.0来做吧.项目地址: https://git.oschina.net/b ...

  2. Spring Cloud搭建微服务架构----文章汇总

    Spring Cloud搭建微服务架构----前言 原文地址:https://my.oschina.net/u/1000241/blog/882929 Spring Cloud搭建微服务架构----使 ...

  3. Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】

    转载:http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ 原创  2017-08-26  翟永超  Spring Cloud 被围观 ...

  4. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  5. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  6. Spring Cloud与微服务构建:Spring Cloud简介

    Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...

  7. Spring Cloud与微服务构建:微服务简介

    Spring Cloud与微服务构建:微服务简介 单体架构及其不足 1.单体架构简介 在软件设计中,经常提及和使用经典的3曾模型,即表示层.业务逻辑层和数据访问层. 表示层:用于直接和用户交互,也成为 ...

  8. 第1章 Spring Cloud 构建微服务架构(一)服务注册与发现

      一.Spring Cloud 简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总 ...

  9. Spring Cloud Alibaba | 微服务分布式事务之Seata

    Spring Cloud Alibaba | 微服务分布式事务之Seata 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud:Green ...

随机推荐

  1. php 读取excel文件

    首先下载插件PHPExcel (PHPExcel-1.8),以tp5框架为例,将该文件放在verdor文件夹下.然后引入IOFactory文件. 1.读取文件的部分内容(用于固定格式) public ...

  2. [ZJOI2009]狼和羊的故事 BZOJ1412

    题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...

  3. object都有string

    object都有tostringString item=spinner.getSelectedItem().toString();String item01=String.valueOf(spinne ...

  4. Python迭代器生成器,模块和包

      1.迭代器和生成器 2.模块和包 1.迭代器 迭代器对象要求支持迭代器协议的对象,在Python中,支持迭代器协议就是实现对象的__iter__()和__next__()方法.    其中__it ...

  5. vue.js 基础

      vuejs 遍历 数组, vue js 文章 http://www.cnblogs.com/libin-1/p/6851775.html https://zhuanlan.zhihu.com/p/ ...

  6. ListItemType.AlternatingItem 和 ListItemType.Item

    项目中后台绑定Reapter项时,判断 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Alte ...

  7. DJ 算法的队列优先优化

    DJ算法就是求单源最短路的算法,但是时间复杂度不太理想,所以在此献上用最小堆来优化的算法. 如果不懂优先队列可以先去看STL分类关于优先队列的介绍: ///POJ 2387为例 #include< ...

  8. Experimental Educational Round: VolBIT Formulas Blitz A

    Description The HR manager was disappointed again. The last applicant failed the interview the same ...

  9. day27 粘包及粘包的解决方案

    1.   粘包现象 先了解一个词MTU MTU是Maximum Transmission Unit的缩写.意思是网络上传送的最大数据包.MTU的单位是字节. 大部分网络设备的MTU都是1500个字节, ...

  10. 如何将微信上传AMR格式语音转化为MP3格式

    1. 服务器安装ffmpeg 2. 执行命令 ffmpeg -i {amr_file_path} -f mp3 -acodec libmp3lame -y {mp3_file_path} public ...