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. 使用栈实现队列(1)(Java)

    class MyQueue { private Stack s1; private Stack s2; public MyQueue(int size) { this.s1 = new Stack(s ...

  2. Python----多项式回归

    多项式线性回归 1.多项式线性方程: 与多元线性回归相比,它只有一个自变量,但有不同次方数. 2.举例: import numpy as np import matplotlib.pyplot as ...

  3. Python操作Excel 之 openpyxl

    一.基础 安装openpyxl 模块 pip install openpyxl 或者通过轮子安装 font(字体类):字号.字体颜色.下划线等 fill(填充类):颜色等 border(边框类):设置 ...

  4. react中的this.setState()

    修改组件的状态可以使用的一些方法: 1.比较常用的 this.setState({ message:"你好" }) 2.state更新是异步的时候 因为this.props和thi ...

  5. C语言中结构体内存对齐

    先写一个小程序: #include<stdio.h> struct student  {    int a;   char k;   short m; }; int main() { st ...

  6. noip2017部分题目

    D1T3 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张NN个点MM条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,NN号点是公园的出口,每条边有一个非负权值, 代表策策经过 ...

  7. 【BZOJ4001】[TJOI2015]概率论(生成函数)

    [BZOJ4001][TJOI2015]概率论(生成函数) 题面 BZOJ 洛谷 题解 这题好仙啊.... 设\(g_n\)表示\(n\)个点的二叉树个数,\(f_n\)表示\(n\)个点的二叉树的叶 ...

  8. 查看macOS下正在使用的zsh

    使用dscl . -read /Users/$USER UserShell查看 如果你的结果是/bin/zsh,又恰巧用brew安装了zsh的话,那么你可能就白安装了 将brew安装的zsh添加到/e ...

  9. postman随机生成数

    postman做重复测试时,随机数就有很大的作用,不用每次都输入 在postman的Params中,输入一个左大括号,会显示三种随机数: 也可以在body中设置 随机数如下: {{$guid}}:添加 ...

  10. mac下安装gradle

    安装 gradle brew install gradle 配置Path环境 vim .bash_profile // 写入 export PATH="${PATH}:/usr/local/ ...