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. dede织梦 arclist标签完美支持currentstyle属性

    由于客户需求,所以进行对文章的arclist标签进行设置当前样式(currentstyle),修改前记得备份. dede版本v5.7sp 找到PHP修改: include/taglib/arclist ...

  2. CentOS配置svn

    参考: https://www.cnblogs.com/taohaijun/p/7172939.html 1.检查已安装版本  rpm -qa subversion 卸载旧版本SVN yum remo ...

  3. .netcore2.0发送邮件

    SmtpClient smtpClient = new SmtpClient(); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指 ...

  4. 使用sqlserver 链接远程服务器进行查询

    --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' exec sp_addlinkedsrvlogi ...

  5. Base 64 & decodeURIComponent

    Base 64 & decodeURIComponent js btoa() & atob() let obj = [{"key":"q",&q ...

  6. 【很好的分享】zookeeper系列

    http://blog.csdn.net/tswisdom/article/details/41522069

  7. vue组件之间的通信

    1.父组件给子组件传递数据 <body> <div id="app"> 父组件:{{total}} <br> <son-component ...

  8. SpringBoot配置activemq消息队列

    1.配置pom相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  9. Java逆向工程SpringBoot + Mybatis Generator + MySQL

    Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...

  10. [CIDR]calculate CIDR network capacity / 计算CIDR子网的网络容量

    题目: 答案: 过程: 以B为例,192.168.9.140/27 /27 掩码是 8 8 8 111 00000 192.168.9.140是这个网段里的一个地址 140是 10001100 所以这 ...