Spring Cloud 之配置服务器(上)

学习目标

今天我们要学习的是 Spring Cloud Config Server(配置服务器),首先我们先创建项目:打开 https://start.spring.io/,填写相关信息,项目名为 spring-cloud-chapter-3-config-server,添加 Web、Actuator 以及 Config Server 依赖,点击 “Generate Project” 按钮生成项目,并导入到 idea 中。(注:此处使用的 Spring Boot 版本为 1.X 系列)


一、认识 Spring Cloud 配置服务器

1)Spring Cloud Config Server:
Spring Cloud 配置服务器提供分布式动态化集中管理应用配置信息的能力。

  • 分布式:通过负载均衡将流量分发到不同的配置服务器上
  • 动态化:数据源(Git,数据库)发生变化时,客户端能够感知的到
  • 集中管理:一个配置服务器可能管理多个配置客户端

2)构建 Spring Cloud 配置服务器
使用 @EnableConfigServer 注解标注


二、服务端 Environment 仓储

1)EnvironmentRepository
Spring Cloud 配置服务器管理多个客户端应用的配置信息,然而这些配置信息需要通过一定的规则获取。Spring Cloud Config Server 提供 EnvironmentRepository 接口供客户端应用获取,其中获取维度有三:

  • {application}:配置客户端应用名称,即配置项:spring.application.name
  • {profile}:配置客户端应用当前激活的 Profile,即配置项:spring.profiles.active
  • {label}:配置服务端标记的版本信息,如 Git 中的分支名

2)两种方式搭建 Spring Cloud Config Server

3)服务端配置映射

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

三、搭建 Spring Cloud Config Server 之基于文件系统创建本地仓库

上面我们说到了有两种方式可以搭建 Spring Cloud Config Server,我们先来看第一种--基于文件系统创建本地仓库。
1)在启动类中添加 @EnableConfigServer 注解,激活应用配置服务器。

package top.alanshelby.springcloudchapter3configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringCloudChapter3ConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringCloudChapter3ConfigServerApplication.class, args);
    }
}

2)创建本地目录

理解 Java 中的 ${user.dir},在 IDE 中是指的当前项目物理路径

可以使用下面代码进行输出:

System.out.println(System.getProperty("user.dir"));

输出的结果为当前项目的路径:

E:\Project\CourseProject\SpringCloud\spring-cloud-chapter\spring-cloud-chapter-3-config-server

在 idea 中 \src\main\resources 目录下创建名为 configs 的目录,它的绝对路径:${user.dir}\\src\\main\\resources\\configs
3)配置 git 本地仓库 uri
在 application.properties 文件中添加如下配置信息:

# 配置服务器文件系统 git 仓库
spring.cloud.config.server.git.uri = ${user.dir}\\src\\main\\resources\\configs

4)给应用 blogs 创建三个环境的配置文件,并添加 name 属性,这三个配置文件均位于 configs 目录下:

  • blogs.properties
    • name = blogs
  • blogs-prod.properties
    • name = blogs-prod
  • blogs-test.properties
    • name = blogs-test

三个文件的环境 profile 分别是:defaultprodtest
5)初始化本地 git 仓库
在 ${user.dir}\src\main\resources\configs 路径下执行以下 git 命令:

- git init
- git add .
- git commit -m "xxx"
出现以下结果表示成功:
 3 files changed, 7 insertions(+)
 create mode 100644 blogs-prod.properties
 create mode 100644 blogs-test.properties
 create mode 100644 blogs.properties

6)测试配置服务器
启动项目,通过浏览器测试应用为 blogs(与创建的三个配置文件名称相对应,具体映射关系可看本文第二部分中的服务端配置映射),Profile 为 test 的配置内容,访问 http://localhost:9090/blogs/test,这时就会看到 blogs.propertiesblogs-test.properties 两个配置文件的信息,请注意:当指定了 Profile 时,默认的 Profile(不指定)配置信息也会输出 blogs.properties


四、搭建 Spring Cloud Config Server 之基于远程 git 仓库

上面我们学习了如何利用文件系统创建本地仓库,接下来我们来看看如何基于远程 git 仓库来 Spring Cloud Config Server。
1)在启动类中添加 @EnableConfigServer 注解,激活应用配置服务器。
2)配置远程 Git 仓库地址

# 配置服务器远程 Git 仓库(GitHub)
spring.cloud.config.server.git.uri = https://github.com/AlanShelby/blogs-temporary.git

3)本地 clone 远程 Git 仓库

https://github.com/AlanShelby/blogs-temporary.git

4)给应用 blogstemp 创建三个环境的配置文件

  • blogstemp.properties
    • name = blogstemp
  • blogstemp-prod.properties
    • name = blogstemp-prod
  • blogstemp-test.properties
    • name = blogstemp-test

三个文件的环境 profile 分别是:defaultprodtest
5)提交到远程 Git 仓库(默认分支为 master,即后面所用到的 label)

 git add blogstemp*.properties
 git commit -m "add blogstemp config files"
 git push

6)配置强制拉取内容

# 强制拉去 Git 内容
spring.cloud.config.server.git.force-pull = true

7)测试配置服务器
启动项目,通过浏览器测试应用为 blogstemp,Profile 为 test 的配置内容,访问 http://localhost:9090/blogstemp/test,这时就会看到 blogstemp.properties 和 blogstemp-test.properties 两个配置文件的信息。

注意:这里的 version 与你 git 提交的 version 是一致的,可在本地使用 git log 命令查看。


五、Spring Cloud 配置客户端

1)创建 Spring Cloud Config Client 应用
创建一个名为 spring-cloud-chapter-3-config-client 的应用

2)在 classPath 下面创建 bootstrap.properties
3)配置 bootstrap.properties
配置以 spring.cloud.config. 开头的配置信息

# 配置客户端应用关联的应用,该配置时可选的,如果不配置,采用 spring.application.name
spring.cloud.config.name = blogstemp
# 关联 profile
spring.cloud.config.profile = prod
# 关联 label
spring.cloud.config.label = master
# 关联 uri
spring.cloud.config.uri = http://localhost:9090/

application.properties 中添加如下配置:

spring.application.name = spring-cloud-config-client
server.port = 8080
management.security.enabled = false

4)先启动 spring-cloud-chapter-3-config-server 然后再启动 spring-cloud-chapter-3-config-client 项目,在 spring-cloud-chapter-3-config-client 启动日志中可以看到如下信息,表示 clien 已经连接到了 server,并取到了 GitHub 上的 blogstemp-prod.properties 配置文件。

Fetching config from server at: http://localhost:9090/

Located environment: name=blogstemp, profiles=[prod], label=master, version=11399ab7d42f35c3282bb2c775f97e2d77b2c0e5, state=null

Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/AlanShelby/blogs-temporary.git/blogstemp-prod.properties'}, MapPropertySource {name='https://github.com/AlanShelby/blogs-temporary.git/blogstemp.properties'}]]

5)测试 Spring Cloud 配置客户端
通过浏览器访问 http://localhost:8080/env


至此,关于Spring Cloud Config Server(配置服务器)就讲解完了,这是我的理解,各位看官如果有不同见解或文章中有错误,请不吝指正。
所用代码码云地址:https://gitee.com/AlanShelby/spring-cloud-chapter
知乎专栏地址:https://zhuanlan.zhihu.com/c_200981602
个人微信公众号:AlanShelby(多多关注,感谢~)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271