一、概述

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配置中心的使用相关推荐

  1. (七)Alian 的 Spring Cloud Config 配置中心(客户端)

    目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景   通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...

  2. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  3. (六)Alian 的 Spring Cloud Config 配置中心(服务端)

    目录 一.简介 二.数据库 2.1.应用表 2.2.属性表 2.3.视图 2.4.初始化数据 三.配置 3.1.pom.xml 3.2.application.properties 3.3.主类 3. ...

  4. Spring Cloud Config 配置中心

    1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <p ...

  5. git在clone时需要输入密码Enter passphrase for key 导致spring cloud config 配置中心无法拉取配置文件的解决方法

    前几天把系统从win7换到了win10 重装了开发环境 一直没什么问题 今天在调试spring cloud 时 发现无论如何都拉取不到配置文件, 通过微服务日志提示 Could not locate ...

  6. 【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. ...

  7. Spring Cloud Config配置中心使用(草稿版)

    服务端搭建: cli:

  8. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  9. 【夯实Spring Cloud】Spring Cloud分布式配置中心详解

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

最新文章

  1. echarts 自定义图表
  2. vivox21升级鸿蒙,vivo X21i相机规格再升级,加持AI成又一拍照神器
  3. 簡單編譯內核 linux kernel gnu
  4. python 竖线 绘图_Python可视化 | Seaborn5分钟入门(二)——barplot countplot
  5. 后面的 飞鸽传书 l代表lock
  6. 最近让我焦灼的四个问题
  7. 设计模式-Builder Pattern
  8. js obj对象转formdata格式代码
  9. 【数据结构笔记】哈夫曼树的构造算法
  10. 从零开始的JAVA反序列化漏洞学习(一)
  11. 计算机考研408每日一题 day158
  12. 躺平国历险记:两个超平面之间距离的计算
  13. 2019届网易云音乐前端实习生电面心得
  14. Linux驱动学习12(初步认识内存管理)
  15. icc校色文件使用教程_Windows7色彩管理显示器ICC设置方法
  16. Android 音频焦点处理
  17. windows、mac桌面录制GIF
  18. 一起自学SLAM算法:11.5 强化学习与自主导航
  19. Elastic开源社区:开发者招募
  20. 主流部署端深度学习框架

热门文章

  1. DotNet并行计算的使用误区(二)
  2. 深入探讨下Linux下修改hostname的五个问题(四)
  3. 饥荒 死亡后不删存档的办法
  4. 艾伟_转载:C# Design Patterns (4) - Proxy
  5. 【转载】网络视频企业探索盈利模式 PPS研发新展示系统
  6. 阿里热更新android,阿里最新热更新使用采坑记录
  7. jsp java el_jsp之EL表达式
  8. IE8不能正常登录网上银行的解决方法
  9. Linux下软件安装方法汇总
  10. AndroidStudio更换黑色主题方法