consul 配置
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 配置的更多相关文章
- python 读取consul配置
自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...
- Spring Boot 配置 - Consul 配置中心
▶ Spring Boot 依赖与配置 Maven 依赖 <dependencyManagement> <dependencies> <dependency> &l ...
- Python微服务实践-集成Consul配置中心
A litmus test for whether an app has all config correctly factored out of the code is whether the co ...
- Spring Boot实战系列(7)集成Consul配置中心
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...
- consul配置
参考:https://blog.csdn.net/achenyuan/article/details/80389410 gcp: consul安装目录:/usr/local/bin/consul co ...
- Spring Cloud 系列之 Consul 配置中心
前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...
- consul配置参数大全、详解、总结
命令行选项 以下选项全部在命令行中指定. -advertise - 通告地址用于更改我们通告给集群中其他节点的地址.默认情况下,-bind地址是通告的.但是,在某些情况下,可能存在无法绑定的可路由地址 ...
- consul配置和使用
一:consul介绍 consul用于提供服务发现和服务配置的工具.有以下特性:1. 服务发现 consul的客户端提供一个服务,比如api或者mysql,另外一个客户端就可以去发现指定服务的服务提供 ...
- .Net Core with 微服务 - Consul 配置中心
上一次我们介绍了Elastic APM组件.这一次我们继续介绍微服务相关组件配置中心的使用方法.本来打算介绍下携程开源的重型配置中心框架 apollo 但是体系实在是太过于庞大,还是让我爱不起来.因为 ...
随机推荐
- HTTP 基础术语
URI 和 URL:URI用于标记一个网络资源,URL则表示这个网络资源的访问地址,详细说明 超文本:普通的一段文字叫做文本,如果给这段文字加上超链接,那么就叫做超文本,HTML 就是超文本标记语言 ...
- Serlvet学习笔记之一 ——实现servlet的3种方法
1.配置环境,从tomcat的lib下面引入servlet-api.jar包.
- NUC970设备驱动
安装完WinUSB4NuVCOM_NUC970.exe后 USB0要配置成DEVICE 才可以在设备管理器中显示.
- 新唐M0 M4系统初始化
系统初始化包含了时钟(clock)初始化和多功能引脚(Multi Function Pin 简称MFP寄存器)配置.void SYS_Init(void) { /* 解锁保护寄存器 */ SYS_Un ...
- solr学习笔记-linux下配置solr(转)
本文地址: http://zhoujianghai.iteye.com/blog/1540176 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Jav ...
- beginUpdates和endUpdates-实现UITableView的动画块
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...
- stl中的map经验
如果想使用一个map临时变量装载参数map,不需要使用new创建一个对象. 声明一个变量,直接赋值就可以.map内部自己重载了=操作符,会自己分配内存.
- nested exception is org.springframework.beans.factory.BeanCreationException: 不能注入对象 创建对象失败 spring
[出现错误的背景] 在使用Spring+SpringMVC+Mybatis SSM集成框架时,服务器启动就会报错. [错误根源] XML配置错误. [解决方案] 第一步.查找springmvc.xml ...
- 【Linux系列】find命令使用
Linux下find命令在目录结构中搜素文件,病执行制定的操作. 一.命令格式 find pathname -options[-print -exec -ok] 二.命令功能 用于在文件树种查找文件, ...
- 【BZOJ3932】[CQOI2015]任务查询系统 主席树
[BZOJ3932][CQOI2015]任务查询系统 Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si, ...