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. nginx配置默认首页(index.htnl index.htm)全流程(包含遇到问题的解决)

    需求: 自己有个域名,原来直接扔在了服务器的文件夹里(根据客服人员指导),自己玩了一遍nginx的安装部署等操作之后,域名的指向发生了改变,到了nginx成功的界面. 自己抱着极大的好奇心来配置ngi ...

  2. Linux基础优化与安全归纳总结

    一名运维工程师在运维岗位上时间久了,就会发现Linux优化的重要性,同时会给运维工作带来很多的便利性.本人逐渐认识到了这一点,所以特意在工作闲暇之余,通过阅读Linux相关书籍及向同事.同行高手咨询, ...

  3. Tomcat FAIL - Deploy Upload Failed, Exception: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (110960596) exceeds the confi

    https://maxrohde.com/2011/04/27/large-war-file-cannot-be-deployed-in-tomcat-7/ Go to the web.xml of ...

  4. (三)jdk8学习心得之方法引用

    三.方法引用 https://www.jianshu.com/p/c9790ba76cee 这边博客写的很好,可以首先阅读,在这里感谢这篇文章的博主. 1. 格式 调用者::调用者具备的方法名 2. ...

  5. git submoudle提交

    进入到各个submoudle文件夹 git status 查看所在branch和文件修改状态 git add [files]; git commit "" git pull ori ...

  6. LODOP打印用JS获取的当前日期

    该文详细一步步解释JS获取当前时间的方法,新手小白也看到懂,最后是实际的获取当前年月份的方法.JS中的Date()对象,包含很多当前系统时间的方法,首先建立一个Date()对象,这里取名为date,然 ...

  7. Luogu4494 [HAOI2018]反色游戏 【割顶】

    首先发现对于一个联通块有奇数个黑点,那么总体来说答案无解.这个很容易想,因为对每个边进行操作会同时改变两个点的颜色,异或值不变. 然后一个朴素的想法是写出异或方程进行高斯消元. 可以发现高斯消元的过程 ...

  8. Linux haproxy基础

    代理作用 web缓存,提供缓存功能,可以加速响应过程. 反向代理,可以隐藏后端服务器 内容路由,可把不同内容类型的请求转发至特定服务器, 转码器,与客户端通信,由于带宽限制,可将报文转码压缩:与后端服 ...

  9. redis-cli 通过管道 --pipe 快速导入数据到redis中

    最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入 一:先看看命令 cat redis.txt | redis-cli -h 12 ...

  10. 第二周java学习总结

    学号 20175206 <Java程序设计>第二周学习总结 教材学习内容总结 第二章是基本数据类型与数组,第三章是运算符.表达式和语句的内容.如果说第一章是让我们了解JAVA,那么第二章和 ...