风.foxwho(神秘狐)

神秘狐,梦想 构架一个世界

consul服务发现与注册于配置 (mac版为例)

consul 介绍

Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,比如 Airbnb的SmartStack等相比,Consul的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。使用起来也较 为简单。Consul用Golang实现,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合。
gitHub: https://github.com/hashicorp/consul
官网:https://www.consul.io/
官方说明:https://www.consul.io/docs/index.html
go api: https://godoc.org/github.com/hashicorp/consul/api
golang API 代码位置:https://github.com/hashicorp/consul/tree/master/api

consul 目录或文件说明

/tmp/consul: 数据存储
/Users/fox/bin/:程序安装目录
fox : mac系统用户的用户名

server 和 client 说明

本部分来自 https://blog.csdn.net/buxiaoxia/article/details/69788114#t2

CLIENT

CLIENT表示consul的client模式,就是客户端模式。是consul节点的一种模式,这种模式下,所有注册到当前节点的服务会被转发到SERVER,本身是不持久化这些信息。
简单的说,client 处理健康检查,注册服务等,但是这个注册只是转发到server中,如果有成千上万的服务,分别启动多个client,可以减少server 压力

SERVER

SERVER表示consul的server模式,表明这个consul是个server,这种模式下,功能和CLIENT都一样,唯一不同的是,它会把所有的信息持久化的本地,这样遇到故障,信息是可以被保留的。

SERVER-LEADER

中间那个SERVER下面有LEADER的字眼,表明这个SERVER是它们的老大,它和其它SERVER不一样的一点是,它需要负责同步注册的信息给其它的SERVER,同时也要负责各个节点的健康监测。

consul 安装

打开 https://www.consul.io/intro/getting-started/install.html ,在 Install Consul 下面有 binary package ,点击 这个 binary package (它是个超链接 https://www.consul.io/downloads.html),选择你要下载的版本
创建目录

mkdir -p /Users/fox/bin/

打开这个目录 /Users/fox/bin/,然后下载consul
解压缩后 只有一个 单文件 consul
执行脚本

./consul

输出

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

Available commands are:
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    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
    kv             Interact with the key-value store
    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
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    snapshot       Saves, restores and inspects snapshots of Consul server state
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

设置环境配置

sudo vim /etc/paths.d/user_fox

内容输入如下

/Users/fox/bin

应用生效

source /etc/profile

测试,在任何目录中,输入

consul

输出

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

Available commands are:
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    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
    kv             Interact with the key-value store
    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
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    snapshot       Saves, restores and inspects snapshots of Consul server state
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

consul 参数说明

-dev 开发者模式 该节点的启动不能用于生产环境,因为该模式下不会持久化任何状态
该启动模式仅仅是为了快速便捷的启动单节点consul
该节点处于server模式
该节点是leader
该节点是一个健康节点
-ui 启动自有主机的界面
-bootstrap-expect 1 集群节点,表示等待多少个节点再启动,这里是1个,一个就启动
-bind=127.0.0.1 绑定IP ,本机IP地址,内网IP
-advertise-wan=10.23.123.12 绑定外网ip
-node=node1 节点名称,如果没有,默认是主机名
-server 设置为服务端
-data-dir /tmp/consul 数据存储目录为 /tmp/consul
-datacenter=dc1 数据中心
更多请看
https://www.consul.io/docs/agent/options.html

consul agent server client

agent可以运行在server或者client模式

consul agent server 服务端

consul agent server 服务端启动

consul agent -dev -server -bootstrap-expect 1 -data-dir /tmp/consul -node=node1

停止

Ctrl+C  #按键

如果你按错了那么使用强制停止

ps -ef |grep consul

输出

  501   831   813   0  1:12下午 ttys001    0:02.37 consul agent -dev -config-dir /Users/fox/bin/consul.d/
  501   890   813   0  1:16下午 ttys001    0:00.00 grep consul

第一行就是要停止的进程

kill -9 831

查看成员

consul members

服务操作

创建一个 服务

新建目录

mkdir -p /Users/fox/bin/consul.d

创建web服务和service2服务

echo '{"service": {"name": "web", "tags": ["rails"], "port": 801}}' >/Users/fox/bin/consul.d/web.json

echo '{"service": {"name": "service2", "tags": ["rails"], "port": 802}}' >/Users/fox/bin/consul.d/service2.json

启动代理(你要先关闭之前开启的那个代理哦)

consul agent -dev -config-dir /Users/fox/bin/consul.d/

查询服务

DNS 查询

服务的DNS名称是 NAME.service.consul
service2:服务名称
web:服务名称

dig @127.0.0.1 -p 8600 service2.service.consul

输出

; <<>> DiG 9.9.7-P3 <<>> @127.0.0.1 -p 8600 service2.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56742
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;service2.service.consul.   IN  A

;; ANSWER SECTION:
service2.service.consul. 0  IN  A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Thu Jan 18 13:29:46 CST 2018
;; MSG SIZE  rcvd: 68

HTTP API 查询

curl http://localhost:8500/v1/catalog/service/service2

输出

[
    {
        "ID": "e3e23dc4-f3c7-24bb-e35a-53ae6bcae58a",
        "Node": "fox",
        "Address": "127.0.0.1",
        "Datacenter": "dc1",
        "TaggedAddresses": {
            "lan": "127.0.0.1",
            "wan": "127.0.0.1"
        },
        "NodeMeta": {
            "consul-network-segment": ""
        },
        "ServiceID": "service2",
        "ServiceName": "service2",
        "ServiceTags": [
            "rails"
        ],
        "ServiceAddress": "",
        "ServicePort": 802,
        "ServiceEnableTagOverride": false,
        "CreateIndex": 6,
        "ModifyIndex": 6
    }
]

查询健康

 curl 'http://localhost:8500/v1/health/service/service2?passing'

输出

[
    {
        "Node": {
            "ID": "e3e23dc4-f3c7-24bb-e35a-53ae6bcae58a",
            "Node": "fox",
            "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": "service2",
            "Service": "service2",
            "Tags": [
                "rails"
            ],
            "Address": "",
            "Port": 802,
            "EnableTagOverride": false,
            "CreateIndex": 6,
            "ModifyIndex": 6
        },
        "Checks": [
            {
                "Node": "fox",
                "CheckID": "serfHealth",
                "Name": "Serf Health Status",
                "Status": "passing",
                "Notes": "",
                "Output": "Agent alive and reachable",
                "ServiceID": "",
                "ServiceName": "",
                "ServiceTags": [],
                "Definition": {},
                "CreateIndex": 5,
                "ModifyIndex": 5
            }
        ]
    }
]

Consul Web界面

加入 -ui 参数, 启动自有主机的界面
启动代理(你要先关闭之前开启的那个代理哦)

consul agent -dev -ui -config-dir /Users/fox/bin/consul.d/

等待一会,浏览器中输入如下,就可以看到UI界面了

http://localhost:8500/ui

Consul 集群 docker版

请打开网址查看
http://blog.csdn.net/fenglailea/article/details/79098246

Consul 集群

稍后添加

A&Q

Error starting agent: Failed to start Consul server: Failed to start RPC layer: listen tcp 127.0.0.1:8300: bind: address already in use

原因:你已经启动一个了,要么关闭它,要么继续使用已经启动过得
用这个命令查看

ps -ef |grep consul

https://segmentfault.com/a/1190000005005227
https://www.jianshu.com/p/28c6bd590ca0
http://tonybai.com/2015/07/06/implement-distributed-services-registery-and-discovery-by-consul/
https://www.cnblogs.com/newP/p/6349316.html

阅读更多

扫码向博主提问

去开通我的Chat快问

wljk506

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • php
  • java
  • go
  • 架构
  • more
版权声明:原创文章欢迎转载,不过要记得加出处哦 https://blog.csdn.net/wljk506/article/details/79093848
个人分类: Go 服务框架
上一篇go lang go get There is no tracking information for the current branch.Please specify which
下一篇consul 服务发现 集群 docker 版
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭