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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程介绍 【完善体系+精品资料】本课程总计115课时,打造全网最全的微服务体系课程;从微服务是什么、能够做什么开始讲起,绝对零基础入门到精通类型。课程整体脉络十分清晰,每个章节一个知识点,画图+源码+运行讲解,不信你学不会。1、课程先讲解了什么是单体架构、什么是微服务架构、他们之间有什么区别和联系,各自有什么优缺点。2、从本质入手,使用最简单的Spring Boot搭建微服务,让你认清微服务是一种思想和解决问题的手段,而不是新兴技术。3、讲解Spring Boot 与 Spring Cloud 微服务架构之间的联系,原生的RestTemplate工具,以及Actuator监控端点的使用。4、带着微服务所带来的各种优缺点,为大家引入服务发现注册的概念和原理,从而引入我们的第一个注册中心服务Eureka。5、引入负载均衡的理念,区分什么是服务端负载均衡,什么是客户端负载均衡,进而引入Ribbon负载均衡组件的详细使用。6、为了解决微服务之间复杂的调用,降低代码的复杂度,我们引入了Feign声明式客户端,让你几行代码学习服务的远程调用。7、为了解决服务之间的稳定性,避免发生雪崩问题,我们引入了Hystrix断路器,服务降级和熔断机制。8、微服务集群十分庞大,监控起来是十分困难的,尤其是对每一个接口的熔断情况进行监控,因此我们引入了Turbine微服务监控。9、微服务的调用是杂乱无章的,可以网状调用,怎么做到统一的入口出口,统一的授权、加密、解密、日志过滤,我们引入了第一代网关Zuul。10、微服务配置分散,每次要修改配置都要重启服务,因此我们引入了Config配置中心。11、跟上主流,Consul是当前主流的服务注册发现配置中心一体化的解决方案。12、阿里的Nacos服务注册发现配置中心在国内炙手可热,Nacos 经历过双十一的微服务中间件。13、Turbin做微服务监控还是太弱,我们需要更强大,可视化,操作性更强的监控系统,因此我引入了Spring Boot Admin体系。14、Zuul已经停止更新支持,Spring Cloud官方推荐的二代网关Spring Cloud Gateway更加强大。15、微服务的安全架构体系虽然复杂,但是是有学习条例的,什么是认证授权、什么是OAuth2.0的原理、 JWT、怎么样去开发实现。 课程资料 【独家资料】1、课程附带全部63个项目源码,其中Hoxton版本项目源码37个,Edgware版本项目26个,2、230页高清PDF正版课件。3、附带nacos、consul、cmder等视频配套软件。学习方法1、每一节课程均有代码,较好的方式为一边听我的讲解,一边使用我提供的项目代码进行观察和运行。2、课程体系庞大,但是并不杂乱,每个章节只针对一个知识点,减轻学习压力。3、坚持每天学习1~2个章节,可以在地铁、公交上用手机学习。【完善知识体系图】

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风.foxwho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值