介绍

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

什么是配置中心?

当微服务过多的时候,每个微服务的配置很难集中管理。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. Docker下的资源限制问题

    Docker下的资源限制问题 问题背景 公司某产品出现了一个奇怪的OOM 错误提示. 问题现象是 前台产品 提示 OOM cannot create native thread 但是同时查看 机器的资 ...

  2. [转帖]DOCKER默认网段和主机网段冲突解决

    https://www.cnblogs.com/yinliang/p/13189334.html 一. docker默认网卡docker0 172.17.0.0可能会与主机冲突,这时候需要修改dock ...

  3. [转帖]Linux:crontab要点整理(表达式,转义,权限管理,日志)

    https://www.jianshu.com/p/fd46652f247e 摘要:Linux,crontab整理crontab的使用,包括cron表达式,设置和删除任务,权限管理,查看日志 cron ...

  4. 【转帖】Mysql一张表可以存储多少数据

    https://www.cnblogs.com/wenbochang/p/16723537.html Mysql一张表可以存储多少数据 在操作系统中,我们知道为了跟磁盘交互,内存也是分页的,一页大小4 ...

  5. 【转帖】python 安装whl文件

    前言 WHL文件是以Wheel格式保存的Python安装包,Wheel是Python发行版的标准内置包格式.在本质上是一个压缩包,WHL文件中包含了Python安装的py文件和元数据,以及经过编译的p ...

  6. [转帖]Zen4架构+5nm制程+96核心 第四代AMD EPYC处理器强势来袭

    https://new.qq.com/rain/a/20221111A098QE00   不得不承认,技术的持续突破和迭代,使得AMD处理器在近年来得到了"喷气机式"的增长,无论是 ...

  7. Nginx的再学习

    第一部分 Nginx的版本 Nginx官网提供了三个类型的版本 Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版 Stable version:最 ...

  8. 数据结构与算法 第二章线性表(48课时课程笔记)Data Structure and Algorithms

    2.1 线性表的类型定义 一个线性表是n个数据元素的有限序列. (1)结构初始化 InitList(&L) 构造一个空的线性表L. (2)销毁结构 DestroyList(&L) (3 ...

  9. Leetcode 42题 接雨水(Trapping Rain Water) Java语言求解

    题目链接 https://leetcode-cn.com/problems/trapping-rain-water/ 题目内容 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱 ...

  10. [2] 以逆向的角度来看流程控制语句——switch

    [2] 以逆向的角度来看流程控制语句--switch 1. switch分支数小于4 汇编标识: 00401021 mov [ebp-4], ecx 00401024 cmp dword ptr [e ...