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. 【题解】洛谷P1065 [NOIP2006TG] 作业调度方案(模拟+阅读理解)

    次元传送门:洛谷P1065 思路 简单讲一下用到的数组含义 work 第i个工件已经做了几道工序 num 第i个工序的安排顺序 finnish 第i个工件每道工序的结束时间 need 第i个工件第j道 ...

  2. HDU 2082 找单词 (普通型 数量有限 母函数)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2082 找单词 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  3. Python 学习笔记(十一)Python语句(一)

    运算符和条件语句 算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...

  4. ORCLE10安装常见配置问题-oui.exe停止工作

    其实这是一个在安装过程中很常见的问题,之前小编说过关于甲骨文的软件用起来都很强大,但是大腕出厂,出场费是很高的,就像甲骨文的软件使用的话对于他的安装和配置的换将也是很挑剔的,出现这个问题就是因为安装文 ...

  5. Json中dumps、loads、dump、load函数实例讲解

    1.dumps() 1. json.dumps() 用于将字典(dic)类型的数据转成字符串(str),直接将dict类型的数据写入json文件中会发生报错,因此在将数据写入时需要用到该函数. imp ...

  6. postman中 form-data、x-www-form-urlencoded、raw、binary的区别【转】

    链接:https://blog.csdn.net/wangjun5159/article/details/47781443 1.form-data: 就是http请求中的multipart/form- ...

  7. 【memcached启动报错】

    #前台启动不了 #指定-u root #后台启动 #扩展选项: #利用telnet连接memcached 的端口登录memcached服务 #error表示有语法错误 #store表示正确

  8. ecshop 后台添加新菜单 以及 权限控制

    首先 在languages\zh_cn\admin\common.php 中添加 一级菜单 二级菜单 其次 在admin\includes\inc_menu.php 中添加 然后 在admin\inc ...

  9. FMX有一套自己的消息处理机制。类似这样:

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  10. Set的源码分析

    Set的内部实现其实是一个Map.即HashSet的内部实现是一个HashMap,TreeSet的内部实现是一个TreeMap,LinkedHashSet的内部实现是一个LinkedHashMap. ...