十九、springcloud(五)配置中心本地示例和refresh
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的更多相关文章
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- 二十、springcloud(六)配置中心服务化和高可用
1.问题描述 前一篇,spring-cloud-houge-provider(称之为客户端)直接从spring-cloud-houge-config(称之为服务端)读取配置,客户端和服务端的耦合性太高 ...
- spring cloud深入学习(八)-----配置中心svn示例和refresh
svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依赖 <dependencies> <dependency> <groupId>org.spri ...
- Spring Cloud(十四)Config 配置中心与客户端的使用与详细
前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...
- Spring Cloud(九):配置中心(消息总线)【Finchley 版】
Spring Cloud(九):配置中心(消息总线)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-05-07 | 我们在 Spring Cloud(七):配置中心 ...
- Spring Cloud(九):分布式配置中心和消息总线
我们在Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码 ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 七、springcloud之配置中心Config(二)之高可用集群
方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...
- SpringCloud 分布式配置中心
SpringCloud 分布式配置中心 服务端 创建工程并完善结构 国际惯例,把maven工程创建完善 pom.xml <?xml version="1.0" encodin ...
随机推荐
- DP入门水题集
HDU 1087 Input contains multiple test cases. Each test case is described in a line as follow:N value ...
- caffe-windows画loss与accuracy曲线
参考博客: http://blog.csdn.net/sunshine_in_moon/article/details/53541573 进入tools/extra/文件夹中,修改plot_train ...
- erlang调试方法
第一种方式,加打印记录 %%-define(debug, ok). -ifdef(debug). -define(LOG(X), io:format("pid:~p , {~p,~p}: ~ ...
- Flask之SQLAlchemy,flask_session以及蓝图
数据库操作 ORM ORM 全拼 Object-Relation Mapping,中文意为 对象-关系映射.主要实现模型对象到关系数据库数据的映射 优点 : 只需要面向对象编程, 不需要面向数据库编写 ...
- Luffy之购物车页面搭建
前面已经将一些课程加入购物车中,并保存到了后端的redis数据库中,此时做购物车页面时,我们需要将在前端向后端发送请求,用来获取数据数据 购物车页面 1.首先后端要将数据构建好,后端视图函数如下代码: ...
- .net正则IP加端口,并返回IP加端口
public string GetIp(string url) { var reg = new Regex(@"\d{2,3}([.]\d{1,3}){3}:\d{2,5}"); ...
- C#Windows窗体中添加了AxWindowsMediaPlayer的详细用法影响键盘操作的问题
最近在写一个飞机大战游戏,但在为游戏背景添加声音的时候,发现添加了AxWindowsMediaPlayer的详细用法音乐控件不能再通过键盘控制飞机的移动了,在网上查了许久,没找到原因,差点就想去找老师 ...
- php7 数据导出Excel office 2011中文乱码问题
public function test(){ $data = array( array( 'name' => '对对对', 'score' => 80, 'grade' => '急 ...
- JS 中的广度与深度优先遍历
现在有一种类似树的数据结构,但是不存在共同的根节点 root,每一个节点的结构为 {key: 'one', value: '1', children: [...]},都包含 key 和 value,如 ...
- flashfxp 数据socket错误 连接已超时 filezilla
最近windows server 开启了防火墙后发现flashfxp连不上,报超时. 1,服务端的动态端口从指定的范围内取, 2,防火墙开启范围内端口. 参考:http://jingyan.baidu ...