Consul 入门
1. 什么是Consul?
Consul 有很多组件,对于整体来说,它是一个服务发现和服务配置的工具,它提供了一下特性:
- 服务发现
- 健康检查
- KV存储
- 多数据中心
2.安装Consul
以下是在 CentOS 系统上操作
下载
wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip
查看版本
consul -v
Consul v1.1.0
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
3.启动 Agent
使用dev模式启动。
$ consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.1.0'
Node ID: 'a94cdf4f-e36d-9bef-927c-61c1d14500cd'
Node name: 'bogon'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2018/06/23 00:03:45 [DEBUG] agent: Using random ID "a94cdf4f-e36d-9bef-927c-61c1d14500cd" as node ID
2018/06/23 00:03:45 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:a94cdf4f-e36d-9bef-927c-61c1d14500cd Address:127.0.0.1:8300}]
2018/06/23 00:03:45 [INFO] serf: EventMemberJoin: bogon.dc1 127.0.0.1
2018/06/23 00:03:45 [INFO] serf: EventMemberJoin: bogon 127.0.0.1
2018/06/23 00:03:45 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2018/06/23 00:03:45 [INFO] consul: Adding LAN server bogon (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2018/06/23 00:03:45 [INFO] consul: Handled member-join event for server "bogon.dc1" in area "wan"
2018/06/23 00:03:45 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2018/06/23 00:03:45 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2018/06/23 00:03:45 [INFO] agent: started state syncer
2018/06/23 00:03:45 [WARN] raft: Heartbeat timeout from "" reached, starting election
2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2018/06/23 00:03:45 [DEBUG] raft: Votes needed: 1
2018/06/23 00:03:45 [DEBUG] raft: Vote granted from a94cdf4f-e36d-9bef-927c-61c1d14500cd in term 2. Tally: 1
2018/06/23 00:03:45 [INFO] raft: Election won. Tally: 1
2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2018/06/23 00:03:45 [INFO] consul: cluster leadership acquired
2018/06/23 00:03:45 [DEBUG] consul: Skipping self join check for "bogon" since the cluster is too small
2018/06/23 00:03:45 [INFO] consul: member 'bogon' joined, marking health alive
2018/06/23 00:03:45 [INFO] consul: New leader elected: bogon
2018/06/23 00:03:45 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/06/23 00:03:45 [INFO] agent: Synced node info
2018/06/23 00:03:46 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/06/23 00:03:46 [DEBUG] agent: Node info in sync
2018/06/23 00:03:46 [DEBUG] agent: Node info in sync
4. 查看集群成员
在另外一个终端使用 consul members
, 可以查看集群成员信息。
$ consul members
Node Address Status Type Build Protocol DC Segment
bogon 127.0.0.1:8301 alive server 1.1.0 2 dc1 <all>
也可以通过HTTP API 来查看Consul成员信息
$ curl localhost:8500/v1/catalog/nodes
[
{
"ID": "a94cdf4f-e36d-9bef-927c-61c1d14500cd",
"Node": "bogon",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
}
]
5.服务注册
定义一个服务步骤
$ sudo mkdir /etc/consul.d
定义一个服务名为web, 运行端口为80
$ echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \
| sudo tee /etc/consul.d/web.json
重启agent,并制定配置目录
$ consul agent -dev -config-dir=/etc/consul.d
==> Starting Consul agent...
...
[INFO] agent: Synced service 'web'
...
下面通过HTTP API的方式来查询服务
$ curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "68530a9c-2434-9a80-32ac-e4e455086655",
"Node": "bogon",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServiceMeta": {},
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
}
]
同时也可以通过下面的HTTP API 来查询服务的健康状况
$ curl 'http://localhost:8500/v1/health/service/web?passing'
[
{
"Node": {
"ID": "68530a9c-2434-9a80-32ac-e4e455086655",
"Node": "bogon",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
},
"Service": {
"ID": "web",
"Service": "web",
"Tags": [
"rails"
],
"Address": "",
"Meta": null,
"Port": 80,
"EnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
},
"Checks": [
{
"Node": "bogon",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 5,
"ModifyIndex": 5
}
]
}
]
更新服务
可以通过修改配置文件和发送SIGHUP
到agent, 更新服务的信息。
另外,也可以通过HTTP API 的方式动态添加或者移除服务信息。
Consul 入门的更多相关文章
- Consul 入门-运行
HashiCorp Consul 是由 HashiCorp 公司开发的,它是一家专注于 DevOps 工具链的公司,旗下的明星级产品包括 Vagrant.Terraform.Vault.Nomad 以 ...
- Spring Cloud Consul入门
1. Consul介绍 Consul是一套开源的分布式服务发现和配置管理系统,支持多数据中心分布式高可用.Consul是HashiCorp( Vagrant的创建者)开发的一个服务发现与配置项目,用G ...
- Consul入门
推荐: Consul 原理和使用简介 启动:consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node Litao-Mac ...
- Consul 入门(二)
KV 存储 通过命令行操作 $ consul kv put hello world # 设置数据 Success! Data written to: hello $ consul kv get hel ...
- Consul入门初识
Consul Consul是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由HashiCrop公司用Go语言开发,基于Mozilla Public License 2.0的协议进行开源 ...
- 01. Consul 入门
简介 Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式",内置了服务注册 ...
- Consul 入门-初识
背景 现状:单体架构逐渐被微服务架构所替代,原本两个功能模被拆分成了两个服务.原本两个模块块间的通信只需要函数调用就能够实现,现在却做不到了,因为它们不在同一个进程中,甚至两个服务都可能部署到不同的机 ...
- Spring Cloud Consul 入门指引
1 概述 Spring Cloud Consul 项目为 Spring Boot 应用程序提供了与 Consul 的轻松集成. Consul 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战: ...
- Consul 服务注册与服务发现
上一篇:Mac OS.Ubuntu 安装及使用 Consul 1. 服务注册 对 Consul 进行服务注册之前,需要先部署一个服务站点,我们可以使用 ASP.NET Core 创建 Web 应用程序 ...
随机推荐
- 传入中文参数-->服务器_转码的方法
如果要传入 中文参数到 服务器 使用lr_convert_string_encoding() LR_ENC_SYSTEM_LOCALE , 转为 ...
- python基本数据类型——集合
集合 无序可变序列,集合中元素不允许重复,即每个元素都是唯一的 集合中的元素按照升序排列 # 创建集合 >>aset = set([0,2,4,5,7,2,3,5,9,0]) >&g ...
- HTML 之 表单
关于HTML的表单 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...
- 剑指offer-从上往下打印二叉树22
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom( ...
- 如何在etherscan提交代币官方信息
https://ethlinkersupport.zendesk.com/hc/zh-cn/articles/360001334992-%E5%A6%82%E4%BD%95%E5%9C%A8ether ...
- 春招实习汇总(7个offer)
转载出处 刚从北京到家,总算也可以歇歇了,最近一段时间真是忙于奔命的感觉,也确实体会到了找工作的艰辛,总而言之,求职之路,如人饮水,冷暖自知. 我想把这段时间找工作的体验和经历分享出来告诉大家,让大避 ...
- .net转PHP从零开始-环境的搭建
PHP初级开发环境安装很简单,只需要使用一键安装的phpstudy 下载地址:http://www.phpstudy.net/ 安装后可以看到 这样的界面,设置好相关的配置,然后,选择查看phpinf ...
- net::ERR_ABORTED ,引入js文件出现报错的解决方法
在head头里面添加 <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation ...
- css重修之书(一):如何用css制作比1px更细的边框
如何用css制作比1px更细的边框 在项目的开发过程中,我们常常会使用到border:1px solid xxx,来对元素添加边框: 可是1px的border看起来还是粗了一些粗,不美观,那么有什么方 ...
- No Route to Host from master/192.168.2.131 to master:9000 failed on socket t
host里边添加的ip地址与当前的ip地址(ifconfig可以查看)不一致,修改当前ip地址就可以了.