1、创建spring-cloud-houge-config子项目,测试需要的项目入下

2、添加依赖

  

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId></dependency>

3、配置文件

  aoolication.properties

spring.application.name=config-server
server.port=8001
#读取本地文件
spring.profiles.active=native

  在src/main/resources下添加config-dev.properties,config-test.properties,config-pro.properties

  每个配置文件中都写一个属性env,属性值分别是 dev/test/pro

4、启动类

@EnableConfigServer //激活对配置中心的支持
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }

5、启动config

  

   http://localhost:8001/config-dev.properties

6、启动测试服务(spring-cloud-houge-provider)

  a、添加依赖

  

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

  b、配置文件

  bootstrap.properties

  

spring.cloud.config.name=config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8001/

  c、启动类

  

@SpringBootApplication
@EnableDiscoveryClient //使项目有服务注册功能
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}

  d、添加测试代码

@RestController
//@RefreshScope
@RequestMapping("/dynasty")
public class DynastyController { @Value("${env}")
private String env; @RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello, " + name + ":" + env;
}
}

  e、http://localhost:9000/dynasty/hello?name=houge

6、refresh、开启更新

  手动修改config的配置文件,调用端不会立即更新,需修改调用端代码

  

@RestController
@RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中
@RequestMapping("/dynasty")
public class DynastyController { @Value("${env}")
private String env; @RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello, " + name + ":" + env;
}
}

  springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置

  

management.security.enabled=false

7、测试

  修改config-dev.properites中env=local

  以post请求的方式来访问http://192.168.0.131:9000/refresh 就会更新修改后的配置文件

  

  http://localhost:9000/dynasty/hello?name=houge

十九、springcloud(五)配置中心本地示例和refresh的更多相关文章

  1. springcloud(七):配置中心svn示例和refresh

    上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...

  2. 二十、springcloud(六)配置中心服务化和高可用

    1.问题描述 前一篇,spring-cloud-houge-provider(称之为客户端)直接从spring-cloud-houge-config(称之为服务端)读取配置,客户端和服务端的耦合性太高 ...

  3. spring cloud深入学习(八)-----配置中心svn示例和refresh

    svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依赖 <dependencies> <dependency> <groupId>org.spri ...

  4. Spring Cloud(十四)Config 配置中心与客户端的使用与详细

    前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...

  5. Spring Cloud(九):配置中心(消息总线)【Finchley 版】

    Spring Cloud(九):配置中心(消息总线)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-05-07 |  我们在 Spring Cloud(七):配置中心 ...

  6. Spring Cloud(九):分布式配置中心和消息总线

    我们在Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码 ...

  7. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  8. 七、springcloud之配置中心Config(二)之高可用集群

    方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...

  9. SpringCloud 分布式配置中心

    SpringCloud 分布式配置中心 服务端 创建工程并完善结构 国际惯例,把maven工程创建完善 pom.xml <?xml version="1.0" encodin ...

随机推荐

  1. java笔记 -- 数学函数与常量

    Math类中, 包含了各种数学函数 不用在数学方法名和常量名前添加前缀Math., 只要在源文件的顶部加上这行代码: import static java.lang.Math.*; (静态导入) 例: ...

  2. Git commit/pull/push的操作步骤

    1.操作步骤需要严格执行如下顺序:commit->pull->push 2.commit:将代码提交到本地仓库. 3.pull:将远程仓库代码同步到本地仓库.如遇冲突,解决冲突,重复com ...

  3. C++类的大小计算

    转自http://www.tuicool.com/articles/uiUJry 一个空的class在内存中多少字节?如果加入一个成员函数后是多大?这个成员函数存储在内存中什么部分? 一个Class对 ...

  4. xml和tomcat介绍

    一 xml介绍: xml:可扩展性的文件 功能: 1.作为框架的配置文件 2.方便在网络中传输数据 <a> <b></b> <c></c> ...

  5. Steam饥荒

    存档回滚 D:\Program Files (x86)\Steam\userdata\***\219740\remote 巨人国是survival_数字,海难是shipwreck_数字,哈姆雷特是po ...

  6. Flask之SQLAlchemy,flask_session以及蓝图

    数据库操作 ORM ORM 全拼 Object-Relation Mapping,中文意为 对象-关系映射.主要实现模型对象到关系数据库数据的映射 优点 : 只需要面向对象编程, 不需要面向数据库编写 ...

  7. 『Python CoolBook』数据结构和算法_字典比较&字典和集合

    一.字典元素排序 dict.keys(),dict.values(),dict.items() 结合max.min.sorted.zip进行排序是个很好的办法,另外注意不使用zip时,字典的lambd ...

  8. MapReduce(五)

    MapReduce的(五) 1.MapReduce的多表关联查询. 根据文本数据格式.查询多个文本中的内容关联.查询. 2.MapReduce的多任务窜执行的使用 多任务的串联执行问题,主要是要建立c ...

  9. python3练习-查找文件

    题: 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径 import os import os.path def find_file(root,pa ...

  10. let,const 声明的变量不会绑定给window对象 而var会

    先来看一道题 let id = 2; let json = { id: 1, show:function(){ setTimeout(function(){ console.log(this.id); ...