Spring Cloud 2-Config 分布式配置中心(七)
1.github配置
通过github作为分布式配置中心,在github上新建仓库:spring-cloud-learn,并创建文件夹config-repo(也可以不创建文件夹,配置会有一点不同)并上传3分文件,内容分别为:
- lyf-dev.properties 模拟开发环境
from=local-dev - lyf-test.properties 模拟测试环境
from=local-test - lyf-prod.properties 模拟生产环境
from=local-prod
2.服务端配置
pom.xml
<!-- config-server 服务配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
application.xml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/xianghaizing/spring-cloud-learn
search-paths: config-repo
server:
port: 8888
uri指定github地址search-paths指定配置搜索目录
Application.java
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
@EnableConfigServer 启动配置中心
3.配置和命名
1. 配置加载顺序
bootstrap.yml 高于 application.yml, bootstrap.yml一般用于加载远程配置
2. 客户端命名规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
一定要记住这个命名规则,不然客户端获取不到配置.这个坑困扰了我好几天,终于踩平了
application作为client的spring.application.name(必须一致)profile作为client的spring.cloud.config.profilelabel作为client的spring.cloud.config.label(默认master)
4.客户端配置
pom.xml
<!-- spring mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置中心client端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
application.yml
spring:
application:
name: lyf
cloud:
config:
uri: http://localhost:8888
profile: dev
label: master
server:
port: 8030
management:
endpoints:
web:
exposure:
include: refresh # 暴露刷新节点 2.x版本需要手动开启
Controller.java
@RefreshScope
@RestController
public class ConfigClientController {
@Value("${from}")
private String from;
@RequestMapping("/from")
public String getFrom(){
return this.from;
}
}
${from}就是远程properties文件中的from=dev中的那个key
5.获取远程配置
启动服务端和客户端
1.访问服务端
由于配置文件名为lyf-dev.properties,所以访问地址为 /lyf/dev
GET http://localhost:8888/lyf/dev
{
"name": "lyf",
"profiles": [
"dev"
],
"label": null,
"version": "9dd38a778c9b4ec9d7b8e972aeabe749e24e0161",
"state": null,
"propertySources": [
{
"name": "https://github.com/xianghaizing/spring-cloud-learn/config-repo/lyf-dev.properties",
"source": {
"from": "local-dev"
}
},
{
"name": "https://github.com/xianghaizing/spring-cloud-learn/config-repo/lyf.properties",
"source": {
"from": "local"
}
}
]
}
2.访问客户端
GET http://localhost:8030/from
local-dev
6.修改配置
修改dev配置: from=local-dev-02 并push到github
1.访问服务
GET http://localhost:8888/lyf/dev
{
"name": "lyf",
"profiles": [
"dev"
],
"label": null,
"version": "a89342009a315c78671b31a35392158673828bba",
"state": null,
"propertySources": [
{
"name": "https://github.com/xianghaizing/spring-cloud-learn/config-repo/lyf-dev.properties",
"source": {
"from": "local-dev-02"
}
},
{
"name": "https://github.com/xianghaizing/spring-cloud-learn/config-repo/lyf.properties",
"source": {
"from": "local"
}
}
]
}
2.访问客户端
GET http://localhost:8030/from
local-dev
此时客户端并没有拿到最新配置,需要手动发送post请求执行刷新
POST http://localhost:8030/actuator/refresh
[
"config.client.version",
"from"
]
再次访问
GET http://localhost:8030/from
local-dev-02
Spring Cloud 2-Config 分布式配置中心(七)的更多相关文章
- Spring Cloud(九):分布式配置中心和消息总线
我们在Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码 ...
- Spring Cloud之——Config(配置中心)
Spring Cloud Config(配置中心) 大家好,有一段时间没有写技术博客了.由于工作上的事情,这方面很难分配时间.近几年随着服务化的兴起,一批服务化的框架应运而生,像dubbo,thrif ...
- Spring Cloud(八):分布式配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- Spring Cloud 入门教程 - 搭建配置中心服务
简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...
- Spring Cloud 系列之 Apollo 配置中心(三)
本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) 本篇文章讲解 Apol ...
- Spring Cloud 系列之 Apollo 配置中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Apollo 配置中心(一) 本篇文章讲解 Apollo 部门管理.用户管理.配置管理.集群管理. 点击链接观看:Ap ...
- Spring Cloud 系列之 Apollo 配置中心(四)
本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) Spring Clou ...
- Spring Cloud Config 分布式配置中心使用教程
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- Spring Cloud Config 分布式配置中心【Finchley 版】
一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...
随机推荐
- Mysql数据库引擎介绍--转载
引用博文链接:https:/www.cnblogs.com/zhangjinghe/p/7599988.html MYSQL数据库引擎区别详解 数据库引擎介绍 MySQL数据库引擎取决于MySQL在安 ...
- 脚本安装Rocky版OpenStack 1控制节点+1计算节点环境部署
视频安装指南请访问: http://39.96.203.138/wordpress/document/%E8%84%9A%E6%9C%AC%E5%AE%89%E8%A3%85rocky%E7%89%8 ...
- Asp.Net Core中DI的知识总结
在asp.net core中DI的概念是由这几部分组成的: IServiceCollection,保存IServiceDescriptor实例的列表 IServiceProvider,只有一个方法Ge ...
- nginx的概念与几种负载均衡算法
Nginx的背景 Nginx和Apache一样都是一种WEB服务器.基于REST架构风格,以URI(Uniform Resources Identifier,统一资源描述符)或URL(Uniform ...
- rabbitmq 出现 com.rabbitmq.client.ShutdownSignalException: , ..................
-classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1. ...
- XXXX is not in the sudoers file. This incident will be reported解决方法
假设你用的是Red Hat系列(包括Fedora和CentOS)的Linux系统.当你执行sudo命令时可能会提示“某某用户 is not in the sudoers file. This inc ...
- nginx 返回json格式内容
例子: #如果访问的ip是192.168.1.1,就直接返回json格式的内容 location / { default_type application/json; #####格式 if ( $re ...
- [2019BUAA人工智能实战_陈泽寅]第1次个人作业
我们的征程是星辰和大海 一.索引 项目 内容 这个作业属于哪个课程 BUAA人工智能实战 这个作业的要求在哪里 here 我在这个课程的目标是 理论实践相结合,提升自己coding能力 这个作业在哪个 ...
- 偶写的第一个控件,一个用选择代替输入的Edit控件…
FDataSource :=TDataSource.Create(self); FDBGrid.FreeNotification(self); FADOQuery.FreeNotification(s ...
- java网页爬数据获取class中的空格
<ul class=""> <li class="avatar_img"><img src="http://avatar ...