您的位置:首页 > 其它

consul(第一篇)consul 入门

2016-06-01 21:31 696 查看

consul 入门

consul是什么

consul包含多个组件,从整体上看可以理解为一个服务发现、配置的工具,包含一下几个特点:

服务发现(Service Discovery):客户端通过consul提供服务,其他客户端可以通过consul利用dns或者http发现依赖服务

健康检查(Health Checking): consul提供任务的健康检查,可以用来操作或者监控集群的健康,也可以在服务发现时去除失效的服务

键值对存储(Key/Value Store): 存储层级键值对

多数据中心(Multi Datacenter): consul支持开箱即用的多数据中心

consul 安装

1.下载consul安装包并解压

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip


解压后得到可执行的二进制文件,执行
./consul
得到

usage: consul [--version] [--help] <command> [<args>]

Available commands are:
agent          Runs a Consul agent
configtest     Validate config file
event          Fire a new event
exec           Executes a command on Consul nodes
force-leave    Forces a member of the cluster to enter the "left" state
info           Provides debugging information for operators
join           Tell Consul agent to join cluster
keygen         Generates a new encryption key
keyring        Manages gossip layer encryption keys
leave          Gracefully leaves the Consul cluster and shuts down
lock           Execute a command holding a lock
maint          Controls node or service maintenance mode
members        Lists the members of a Consul cluster
monitor        Stream logs from a Consul agent
reload         Triggers the agent to reload configuration files
rtt            Estimates network round trip time between nodes
version        Prints the Consul version
watch          Watch for changes in Consul


表示consul成功安装,同时将
consul
所在目录配置到
path
,例如这里
consul
software
努力下,进行如下配置

vim ~/.bash_profile

export GOROOT=/software/go
export GOPATH=/root/work
PATH=$PATH:$HOME/bin:$GOROOT/bin:/software

export PATH

source ~/.bash_profile


启动

首先下载web-ui

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_web_ui.zip[/code] 
通过下面命令启动consul

consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -advertise 120.76.145.187 -client 120.76.145.187 -ui-dir /software/consul-ui


server
表示启动的为consul server ,构建一个consul cluster 一般建议使用3或者5个consul server

bootstrap-expect 1
表示期望的服务节点数目为1

-data-dir
数据目录,如果该文件夹不存在则手工创建,如果在consul发生错误后,建议先清理该目录文件

advertise
设置广播地址,ip可以设置为公网ip

client
设置client访问的地址

ui-dir
web控制台目录位置

注意 通过
advertise
client
才能在WAN上访问consul,否则只能在LAN内访问.

官方给出的快捷启动为
consul agent -dev
如果想简单体验一下可以使用该命令

启动后访问
http://ip:8500/ui
得到如下界面



获取基本信息

获取consul server集群中的成员列表

使用命令方式

如果以局域网中,直接使用
consul members


如果是广域网中,使用
consul members  --rpc-addr=ip:8400


得到结果:

Node          Address              Status  Type    Build  Protocol  DC
iZ94235juoyZ  120.76.145.187:8301  alive   server  0.6.4  2         dc1


使用http接口方式

curl 120.76.145.187:8500/v1/catalog/nodes


返回结果:

[{"Node":"iZ94235juoyZ","Address":"120.76.145.187","TaggedAddresses":{"wan":"120.76.145.187"},"CreateIndex":3,"ModifyIndex":4}]


通过dns方式

dig @120.76.145.187 -p 8600 iZ94235juoyZ.node.consul


返回结果:

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6 <<>> @120.76.145.187 -p 8600 iZ94235juoyZ.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 625
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;iZ94235juoyZ.node.consul.      IN      A

;; ANSWER SECTION:
iZ94235juoyZ.node.consul. 0     IN      A       120.76.145.187

;; Query time: 0 msec
;; SERVER: 120.76.145.187#8600(120.76.145.187)
;; WHEN: Wed Jun  1 21:24:31 2016
;; MSG SIZE  rcvd: 82


ok ~ it’s work ! more about is here

转载请注明

http://blog.csdn.net/liaokailin/article/details/51559228

欢迎关注,您的肯定是对我最大的支持

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  consul 服务发现