SpringCloudConfig分布式配置中心-基本使用
一、基本使用
1. Config-Server端
(1)pom:
- parent依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version> </parent>
- dependencyManagement
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>
- dependencies
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency> </dependencies>
查看全部pom代码
(2)启动类:增加注解@EnableConfigServer,当然@SpringBootApplication也是必不可少的
查看全部启动类代码
(3)application.yml
如果用git作配置仓库,则:
spring:cloud:config:server:git:uri: https://gitee.com/LOVE0612/SpringCloud.gitusername: 562759534@qq.compassword: 123@abc
- 如果希望ConfigServer启动的时候就将配置文件clone下来,则可增加配置:spring.cloud.config.server.git.clone-on-start=true。
- 如果配置文件不在git仓库的根目录下, 例如:如果配置文件在git仓库的properties目录下,则可增加配置spring.cloud.config.server.git.search-paths=properties。当有多个目录时逗号分隔。
- 如果需要制定git clone后在本地存储位置,则可增加配置spring.cloud.config.server.git.baseDir。
如果用svn作配置仓库,则:
spring:cloud:config:server:svn:uri: https://192.168.1.111/svn/SpringCloudusername: luyanchaopassword: abc@123
- 同样存在spring.cloud.config.server.svn.search-paths配置项,使用方法同git。
- 同样存在spring.cloud.config.server.git.baseDir配置项,使用方法同git。
如果使用本地文件系统作配置仓库,则:
spring:cloud:config:server:native:search-locations: propertiesprofile: native
- search-locations:可以是绝对路径也可以是classpath路径。
- 使用本地文件系统作为配置仓库的最大缺点在于:如果ConfigServer多点部署以保证高可用时,需要将文件系统进行共享。
如果使用多种/多个配置仓库,则:
spring:cloud:config:server:git:uri: https://gitee.com/LOVE0612/SpringCloud.gitusername: 562759534@qq.compassword: 1226ai0612order: 1svn:uri: https://192.168.1.111/svn/SpringCloudusername: luyanchaopassword: abc@123order: 2native:search-locations: propertiesorder: 3profile: native, git, svn
- profile:设定哪些配置仓库可用,逗号分隔
- order:配置仓库的优先级顺序,数字越低优先级越高
server:port: 51011spring:application:name: ConfigServercloud:config:server:git:uri: https://gitee.com/LOVE0612/SpringCloud.gitsearch-paths: propertiesusername: 562759534@qq.compassword: 123@abcclone-on-start: true
(3)run,并测试
ConfigServer提供多种rest接口访问配置信息,可通过run日志查看:
Mapped "{[/{name}-{profiles}.properties],methods=[GET]}" onto ... Mapped "{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}" onto ... Mapped "{[/{name}/{profiles:.*[^-].*}],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.properties],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.json],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}" onto... Mapped "{[/{name}/{profiles}/{label:.*}],methods=[GET]}" onto ... Mapped "{[/{name}-{profiles}.json],methods=[GET]}" onto ... Mapped "{[/{name}/{profile}/{label}/**],methods=[GET],produces=[application/octet-stream]}" onto ... Mapped "{[/{name}/{profile}/{label}/**],methods=[GET]}" onto ... Mapped "{[/{name}/{profile}/**],methods=[GET],params=[useDefaultLabel]}" onto...
- 参数name:即ConfigClient中定义的spring.application.name的值,后续会讲到
- 参数profiles:这个不需要多说,用过SpringBoot的应该都知道
- 参数label:如果是git或svn,label对应的是分之;如果是本地文件系统,label对应子目录名,例如:classpath:properties/{label}
2. Config-Client端
(1)pom:
- parent依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version> </parent>
- dependencyManagement
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>
- dependencies
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency> </dependencies>
(2)启动类:有@SpringBootApplication即可
(3)bootstrap.yml,注意这里不是application.yml哦
spring:application:name: ServiceGatewaycloud:config:uri: http://127.0.0.1:51011profile: company
- spring.application.name:项目名称
- spring.cloud.config.uri:ConfigServer的地址
- spring.cloud.config.profile:这个不需要多说,用过SpringBoot的应该都知道
根据以上配置内容,可得知最终调用ConfigServer的接口地址为:http://127.0.0.1:51011/ServiceGateway-company.yml
3. 多个Config-Client端公用配置
如果多个Config-Client中有部分配置是相同的,为了避免出现重复代码,我们可以把这部分相同的配置抽出来单独做一个配置文件,命名为:application.yml、application.properties,或application-*.yml、application-*.properties,然后放到Config-Server端即可。
SpringCloudConfig分布式配置中心-基本使用相关推荐
- SpringCloud-config分布式配置中心
为什么要统一管理微服务配置? 随着微服务不断的增多,每个微服务都有自己对应的配置文件.在研发过程中有测试环境.UAT环境.生产环境,因此每个微服务又对应至少三个不同环境的配置文件.这么多的配置文件,如 ...
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
上一篇文章,留了一个悬念,Config Client 实现配置的实时更新,我们可以使用 /refresh 接口触发,如果所有客户端的配置的更改,都需要手动触发客户端 /refresh ,当服务越来越多 ...
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...
- Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config,它支持配 ...
- Java B2B2C o2o多用户商城 springcloud架构 (六)分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)...
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f6-config/ 本文出自方志朋的博客 在上一篇文章讲述z ...
- Apollo分布式配置中心入门
一.概述 1.什么是配置 应用程序在启动和运行的时候往往需要读取一些配置信息,配置基本上伴随着应用程序的整个生命周期,比如:数据库连接参数.启动参数等. 配置主要有以下几个特点: 配置是独立于程序的只 ...
- SpringCloud教程-分布式配置中心Config (SpringCloud版本Greenwich.SR4)
文章目录 Config(分布式配置中心)简介 创建服务端ConfigServer 创建客户端config-client 代码地址: github-spring-cloud地址 Config(分布式配置 ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
转:https://blog.csdn.net/forezp/article/details/70037291 最新版本: 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spr ...
最新文章
- Windows下安装ElasticSearch6.3.1以及Head插件
- brave counts rather people who are at high position
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决
- vc 控制台添加托盘显示_开源:ESP8266读DHT11温湿度,小程序实时显示
- (王道408考研操作系统)第二章进程管理-第三节6:经典同步问题之生产者与消费者问题
- (3)通过输入参数(测量数据)构建三维体模型(02)
- java 对象与引用_Java --对象引用与对象的区别
- IntelliJ IDEA 2020.1 首个稳定版重磅发布!
- Atitit 锁的不同层级 app锁 vm锁 os锁 硬件锁 目录 1. 在硬件层面,CPU提供了原子操作、关中断、锁内存总线的机制	1 1.1. test and set指令	1 1.2. 锁内
- linux命令psd,Linux 下查看 Photoshop PSD 文件
- 推荐一款十分好用的本地查词软件——MDict
- 。成功实现avd 模拟器 与pc 虚拟串口实现通信 通过多方文章综合
- 轻量级纯CSS框架,11款最轻量级的CSS框架
- 详谈!抖音蓝V认证的常见问题总结
- C语言中pthread或Windows API在多线程编程中的基本应用
- 房贷压力那么大,当房奴那么累,为什么很多年轻人还贷款买房?
- Acme Corporation UVA11613 网络流
- Apache Shiro 框架简介
- Android热更新技术的研究与实现Sophix
- linux的每次IO大小控制,Linux优化之IO子系统监控与调优
热门文章
- Leetcode每日一题:226.invert-binary-tree(翻转二叉树)
- 集合及其常见操作,创建,增加,删除,查找
- Redis基础(八)——集群
- 西瓜书+实战+吴恩达机器学习(十六)半监督学习(半监督SVM、半监督k-means、协同训练算法)
- webpack 4.0 中 clean-webpack-plugin 的使用
- 第六次 Scrum Meeting
- Android的滑动分析
- android本地图片,Android中ImageView实现选择本地图片并显示功能
- java线程池 core_Java 线程池 ThreadPoolExecutor 的使用
- linux中怎么创建管道文件,Linux 管道文件