一、概述

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-config
profiles:
active: dev
#配置中心
cloud:
config:
server:
git:
uri: https://gitee.com/wslook/test-config-repo.git
search-paths: user  //配置文件目录,多个用逗号隔开
username: xxx
password: xxx
default-label: master
basedir: ./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-config
profile: dev
label: master
discovery:
enabled: true
serviceId: test-config
fail-fast: true # 注册中心
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2181/eureka/

3. 测试

编写测试代码:

@RequestMapping("/test")
@RestController
public class TestController { @Resource
private OSSProperties ossProperties; @RequestMapping("/config")
public String test(){
return ossProperties.getUrl();
}
}

启动user服务,可以看到,已经把配置拉取下来了

使用postman验证

四、高可用

对于config集群,很简单,因为由注册中心(这里使用的eureka)统一管理服务,所以不需要额外的配置,只需多启动几台config Server服务即可

Spring Cloud Config配置中心的使用的更多相关文章

  1. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  2. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  3. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  6. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  7. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

  8. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

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

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

  10. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

随机推荐

  1. PHP网站自动化配置的实现

    一直都在用yii2做项目,有在用自动化配置,但是没有自己去配置过.中午没事去看了下yii的初始化代码,发现都是php而已! yii2初始化项目代码 所以,我们做项目肯定是可以用php做的,于是我新建了 ...

  2. MYeclipes项目导入导出

    导入: 右键,import,

  3. JS在项目中用到的AOP, 以及函数节流, 防抖, 事件总线

    1. 项目中在绑定事件的时候总想在触发前,或者触发后做一些统一的判断或逻辑,在c#后端代码里,可以用Attribute, filter等标签特性实现AOP的效果,可是js中没有这种用法,归根到本质还是 ...

  4. vue url生产二维码

    <template> <div id="QRcode"> <div class='QR-qrcode' style='display:none;'&g ...

  5. Documentation/kobject.txt翻译--sysfs

    你从未想过的关于kobjects,ksets和ktypes的一切:作者:Greg Kroah-Hartman <gregkh@linuxfoundation.org>:上次更新时间:200 ...

  6. nexus helm proxy 集成&&问题解决

      对于使用kubernetes 进行开发的人员来说helm是很方便的 构建nexus helm plugin git clone https://github.com/sonatype-nexus- ...

  7. 【C++11】新特性 之 auto的使用

      C++11中引入的auto主要有两种用途:自己主动类型判断和返回值占位.auto在C++98中的标识暂时变量的语义,因为使用极少且多余.在C++11中已被删除.前后两个标准的auto,全然是两个概 ...

  8. ETA6093 或 ETA9741 ETA9742 的 TYPE-C 的资料收集

    ETA6093 或 ETA9741 ETA9742 的 TYPE-C 的资料收集 因为项目使用. 这个 IC 好玩,但是还是有一些需要注意的. 对我有用的信息. http://www.great-et ...

  9. spring与hibernate注解及XML方式集成

    spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...

  10. clamav完整查杀linux病毒实战(转)

    开篇前言 Linux服务器一直给我们的印象是安全.稳定.可靠,性能卓越.由于一来Linux本身的安全机制,Linux上的病毒.木马较少,二则由于宣称Linux是最安全的操作系统,导致很多人对Linux ...