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 agentcatalog        Interact with the catalogevent          Fire a new eventexec           Executes a command on Consul nodesforce-leave    Forces a member of the cluster to enter the "left" stateinfo           Provides debugging information for operators.join           Tell Consul agent to join clusterkeygen         Generates a new encryption keykeyring        Manages gossip layer encryption keyskv             Interact with the key-value storeleave          Gracefully leaves the Consul cluster and shuts downlock           Execute a command holding a lockmaint          Controls node or service maintenance modemembers        Lists the members of a Consul clustermonitor        Stream logs from a Consul agentoperator       Provides cluster-level tools for Consul operatorsreload         Triggers the agent to reload configuration filesrtt            Estimates network round trip time between nodessnapshot       Saves, restores and inspects snapshots of Consul server statevalidate       Validate config files/directoriesversion        Prints the Consul versionwatch          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 agentcatalog        Interact with the catalogevent          Fire a new eventexec           Executes a command on Consul nodesforce-leave    Forces a member of the cluster to enter the "left" stateinfo           Provides debugging information for operators.join           Tell Consul agent to join clusterkeygen         Generates a new encryption keykeyring        Manages gossip layer encryption keyskv             Interact with the key-value storeleave          Gracefully leaves the Consul cluster and shuts downlock           Execute a command holding a lockmaint          Controls node or service maintenance modemembers        Lists the members of a Consul clustermonitor        Stream logs from a Consul agentoperator       Provides cluster-level tools for Consul operatorsreload         Triggers the agent to reload configuration filesrtt            Estimates network round trip time between nodessnapshot       Saves, restores and inspects snapshots of Consul server statevalidate       Validate config files/directoriesversion        Prints the Consul versionwatch          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.jsonecho '{"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

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

  1. Bumblebee微服务网关之consul服务发现

    网关需要维护相关负载的服务器,手动添加相对来说是一件比较麻烦的工作:为了解决这一问题组件扩展了一个基于consul服务发现插件,通过配置这个插件和启用后网关会自动从consul服务中获取服务并添加到网 ...

  2. 我是服务的执政官-服务发现和注册工具consul简介

    服务发现和注册 我们有了两个服务.服务A的IP地址是192.168.0.1,端口9001,服务B的IP地址192.168.0.2,端口9002.我们的客户端需要调用服务A和服务B,我们只需要在配置文件 ...

  3. java B2B2C springmvc mybatis电子商务平台源码-Consul服务发现原理...

    Consul 是什么 Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License ...

  4. 学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片

    第一部分:Consul 基础 1,Consul 介绍 官网文档描述:Consul 是一个网络工具,提供功能齐全的服务网格和服务发现. 它可以做什么:自动化网络配置,发现服务并启用跨任何云或运行时的安全 ...

  5. 基于Docker的Consul服务发现集群搭建

    在去年的.NET Core微服务系列文章中,初步学习了一下Consul服务发现,总结了两篇文章.本次基于Docker部署的方式,以一个Demo示例来搭建一个Consul的示例集群,最后给出一个HA的架 ...

  6. Redola.Rpc 集成 Consul 服务发现

    Redola.Rpc 解决了什么问题? Redola.Rpc 是一个使用 C# 开发的 RPC 框架,代码开源在 GitHub 上.目前版本仅支持 .NET Framework 4.6 以上版本,未来 ...

  7. 服务发现和注册和Eureka

    Spring Cloud和云计算没有关系,只是一个基于Spring Boot的快速构建分布式系统的工具集. 一 Spring Cloud特点 # 约定优于配置 # 开箱即用,快速启动 # 适用于各种环 ...

  8. 扩展gRPC支持consul服务发现和Polly策略

    gRPC由于需要用工具生成代码实现,可开发性不是很高,在扩展这方面不是很友好 最近研究了下,进行了扩展,不需要额外的工具生成,直接使用默认Grpc.Tools生成的代理类即可 相关源码在文章底部 客户 ...

  9. Netty游戏服务器实战开发(6):Netty整合Zookeeper实现分布式服务发现与注册

    1:Zookeeper基础 安装zookeeper.当然,很多时候我们会在Windows上开发,所以,我们需要在本地搭建一个zookeeper环境.方便开发过程中的测试. 首先我们去Apache上下载 ...

  10. 微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)

    1.什么是落地SOA(面向服务架构)? SOA面向服务架构,是一种架构思想,是跨语言和平台的.SOA宗旨简单明了,根据项目服务完成架构搭建,以服务为基准点完成组件化和模块化.提供服务是项目的基本内容, ...

最新文章

  1. 漫画|解读电气安全“十不准”
  2. 决策树-特征属性选择划分
  3. 【bzoj3631】[JLOI2014]松鼠的新家
  4. linux ip brd不一致_3 个方便的命令行网速度测试工具 | Linux 中国
  5. python getattr函数_Python中的getattr()函数详解
  6. 【NOIP2016提高A组模拟10.15】打膈膜
  7. python培训多久能入职_Python学到什么程度可以面试工作?
  8. 小程序wxParse
  9. 360 os3.0 android7.1,【360 N6 Pro】360OS安卓7.1系统V3.0.087付费纯净版ROOT刷机包
  10. html5 楼盘效果图,楼盘效果图图集
  11. 微信小程序之在线任务发布与接单平台
  12. 魅族设置语音录音服务器,魅族手机微信怎么开启录音权限呀有步骤图吗
  13. 七号信令中TUP协议的主要消息和故障问题
  14. 易企秀前端压缩源码分析与还原
  15. 安装青龙面板(不用购买服务器即可薅羊毛)Ubuntu
  16. Premiere Pro教程
  17. 教你几种方法最大限度减轻酒精对你身体的伤害
  18. C语言每日一练——第159天:佩奇存钱方案
  19. 体验服服务器更新维护,阴阳师体验服3月24日维护更新 体验服维护详情一览
  20. 功能最完善,代码最简洁的选项卡代码(div+css)

热门文章

  1. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
  2. inventor整理资源中心收藏夹
  3. 《深入浅出struts》读书笔记(2)
  4. 30096大学计算机应用基础,300道计算机应用基础试题(附答案)
  5. opencv外接矩形矫正
  6. 什么是通讯作者?和第一作者的区别有哪些?
  7. R-CNN学习笔记1:Selective Search for Object Recognition
  8. pyspark分类算法之决策树分类器模型实践【decisionTreeClassifier】
  9. 基于网页分析构思出的正文提取算法
  10. 单独学java_自学Java的几大误区是什么