Eureka 2.0 开源工作宣告停止,对于注册中心来说 Consul 是个更好的选择。

在本场 Chat 中你可以学到的:

了解和搭建 Consul 服务;
Spring Cloud Consul 服务发现;
Spring Coud Consul 配置管理和配置刷新;
使用 Docker 搭建 Consul 集群;
Consul 负载均衡;
Spring Cloud Consul配置项整理。
在本场 Chat 旨在帮助大家更加深入了解和使用 Consul。结合 Spring Cloud 完成微服务的注册发现和配置管理。

Consul 介绍和安装

Consul 是什么

Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,采用 Go 语言开发。

Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对。Consul 采用 Raft 一致性协议算法,来保证服务的高可用;使用 GOSSIP 协议管理成员和广播消息,并且支持 ACL 访问控制。

Consul 的使用场景

Docker 实例的注册与配置共享
与 Consul template 服务集成,动态生成 Nginx 和 HAProxy 等配置文件
Spring-Cloud-Consul 服务发现和配置文件存储
Consul 的优势

使用 Raft 算法来保证一致性, 比 ZooKeeper 的 Paxos 算法更简单直接。
支持多数据中心,内外网的服务采用不同的端口进行监听。 ZooKeeper 和 etcd 均不提供多数据中心功能的支持。
支持健康检查,etcd 不提供此功能。
支持 http 和 dns 协议接口。ZooKeeper 的集成较为复杂,etcd 只支持 http 协议。
官方提供 web 管理界面,etcd 无此功能。
Consul 1.2 新增 Service Mesh 解决方案。
Consul、ZooKeeper、etcd、Eureka 比较

链接

官方网站:https://www.consul.io

下载地址:https://www.consul.io/downloads.html

启动

Consul 安装包解压完成之后就是一个可执行程序,直接运行即可。

Consul 默认启动为 dev 模式,重启后丢失数据,启动参数详解请见附录部分。

Windows

consul.exe agent -server -bootstrap -advertise 127.0.0.1 -data-dir ./data -ui
Linux

consul agent -server -bootstrap -advertise 127.0.0.1 -data-dir ./data -ui
Mac

安装

brew install consul
修改 Consul 启动参数:

vim /usr/local/opt/consul/homebrew.mxcl.consul.plist
修改 ProgramArguments 部分:

<key>ProgramArguments</key><array><string>/usr/local/opt/consul/bin/consul</string><string>agent</string><string>-server</string><string>-bootstrap</string><string>-advertise</string><string>127.0.0.1</string><string>-data-dir</string><string>./data</string><string>-ui</string></array>
启动:bashbrew services start consul

访问 Web 界面

我们为 Consul 添加了 -ui 启动参数,就会自动 Consul 的 Web 界面。端口默认为 8500,本地访问:http://localhost:8500/。

Spring Cloud Consul

Spring Cloud Consul 项目是 Spring Cloud 针对 Consul 服务治理的实现。

提供了服务发现(可替代Eureka)和配置管理(可替代Spring Cloud Config)的功能。

服务发现

1. 使用 IDEA Spring Initalizr 创建项目(consul-demo)

添加 Consul Discovery 和 Actuator (用于 Consul 服务监控检查)依赖:

注意:Spring Boot 2.0 的变化,2.0 actuator http 默认只开启了 health 和 info,全功能开放需要添加配置。

management: endpoints: web: exposure: include: '*'
2. 新建 bootstrap.yml 配置文件

spring: application: name: consul-demo cloud: consul: host: localhost port: 8500
3. 查看 Consul web 界面可以看到 consul-demo 服务已经注册

分布式配置

Consul 提供了用于存储配置和其他元数据的 [键/值存储]。是 Spring Cloud Consul Config 的替代方案。

默认情况下,配置存储在 /config 文件夹中。根据应用程序的名称和模拟 Spring Cloud Config 顺序解析属性的活动配置文件,创建多个 PropertySource 实例。

例如,名为“consul-demo”的应用程序和“dev”配置文件将创建以下属性源:

config/consul-demo,dev/config/consul-demo/config/application,dev/config/application/
注意:application 为所有服务的通用配置。,dev 表示 dev 环境的配置。

更改依赖

想要使用 Spring Cloud Consul 的分布式配置功能,需要将 spring-cloud-starter-consul-discovery 依赖更改为 spring-cloud-starter-consul-all。

注:spring-cloud-starter-consul-all 中包含了下列依赖:

<!--消息总线,提供配置实时刷新,不再依赖中间件--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-bus</artifactId></dependency><!--consul的配置中心功能--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId></dependency><!--服务注册和发现功能--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency>
在 Consul KEY/VALUE 中添加配置

首先,在 bootstrap.yml 中添加 Consul 配置:

key spring.cloud.consul.config.format value yaml
spring: cloud: consul: host: localhost port: 8500 config: format: yaml
然后:打开 Consul UI 界面添加 key/value 配置:

添加 consul-demo 服务配置:config/consul-demo/data

server: port: 8100
添加通用配置:config/application/data

management: endpoints: web: exposure: include: '*'
配置的自动更新

Consul 中的 key/value 配置更改之后 Spring Cloud 服务端需要开启 Spring 的定时任务 watch Consul 中的配置,添加注解:@EnableScheduling 。

不同于 Spring cloud config 的配置更新机制(配置更新之后使用 bus 将配置变动推送给各个服务),Spring Cloud Consul 采用的是客户端定时 watch Consul 中的 key/value 变化,然后触发 Spring 的 RefreshEvent,刷新上下文。

Consul 集群搭建

Consul 集群搭建最方便的方式是采用 Docker compose。

集群说明

3 server 节点(consul-server1 ~ 3)和 2 node 节点(consul-node1 ~ 2)
映射本地 consul/data1 ~ 3/ 目录到 Docker 容器中,避免 Consul 集群重启后数据丢失。
Consul web http 端口分别为 8501、8502、8503
新建 docker-compose.yml

version: '2.0'services: consul-server1: image: consul:latest hostname: "consul-server1" ports: - "8501:8500" volumes: - ./consul/data1:/consul/data command: "agent -server -bootstrap-expect 3 -ui -disable-host-node-id -client 0.0.0.0" consul-server2: image: consul:latest hostname: "consul-server2" ports: - "8502:8500" volumes: - ./consul/data2:/consul/data command: "agent -server -ui -join consul-server1 -disable-host-node-id -client 0.0.0.0" depends_on: - consul-server1 consul-server3: image: consul:latest hostname: "consul-server3" ports: - "8503:8500" volumes: - ./consul/data3:/consul/data command: "agent -server -ui -join consul-server1 -disable-host-node-id -client 0.0.0.0" depends_on: - consul-server1 consul-node1: image: consul:latest hostname: "consul-node1" command: "agent -join consul-server1 -disable-host-node-id" depends_on: - consul-server1 consul-node2: image: consul:latest hostname: "consul-node2" command: "agent -join consul-server1 -disable-host-node-id" depends_on: - consul-server1
集群启动时默认以 consul-server1 为 leader,然后 server2 ~ 3 和 node1 ~ 2 加入到该集群。当 server1 出现故障下线是,server2 ~ 3 则会进行选举选出新leader。

集群操作

创建并启动集群:docker-compose up -d
停止整个集群:docker-compose stop
启动集群:docker-compose start
清除整个集群:docker-compose rm(注意:需要先停止)
访问

http://localhost:8501
http://localhost:8502
http://localhost:8503
Consul 负载均衡

Consul 可以使用 Ngxin 来做集群的负载均衡。

设定负载均衡的服务器列表

upstream consul { server 127.0.0.1:8501; server 127.0.0.1:8502; server 127.0.0.1:8503;}
服务配置

server { listen 80; server_name consul.test.com;#服务域名,需要填写你的服务域名 location / { proxy_pass http://consul;#请求转向consul服务器列表 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
Spring Cloud 服务中 Consul 的地址填写 http://consul.test.com (你的服务域名)即可,不需要像 Eureka 中需要填写各个服务的地址,Consul 的集群轻量高效。

总结

相较于 Eureka,Consul 有着更少的资源占用能支撑更大的规模集群。

相较于 Spring Cloud Config 配置中心,使用起来没有 Git 存储的配置管理版本追溯方便,需要自己采用用数据库管理配置并同步到 Consul 的方式。

但是 Spring Cloud Consul 的配置更新方式更加简单高效。

附录

Consul 启动参数说明

启动参数 说明
-advertise 通知展现地址用来改变我们给集群中的其他节点展现的地址,一般情况下-bind地址就是展现地址
-bootstrap 用来控制一个server是否在bootstrap模式,在一个datacenter中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为raft leader
-bootstrap-expect 在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap公用
-bind 该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0
-client consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1
-config-file 明确的指定要加载哪个配置文件
-config-dir 配置文件目录,里面所有以.json结尾的文件都会被加载
-data-dir 提供一个目录用来存放agent的状态,所有的agent允许都需要该目录,该目录必须是稳定的,系统重启后都继续存在
-dc 该标记控制agent允许的datacenter的名称,默认是dc1
-encrypt 指定secret key,使consul在通讯时进行加密,key可以通过consul keygen生成,同一个集群中的节点必须使用相同的key
-join 加入一个已经启动的agent的ip地址,可以多次指定多个agent的地址。如果consul不能加入任何指定的地址中,则agent会启动失败,默认agent启动时不会加入任何节点。
-retry-join 和join类似,但是允许你在第一次失败后进行尝试
-retry-interval 两次join之间的时间间隔,默认是30s
-retry-max 尝试重复join的次数,默认是0,也就是无限次尝试
-log-level consul agent启动后显示的日志信息级别。默认是info,可选:trace、debug、info、warn、err
-node 节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名
-protocol consul使用的协议版本
-rejoin 使consul忽略先前的离开,在再次启动后仍旧尝试加入集群中
-server 定义agent运行在server模式,每个集群至少有一个server,建议每个集群的server不要超过5个
-syslog 开启系统日志功能,只在linux/osx上生效
-ui 使用自带的ui
-ui-dir 提供存放web ui资源的路径,该目录必须是可读的
-pid-file 提供一个路径来存放pid文件,可以使用该文件进行SIGINT/SIGHUP(关闭/更新)agent
Spring Cloud Consul 配置

核心参数

配置项 默认值
spring.cloud.consul.enabled true
spring.cloud.consul.host localhost
spring.cloud.consul.port 8500
服务发现参数

配置项 默认值
spring.cloud.consul.discovery.acl-token
spring.cloud.consul.discovery.catalog-services-watch-delay 10
spring.cloud.consul.discovery.catalog-services-watch-timeout 2
spring.cloud.consul.discovery.datacenters
spring.cloud.consul.discovery.default-query-tag
spring.cloud.consul.discovery.default-zone-metadata-name zone
spring.cloud.consul.discovery.deregister true
spring.cloud.consul.discovery.enabled true
spring.cloud.consul.discovery.fail-fast true
spring.cloud.consul.discovery.health-check-critical-timeout
spring.cloud.consul.discovery.health-check-interval 10s
spring.cloud.consul.discovery.health-check-path /actuator/health
spring.cloud.consul.discovery.health-check-timeout
spring.cloud.consul.discovery.health-check-tls-skip-verify
spring.cloud.consul.discovery.health-check-url
spring.cloud.consul.discovery.heartbeat.enabled false
spring.cloud.consul.discovery.heartbeat.interval-ratio
spring.cloud.consul.discovery.heartbeat.ttl-unit s
spring.cloud.consul.discovery.heartbeat.ttl-value 30
spring.cloud.consul.discovery.hostname
spring.cloud.consul.discovery.instance-group
spring.cloud.consul.discovery.instance-id 默认为服务名+环境+端口号
spring.cloud.consul.discovery.instance-zone
spring.cloud.consul.discovery.ip-address
spring.cloud.consul.discovery.lifecycle.enabled true
spring.cloud.consul.discovery.management-port
spring.cloud.consul.discovery.management-suffix management
spring.cloud.consul.discovery.management-tags
spring.cloud.consul.discovery.port
spring.cloud.consul.discovery.prefer-agent-address false
spring.cloud.consul.discovery.prefer-ip-address false
spring.cloud.consul.discovery.query-passing false
spring.cloud.consul.discovery.register true
spring.cloud.consul.discovery.register-health-check true
spring.cloud.consul.discovery.scheme http
spring.cloud.consul.discovery.server-list-query-tags
spring.cloud.consul.discovery.service-name
spring.cloud.consul.discovery.tags
配置服务参数

配置项 默认值
spring.cloud.consul.config.enabled true
spring.cloud.consul.config.prefix config
spring.cloud.consul.config.default-context application
spring.cloud.consul.config.profile-separator ,
spring.cloud.consul.config.data-key data
spring.cloud.consul.config.format KEY_VALUE, PROPERTIES, YAML, FILES
spring.cloud.consul.config.name ${spring.application.name}
spring.cloud.consul.config.acl-token
spring.cloud.consul.config.fail-fast false
spring.cloud.consul.config.watch.enabled true
spring.cloud.consul.config.watch.wait-time 55
spring.cloud.consul.config.watch.delay 1000

consul 配置的更多相关文章

  1. python 读取consul配置

    自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...

  2. Spring Boot 配置 - Consul 配置中心

    ▶ Spring Boot 依赖与配置 Maven 依赖 <dependencyManagement> <dependencies> <dependency> &l ...

  3. Python微服务实践-集成Consul配置中心

    A litmus test for whether an app has all config correctly factored out of the code is whether the co ...

  4. Spring Boot实战系列(7)集成Consul配置中心

    本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...

  5. consul配置

    参考:https://blog.csdn.net/achenyuan/article/details/80389410 gcp: consul安装目录:/usr/local/bin/consul co ...

  6. Spring Cloud 系列之 Consul 配置中心

    前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...

  7. consul配置参数大全、详解、总结

    命令行选项 以下选项全部在命令行中指定. -advertise - 通告地址用于更改我们通告给集群中其他节点的地址.默认情况下,-bind地址是通告的.但是,在某些情况下,可能存在无法绑定的可路由地址 ...

  8. consul配置和使用

    一:consul介绍 consul用于提供服务发现和服务配置的工具.有以下特性:1. 服务发现 consul的客户端提供一个服务,比如api或者mysql,另外一个客户端就可以去发现指定服务的服务提供 ...

  9. .Net Core with 微服务 - Consul 配置中心

    上一次我们介绍了Elastic APM组件.这一次我们继续介绍微服务相关组件配置中心的使用方法.本来打算介绍下携程开源的重型配置中心框架 apollo 但是体系实在是太过于庞大,还是让我爱不起来.因为 ...

随机推荐

  1. 系统启动日志:/var/log/boot.log

    /var/log/boot.log — 记录系统启动时的日志信息,如果系统启动之后有什么异常可以查看该文件信息 [root@localhost ~]# cat /var/log/boot.log # ...

  2. Python3 urllib 库

    urllib 简介 urllib 基础模块 使用 urllib 发送请求 使用 urllib 构造请求对象 关于 Handler 与 opener 使用 urllib 进行身份验证 使用 urllib ...

  3. iPad UIPopoverController弹出窗口的位置和坐标

    本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...

  4. win10下安装Oracle 11g 32位客户端遇到INS-13001环境不满足最低要求

    在以管理员身份运行setup.exe之后,出现了:[INS-13001]环境不满足最低要求,通过网上搜索之后找到了解决途径 首先,打开你的解压后的database文件夹,找到stage,然后cvu,找 ...

  5. js将字符串转换为数字等类型

    1.js提供了parseInt()和parseFloat()两个转换函数. 2.ECMAScript中可用的3种强制类型转换如下:  Boolean(value)——把给定的值转换成Boolean型: ...

  6. android基础---->发送和接收短信

    收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短 ...

  7. Python subprocess shell 编程规范

    使用subprocess通过shell调用另一个模块组件时,需要对返回的code进行判断.判断结果为执行失败时需要raise Exception,不然调用树过于复杂时,我们很难跟踪到异常发生的位置.s ...

  8. 【BZOJ2251】[2010Beijing Wc]外星联络 后缀数组

    [BZOJ2251][2010Beijing Wc]外星联络 Description 小 P 在看过电影<超时空接触>(Contact)之后被深深的打动,决心致力于寻找外星人的事业.于是, ...

  9. 【BZOJ2938】[Poi2000]病毒 AC自动机+DFS

    [BZOJ2938][Poi2000]病毒 Description 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码 ...

  10. 在CentOS中使用 yum 安装MongoDB及服务器端配置

    转自 http://blog.csdn.net/zhangfeng19880710/article/details/20166853 一.准备工作: 运行yum命令查看MongoDB的包信息 [roo ...