Spring Cloud Config配置中心的使用
一、概述
1. 为什么使用?
1> 配置文件太多,不方便维护
2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人
3> 更新配置项目需重启,试想想,在生产环境,那么多台机器。。。
2. config介绍
config分为Server端和Client端,实现原理如下图所示:
- Server端负责从远端git(码云、GitHub等)拉取配置,并缓存在本地;
- Client端(上图的product和order服务)在启动时,从Server端本地缓存中获取配置
二、Server端配置
1. 新建config Server模块,加载依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId> </dependency>
2. 在启动类上@EnableConfigServer注解,开启configServer
@EnableConfigServer //开启configServer @SpringBootApplication @EnableDiscoveryClient //开启Eureka Client public class TestConfigApplication {public static void main(String[] args) {SpringApplication.run(TestConfigApplication.class, args);} }
3. 在远端git上新建项目(这里使用码云),并把配置上传上去,具体操作略
说明:config语法规定,xxx.yml为公共配置,在拉取配置时会和xxx.{}profiles}.yml合并
4. 修改配置文件
spring:application:name: test-configprofiles:active: dev#配置中心cloud:config:server:git:uri: https://gitee.com/wslook/test-config-repo.gitsearch-paths: user //配置文件目录,多个用逗号隔开username: xxxpassword: xxxdefault-label: masterbasedir: ./configRepo/ //本地缓存地址force-pull: true //强制拉取配置,解决手动修改本地缓存配置后,无法拉取最新配置的问题
# 注册中心eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:2181/eureka/
5. 测试
三、Client端配置
1. 加载依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId> </dependency>
2. 修改配置文件(把配置文件名改为bootstrap.yml)
spring: # 配置中心cloud:config:name: user-configprofile: devlabel: masterdiscovery:enabled: trueserviceId: test-configfail-fast: true# 注册中心 eureka:instance:prefer-ip-address: trueclient:service-url:defaultZone: http://localhost:2181/eureka/
3. 测试
编写测试代码:
@RequestMapping("/test") @RestController public class TestController {@Resourceprivate OSSProperties ossProperties;@RequestMapping("/config")public String test(){return ossProperties.getUrl();} }
启动user服务,可以看到,已经把配置拉取下来了
使用postman验证
四、高可用
对于config集群,很简单,因为由注册中心(这里使用的eureka)统一管理服务,所以不需要额外的配置,只需多启动几台config Server服务即可
转载于:https://www.cnblogs.com/wslook/p/9994251.html
Spring Cloud Config配置中心的使用相关推荐
- (七)Alian 的 Spring Cloud Config 配置中心(客户端)
目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景 通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...
- Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...
- (六)Alian 的 Spring Cloud Config 配置中心(服务端)
目录 一.简介 二.数据库 2.1.应用表 2.2.属性表 2.3.视图 2.4.初始化数据 三.配置 3.1.pom.xml 3.2.application.properties 3.3.主类 3. ...
- Spring Cloud Config 配置中心
1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <p ...
- git在clone时需要输入密码Enter passphrase for key 导致spring cloud config 配置中心无法拉取配置文件的解决方法
前几天把系统从win7换到了win10 重装了开发环境 一直没什么问题 今天在调试spring cloud 时 发现无论如何都拉取不到配置文件, 通过微服务日志提示 Could not locate ...
- 【SpringCloud】Spring Cloud Config 配置中心
文章目录 1.概述 1.1 是什么 1.2 怎么玩 1.3 怎么用 3.案例 3.1 案例1 3.1.1 配置 3.1.2 主类 3.1.3 Git信息 3.1.4 访问 4. 例子 5.客户端 5. ...
- Spring Cloud Config配置中心使用(草稿版)
服务端搭建: cli:
- 玩转Spring Cloud之配置中心(config server config client)
玩转Spring Cloud之配置中心(config server &config client) 本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...
- 【夯实Spring Cloud】Spring Cloud分布式配置中心详解
本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...
最新文章
- echarts 自定义图表
- vivox21升级鸿蒙,vivo X21i相机规格再升级,加持AI成又一拍照神器
- 簡單編譯內核 linux kernel gnu
- python 竖线 绘图_Python可视化 | Seaborn5分钟入门(二)——barplot countplot
- 后面的 飞鸽传书 l代表lock
- 最近让我焦灼的四个问题
- 设计模式-Builder Pattern
- js obj对象转formdata格式代码
- 【数据结构笔记】哈夫曼树的构造算法
- 从零开始的JAVA反序列化漏洞学习(一)
- 计算机考研408每日一题 day158
- 躺平国历险记:两个超平面之间距离的计算
- 2019届网易云音乐前端实习生电面心得
- Linux驱动学习12(初步认识内存管理)
- icc校色文件使用教程_Windows7色彩管理显示器ICC设置方法
- Android 音频焦点处理
- windows、mac桌面录制GIF
- 一起自学SLAM算法:11.5 强化学习与自主导航
- Elastic开源社区:开发者招募
- 主流部署端深度学习框架