介绍

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

什么是配置中心?

当微服务过多的时候,每个微服务的配置很难集中管理。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. 聊聊x86计算机启动发生的事?

    大家好,我是呼噜噜,最近在看linux早期内核0.12的源码,突然想到一个困扰自己好久的问题:当我们按下电源键,计算机发生了什么?神秘地址0x7C00究竟是什么?操作系统又是如何被加载到硬件中的?带着 ...

  2. [转帖]Linux内核线程kthread简介【最好的一篇!】

    https://zhuanlan.zhihu.com/p/581587583 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求).内核需要多个执行流并行,为了 ...

  3. [转帖]Linux下使用 ipset 封大量IP及ipset参数说明

    https://www.cnblogs.com/xiaofeng666/p/10952627.html Linux使用iptables封IP,是常用的应对网络攻击的方法,但要封禁成千上万个IP,如果添 ...

  4. [转帖]高并发系统中的尾延迟Tail Latency

    开发和运维高并发系统的工程师可能都有过类似经验,明明系统已经调优完毕,该异步的异步,该减少互斥的地方引入无锁,该减少IO的地方更换引擎或者硬件,该调节内核的调节相应参数,然而,如果在系统中引入实时监控 ...

  5. vscode推荐插件

    js相关的插件 JavaScript (ES6) code snippets Babel ES6/ES7 html css 汉化 Chinese (Simplified) (简体中文) Languag ...

  6. js正则手机号 验证

    注意一下 现在手机号第二位是不是 只有3 4 5 7 8这几个数, 如果还有请告诉我,否则这个正则表达式式错误的. <div id="app"> <el-inpu ...

  7. 一个思路:实现 golang 中的 `__file__` `__line__` 宏

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 测试 zaplog 发现,开启 caller 的调用,会使 ...

  8. [置顶] k8s,docker,微服务,监控

    综合 第一篇:k8s服务A内部调用服务B的方式 第二篇:go-zero grpc 第一篇:grpc,protobuf安装 第二篇:grpc签发证书 第三篇:golang-grpc 第四篇:python ...

  9. CF676C 题解

    使用尺取法(双指针法). 由于字符种类只有 \(2\) 种,答案一定是全 a 或全 b. 情况 \(1\):全 a 快指针循环移动,并统计字符 b 的数量 \(cntb\),直到 \(cntb\) 即 ...

  10. RabbitMQ集成系统文章01---ABP VNext 分布式事务Event Bus 集成RabbitMQ

    1.在两个应用中都配置好要连接的RabbitMQ "RabbitMQ": { "Connections": { "Default": { & ...