Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】

 发表于 2018-04-19 |  更新于 2018-04-26 | 

本文接之前的《Spring Cloud(七):配置中心(Git、Refresh)》,继续来说说 Spring Cloud Config 的使用。

先来回顾一下,在前文中我们完成了什么:

  • 构建了 config-server,连接到 Git 仓库
  • 在 Git 上创建了一个 config-repo 目录,用来存储配置信息
  • 构建了 config-client,来获取 Git 中的配置信息
  • 在 config-client 中开启了 Refresh,动态刷新配置信息

在本文中,我们继续来看看 Spring Cloud Config 的一些其他能力。

高可用问题

传统作法

通常在生产环境,Config Server 与服务注册中心一样,我们也需要将其扩展为高可用的集群。在之前实现的 config-server 基础上来实现高可用非常简单,不需要我们为这些服务端做任何额外的配置,只需要遵守一个配置规则:将所有的 Config Server 都指向同一个 Git 仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定 Config Server 位置时,只要配置 Config Server 外的均衡负载即可,就像如下图所示的结构:

注册为服务

虽然通过服务端负载均衡已经能够实现,但是作为架构内的配置管理,本身其实也是可以看作架构中的一个微服务。所以,另外一种方式更为简单的方法就是把 config-server 也注册为服务,这样所有客户端就能以服务的方式进行访问。通过这种方法,只需要启动多个指向同一 Git 仓库位置的 config-server 就能实现高可用了。

配置过程也非常简单,我们基于配置中心 Git 版本的内容来改造。

代码改造

服务端改造

添加依赖

在 pom.xml 里边添加以下依赖

复制

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件

在 application.yml 里新增 Eureka 的配置

复制

1
2
3
4
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/

这样 Server 端的改造就完成了。先启动 Eureka 注册中心,在启动 Server 端,在浏览器中访问:http://localhost:7000/ 就会看到 Server 端已经注册了到注册中心了。

客户端改造

添加依赖

复制

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件

bootstrap.yml

复制

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
config:
name: config-client
profile: dev
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/

主要是去掉了spring.cloud.config.uri直接指向 Server 端地址的配置,增加了最后的三个配置:

  • spring.cloud.config.discovery.enabled:开启 Config 服务发现支持
  • spring.cloud.config.discovery.serviceId:指定 Server 端的 name, 也就是 Server 端spring.application.name的值
  • eureka.client.service-url.defaultZone:指向配置中心的地址

这三个配置文件都需要放到bootstrap.yml的配置中。

启动 Client 端,在浏览器中访问:http://localhost:7000/ 就会看到 Server 端和 Client 端都已经注册了到注册中心了。

高可用

为了模拟生产集群环境,我们启动两个 Server 端,端口分别为 12000 和 12001,提供高可用的 Server 端支持。

复制

1
2
3
4
5
6
// 打包
./mvnw clean package -Dmaven.test.skip=true // 启动两个 Server
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=12000
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=12001

如上图就可发现会有两个 Server 端同时提供配置中心的服务,防止某一台 down 掉之后影响整个系统的使用。

我们先单独测试服务端,分别访问:http://localhost:12000/config-client/dev 和 http://localhost:12001/config-client/dev 返回信息:

复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "config-client",
"profiles": [
"dev"
],
"label": null,
"version": "90dd76966da0eed967a0cbce3320f0f7ff63eb6b",
"state": null,
"propertySources": [
{
"name": "https://github.com/zhaoyibo/spring-cloud-study/config-repo/config-client-dev.yml",
"source": {
"info.profile": "dev update"
}
}
]
}

说明两个 Server 端都正常读取到了配置信息。

再次访问 http://localhost:13000/info 返回dev update。说明客户端已经读取到了 Server 端的内容,我们随机停掉一台 Server 端的服务,再次访问 http://localhost:13000/info 依然返回dev update,说明达到了高可用的目的。

相关阅读

Spring Cloud(一):服务治理技术概览
Spring Cloud(二):服务注册与发现 Eureka
Spring Cloud(三):服务提供与调用 Eureka
Spring Cloud(四):服务容错保护 Hystrix
Spring Cloud(五):Hystrix 监控面板
Spring Cloud(六):Hystrix 监控数据聚合 Turbine
Spring Cloud(七):配置中心(Git 版与动态刷新)
Spring Cloud(八):配置中心(服务化与高可用)
Spring Cloud(九):配置中心(消息总线)
Spring Cloud(十):服务网关 Zuul(路由)
Spring Cloud(十一):服务网关 Zuul(过滤器)
Spring Cloud(十二):分布式链路跟踪(Sleuth 与 Zipkin)

示例代码:GitHub

参考

springcloud(八):配置中心服务化和高可用
Spring Cloud 构建微服务架构(四)分布式配置中心(续)

Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】的更多相关文章

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

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

  2. Spring Cloud(八):分布式配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  3. spring cloud深入学习(九)-----配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  4. springcloud(八):配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  5. 二十、springcloud(六)配置中心服务化和高可用

    1.问题描述 前一篇,spring-cloud-houge-provider(称之为客户端)直接从spring-cloud-houge-config(称之为服务端)读取配置,客户端和服务端的耦合性太高 ...

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

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

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

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

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

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

  9. spring cloud 系列第2篇 —— eureka 高可用注册中心的搭建 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.项目结构 eureka-server为服务注册中心,负责服务的管理: eur ...

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

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

随机推荐

  1. C语言scanf与get char,gets的区别

    C语言scanf与get char,gets的区别 1.scanf() scanf是C语言的格式输入函数是通用终端格式化输入函数,它从标准输入设备(键盘) 读取输入的信息.可以读入任何固有类型的数据并 ...

  2. ARP, Fragmentation and Reassembly

    Address Resolution Protocol IP addresses are said to be logical, because they are defined in terms o ...

  3. Learning by doing——获黄色领骑衫之感

    获奖感言 能拿到这件黄色的领骑衫,心里真的非常高兴.仔细看了一下,扣子.领子.各种图案各种细节十分精致.可以说这件领骑衫既有纪念意义,又有实用意义,真的很棒. 背后的故事 其实开始接触博客的时候,我是 ...

  4. SVN文件自动加锁-Win7

    在Win7操作系统上 打开目录C:\Users\Administrator\AppData\Roaming\Subversion 用记事本打开config文件 将enable-auto-props = ...

  5. asp.net mvc HtmlHelperExt EnumDropDownList

    public static class HtmlHelperExt { public static MvcHtmlString EnumDropDownList<TEnum>(this H ...

  6. 多python版本下,使用pip安装第三方库

    说明:win10系统,先安装有Python3.5.2,后又安装了Python2.7.13(并重命名了Python27文件夹下python.exe为python2.7.13.exe),试图使用pip安装 ...

  7. 自动化运维工具Ansible实战(四)常用模块

    转载链接:http://blog.51cto.com/liqingbiao/1962609   Ansible模块按功能分为:云模块.集群模块. 命令模块.数据库模块.文件模块.资产模块.消息模块.监 ...

  8. delect 删除

    delete ---整表数据删除 (慎用) delete  * from 表名; ---条件删除 delete  * from  表名  where  限制条件;

  9. ubuntu包管理机制

    1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...

  10. Python模块、包、异常、文件(案例)

    Python模块.包.异常.文件(案例) python.py #模块 # Python中的模块(Module),是一个Python文件,以.py文件结尾,包含了Python对象定义和Python语句, ...