Spring Cloud  Config 

服务架构

1.github配置

通过github作为分布式配置中心,在github上新建仓库:spring-cloud-learn,并创建文件夹config-repo(也可以不创建文件夹,配置会有一点不同)并上传3分文件,内容分别为:

  1. lyf-dev.properties 模拟开发环境from=local-dev
  2. lyf-test.properties 模拟测试环境from=local-test
  3. 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.profile
  • label 作为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 分布式配置中心(七)的更多相关文章

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

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

  2. Spring Cloud之——Config(配置中心)

    Spring Cloud Config(配置中心) 大家好,有一段时间没有写技术博客了.由于工作上的事情,这方面很难分配时间.近几年随着服务化的兴起,一批服务化的框架应运而生,像dubbo,thrif ...

  3. Spring Cloud(八):分布式配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  4. Spring Cloud 入门教程 - 搭建配置中心服务

    简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...

  5. Spring Cloud 系列之 Apollo 配置中心(三)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) 本篇文章讲解 Apol ...

  6. Spring Cloud 系列之 Apollo 配置中心(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Apollo 配置中心(一) 本篇文章讲解 Apollo 部门管理.用户管理.配置管理.集群管理. 点击链接观看:Ap ...

  7. Spring Cloud 系列之 Apollo 配置中心(四)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) Spring Clou ...

  8. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  9. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

随机推荐

  1. SFP光模块与SFP+、XFP、QSFP、GBIC、BIDI的区别

    SFP.SFP+.XFP.QSFP.GBIC和BIDI等不同封装类型光模块不断推陈出新,我们就以市场上比较常见的为主,来谈谈它与其他类似光模块的区别. SFP光模块 SFP光模块又称⼩封装可插拔光模块 ...

  2. [kuangbin带你飞]专题二十二 区间DP-E-POJ - 1651

    区间DP模板题 做区间DP的题目的时候,我们考虑DP[i][j]的含义是什么? 由题意大概是这样的,我们可以从n个数中每次选一个我们以前没选过的数字拿走,需要消耗a[i]*a[i+1]*a[i-1]的 ...

  3. Python----简单线性回归

    简单线性回归 1.研究一个自变量(X)和一个因变量(y)的关系   简单线性回归模型定义:y=β0+β1x+ε 简单线性回归方程:E(y)=β0+β1x 其中: β0为回归线的截距 β1为回归线的斜率 ...

  4. QQ浏览器、火狐浏览器中页面有点大的问题记录

    做页面时候,发现火狐和腾讯QQ浏览器有个问题,就是会将页面显示的比较大,像点了缩放比例120%似的,事实上缩放比例是100%,很奇怪. 甚至面对这个问题,连腾讯公司主页也会放大,也让我很困惑. 比如: ...

  5. 4月22日MySQL学习

    前面学习的知识基本都是概念知识没有什么代码,然后还有图形界面来辅助学习. 今天学习了MySQL的存储引擎,最常用的两种 MYISAM:不支持事务,也不支持外键,但是访问速度快. INNODB:支持事务 ...

  6. Power BI免费版(Free),专业版(Pro)以及增值版(Premium)授权功能对比, Server

    Features of Power BI Report Server and the Power BI service Features Power BI Report Server Power BI ...

  7. odoo 配置文件参数大全

    odoo 数据库配置文件参数 [options] ; addons模块的查找路径 addons_path = E:\GreenOdoo8.0\source\openerp\addons ; 管理员主控 ...

  8. [算法]浅谈求n范围以内的质数(素数)

    汗颜,数学符号表达今天才学会呀-_-# 下面是百度百科对质数的定义 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 求质数的方法 ...

  9. 【CSA35G】【XSY3318】Counting Quests DP 拉格朗日反演 NTT

    题目大意 zjt 是个神仙. 一天,zjt 正在和 yww 玩猜数游戏. zjt 先想一个 \([1,n]\) 之间的整数 \(x\),然后 yww 开始向他问问题. yww 每次给 zjt 一个区间 ...

  10. Mac anaconda安装 “conda command not found” 解决方法

    官网下载包直接安装的时候可能会产生这种问题,这主要还是环境变量配置的问题 一般我们添加环境变量的方法是编辑.bash_profile或.bashrc,在文件里插入下面这段代码 export PATH= ...