介绍

本文以理论结合实践编写,篇幅较长,各位看官保持耐心:),部分内容引用自网络。

什么是配置中心?

当微服务过多的时候,每个微服务的配置很难集中管理。SpringCloud Config通过git代码托管来实现配置的集中管理。实现配置中心客户端获取远程的配置文件,并可以动态刷新,即时生效。

如何使用?

SpringCloud Config分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

构建服务端

1.新建模块

添加模块:cloud-config-server3344

2.修改pom.xml

添加如下依赖:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

3.在码云上添加配置仓库

登录码云,新建仓库:config-server,新增如下2个yml文件:

内容自定义即可。

4.修改application.yml

注意此处把配置中心微服务注册到了eureka,后续文章会讲到。

server:
port: 3344 spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://gitee.com/indexman/config-server.git #上面创建的git仓库地址 #服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

5.新建启动类

创建启动类:ConfigCenterMain3344

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigCenterMain3344 {
public static void main(String[] args){
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}

6.启动测试

依次访问:localhost:3344/order-dev.ymllocalhost:3344/order-prod.yml,浏览器分别返回:

msg: this is order-dev config.
msg: this is order-prod config.

说明配置成功!

7.补充知识点

  • 如果仓库有多个文件夹,可配置搜索路径:
search-paths: /**  # 指定搜索根路径下的所有目录,若有多个路径使用逗号隔开
  • 如果配置中心为私有仓库,需要配置git用户名和密码:
username: username  # 以及相应的账户名
password: password # 和密码
  • 如果需要本地存储配置仓库,需要配置basedir:
basedir: E:\Java_IDEA\config\basedir  # 可以使用这个配置项来指定本地git仓库的路径
  • 具体的微服务配置可通过如下规则获得:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

构建客户端

1.新建模块

新建模块:cloud-config-client-3355

2.修改pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

3.修改application.yml

server:
port: 3355 spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: order #配置文件名称
profile: dev #读取后缀名称
uri: http://localhost:3344 #配置中心地址k #服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

4.新建启动类

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args){
SpringApplication.run(ConfigClientMain3355.class, args);
}
}

5.新建测试类

@RestController
public class ConfigClientController {
@Value("${msg}")
private String msg; @GetMapping("/configInfo")
public String say(){
return msg;
}
}

6.启动测试

启动后访问:localhost:3355/configInfo

this is order-dev config.

存在问题

以上所示,服务端和客户端都配置和测试通过。此时我去修改码云仓库中的配置会出现什么情况?

服务端和客户端还能实时更新配置吗?


答案是:

  • 服务端实时刷新没问题
  • 客户端在不重启的情况下无法实时更新

那么如何解决?请看下一篇文章:《SpringCloud Config配置中心动态刷新实战》

SpringCloud Config配置中心实战的更多相关文章

  1. spring-cloud config配置中心

    这里那些概念不说,主要是记录下spring cloud config配置中心的服务端和客户端的一个demo. 服务端即提供统一配置文件 客户端即从服务端读取配置 1.新建一个spring boot项目 ...

  2. SpringCloud Config(配置中心)实现配置自动刷新(十六)

    一.实现原理 1.ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应 配置文件: 2.当远 ...

  3. SpringCloud Netflix (六):Config 配置中心

    ------------恢复内容开始------------ SpringCloud Config 配置中心 Config 配置中心 Spring Cloud Config为分布式系统中的外部化配置提 ...

  4. SpringCloud的入门学习之概念理解、Config配置中心

    1.SpringCloud Config分布式配置中心.分布式系统面临的配置问题. 答:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...

  5. SpringCloud配置中心实战

    SpringCloud配置中心实战 1.统一配置中心(Config) 1.1 Spring项目配置加载顺序 1.2 配置规则详解 1.3 Git仓库配置 1.3.1 使用占位符 1.3.2 模式匹配 ...

  6. springcloud之config配置中心-Finchley.SR2版

    本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞: ...

  7. 七、springcloud之配置中心Config(二)之高可用集群

    方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...

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

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

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

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

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

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

随机推荐

  1. SpringMVC02——第一个MVC程序-注解版(high版!!!!)

    注解版 新建一个子项目,添加Web支持 在pom.xml文件中引入相关的依赖:主要引入Spring框架核心库.SpringMVC.servlet,JSTL等. 创建实体类Fruit package c ...

  2. 海思Hi35xx 通过uboot查看flash指定地址的数据

    ​ 前言 在实际应用中有遇到过设备放置一段时间后设备不能启动的问题,uboot 完全没有响应,类似于flash中的数据被擦洗掉一样. 网上有介绍说是nandflash 不稳定,高温或是静电会导致nan ...

  3. IL合集二

    引言 在第一篇关于IL的文章中,我们写了一些IL的相加,创建对象,循环以及实现TryCatch的一些功能,接下来,为大家带上后续关于IL的更新,其中包括,类型转换,以及条件判断,还有定义字段,定义属性 ...

  4. [转帖]/dev/random 和 /dev/urandom的一点备忘

    https://www.cnblogs.com/ohmygirl/p/random.html 1.  基本介绍 /dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两 ...

  5. [转帖]探索惊群 ③ - nginx 惊群现象

    https://wenfh2020.com/2021/09/29/nginx-thundering-herd/    nginx  kernel 本文将通过测试,重现 nginx(1.20.1) 的惊 ...

  6. ELK运维文档

    Logstash 目录 Logstash Monitoring API Node Info API Plugins Info API Node Stats API Hot Threads API lo ...

  7. vivo 海量基础数据计算架构应用实践

    作者:来自 vivo 互联网大数据团队 本文根据刘开周老师在"2023 vivo开发者大会"现场演讲内容整理而成.公众号回复[2023 VDC]获取互联网技术分会场议题相关资料. ...

  8. 浅谈kafka

    作者:京东科技 徐拥 入门 1.什么是kafka? apache Kafka is a distributed streaming platform. What exactly dose that m ...

  9. PicoPixel贴图查看器

    Pico Pixel Pico Pixel是一款纹理查看器,支持查看以下文件格式:TGA,BMP,JPG,DDS,PNG,OpenEXR, KTX, HDR, GIF, TIF. 此外,Pico Pi ...

  10. Unity2019使用Gradle命令行(编译)出安卓包

    在我所经历的项目组中有这几种方法来生成APK 直接在Unity生成APK,可以接入SDK 使用Unity导出Android Studio工程手动生成APK 使用Unity导出Android Studi ...