十九、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 ...
随机推荐
- Rancher2.0导入本地RKE Kubernetes集群图解
简要说明: 使用RKE工具在192.168.3.161机器上,创建包含两个节点的Kubernetes集群,(192.168.3.162和192.168.3.163).RKE会自动在/home/用户 ...
- Windows下安装python的scipy等科学计算包(转)
如果要使用python进行科学计算.数据分析等,一定要安装scipy.seaborn.numpy等等包. 但Windows下安装python的第三方库经常会出现问题.此前,已介绍过Windows下如何 ...
- Bupt归队赛, gunfight
只需要关心是否开枪,上个人和当前这个人的位置关系,转移可以前缀和优化 为了不重复,始终认为第一个就是1,最后答案乘以n #include<bits/stdc++.h> using name ...
- 非阻塞IO发送http请求
import socket from urllib.parse import urlparse from selectors import DefaultSelector, EVENT_READ, E ...
- static_assert enable_if 模板编译期检查
conceptC++ http://www.generic-programming.org/faq/?category=conceptcxx Checking Concept Without Conc ...
- 朋友给的IE滚动条
scrollbar-arrow-color: #f4ae21; /*图6,三角箭头的颜色*/scrollbar-face-color: #333; /*图5,立体滚动条的颜色*/scrollbar ...
- linux c使用socket进行http 通信,并接收任意大小的http响应(五)
http.c data2.c http_url.c http.h data2.h http_url.h主要实现的功能是通过URL结构体来实现HTTP通信,你可以把这三个文件独立出来,作为HTTP通信模 ...
- sql 中取整,四舍五入取整,向下取整,向上取整。
SELECT round(52.45, 0) AS round4, round(52.54, 0) AS round5, round(52.45, 1) AS round41, round(52.54 ...
- Forth相关IO操作
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- web app、hybrid app和native app区别