且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

consule服务注册和发现 安装 部署

更新时间:2022-09-16 13:27:53

安装部署参考

http://blog.sina.com.cn/s/blog_8ea8e9d50102wwlf.html

http://www.techweb.com.cn/network/system/2016-01-28/2270190.shtml


api连接参考 

https://github.com/JoergM/consul-examples


端口介绍

8500,客户端http api接口
8600,客户端DNS服务端口
8400,客户端RPC通信端口
8300,集群server RPC通信接口
8301,集群DC内部通信接口
8302,集群DC之间通信接口


consul的主要接口是RESTful HTTP API,该API可以用来增删查改nodes、services、checks、configguration



所有的endpoints主要分为以下类别:

1
2
3
4
5
6
7
kv - Key/Value存储
agent - Agent控制
catalog - 管理nodes和services
health - 管理健康监测
session - Session操作
acl - ACL创建和管理event - 用户Events
status - Consul系统状态


agent endpoints:agent endpoints用来和本地agent进行交互,一般用来服务注册和检查注册,支持以下接口

1
2
3
4
5
6
/v1/agent/checks : 返回本地agent注册的所有检查(包括配置文件和HTTP接口)
/v1/agent/services : 返回本地agent注册的所有 服务
/v1/agent/members : 返回agent在集群的gossip pool中看到的成员
/v1/agent/self : 返回本地agent的配置和成员信息/v1/agent/join/<address> : 触发本地agent加入node/v1/agent/force-leave/<node>>: 强制删除node
/v1/agent/check/register : 在本地agent增加一个检查项,使用PUT方法传输一个json格式的数据/v1/agent/check/deregister/<checkID> : 注销一个本地agent的检查项/v1/agent/check/pass/<checkID> : 设置一个本地检查项的状态为passing/v1/agent/check/warn/<checkID> : 设置一个本地检查项的状态为warning/v1/agent/check/fail/<checkID> : 设置一个本地检查项的状态为critical
/v1/agent/service/register : 在本地agent增加一个新的服务项,使用PUT方法传输一个json格式的数据/v1/agent/service/deregister/<serviceID> : 注销一个本地agent的服务项

catalog endpoints:catalog endpoints用来注册/注销nodes、services、checks

1
2
3
4
5
/v1/catalog/register : Registers a new node, service, or check/v1/catalog/deregister : Deregisters a node, service, or check/v1/catalog/datacenters : Lists known datacenters
/v1/catalog/nodes : Lists nodes in a given DC
/v1/catalog/services : Lists services in a given DC
/v1/catalog/service/<service> : Lists the nodes in a given service
/v1/catalog/node/<node> : Lists the services provided by a node

health endpoints:health endpoints用来查询健康状况相关信息,该功能从catalog中单独分离出来

1
2
3
4
/v1/healt/node/<node>: 返回node所定义的检查,可用参数?dc=
/v1/health/checks/<service>: 返回和服务相关联的检查,可用参数?dc=
/v1/health/service/<service>: 返回给定datacenter中给定node中service
/v1/health/state/<state>: 返回给定datacenter中指定状态的服务,state可以是"any""unknown""passing""warning", or "critical",可用参数?dc=


session endpoints:session endpoints用来create、update、destory、query sessions

1
2
3
4
5
/v1/session/create: Creates a new session
/v1/session/destroy/<session>: Destroys a given session
/v1/session/info/<session>: Queries a given session
/v1/session/node/<node>: Lists sessions belonging to a node
/v1/session/list: Lists all the active sessions


acl endpoints:acl endpoints用来create、update、destory、query acl

1
2
3
4
5
6
/v1/acl/create: Creates a new token with policy
/v1/acl/update: Update the policy of a token
/v1/acl/destroy/<id>: Destroys a given token
/v1/acl/info/<id>: Queries the policy of a given token
/v1/acl/clone/<id>: Creates a new token by cloning an existing token
/v1/acl/list: Lists all the active tokens


status endpoints:status endpoints用来或者consul 集群的信息

1
2
/v1/status/leader : 返回当前集群的Raft leader
/v1/status/peers : 返回当前集群中同事


服务注册:

 1. http方式

直接调用/v1/agent/service/register接口注册即可,需要注意的是:http method为PUT提交方式

1
curl -X PUT -d '{"id": "jetty","name": "test1111","address": "localhost","port": 8500,"tags": ["dev"],"checks": [{"http": "http://localhost:8500/","interval": "5s"}]}'     http://localhost:8500/v1/agent/service/register


2:通过配置文件的方式静态注册
    创建文件夹/etc/consul.d 
    .d代表有许多配置文件在里面
   vim /consul.d/jetty.json  内容如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{  
  "service":{  
    "id""jetty",  
    "name""jetty",  
    "address""192.168.1.200",  
    "port": 8080,  
    "tags": ["dev"],  
    "checks": [  
        {  
            "http""http://192.168.1.200:8080/health",  
            "interval""5s"  
        }  
    ]  
  }  
}

3. 使用python api 注册

http://python-consul.readthedocs.io/en/v0.7.0/




4.  中文consul翻译帮助文档

http://consul.la/intro/what-is-consul


5. consul 服务模板

https://github.com/hashicorp/consul-template

本文转自   tianshuai369   51CTO博客,原文链接:http://blog.51cto.com/kkkkkk/1914469