Consul 服务发现和配置

Service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.

服务发现和配置,分布式,高可用性,数据中心。

  • 服务注册 - 服务端注册相应的的主机、端口号及其它身份验证信息,协议,版本号,以及运行环境等详细资料。
  • 服务发现 - 客户端应用通过向注册中心查询,获取可用服务列表,相应服务详细信息。
  • 基本服务格式:
     1 {
     2   "service":{
     3     "id": "myservice",
     4     "name": "myservice",
     5     "address": "servicehost",
     6     "port": serviceport,
     7     "tags": ["tag"],
     8     "checks": [
     9         {
    10             "http": "http://host.port/health",
    11             "interval": "5s"
    12         }
    13     ]
    14   }
    15 }

     

  • 健康检查(checks):
    script check:consul主动去检查服务的健康状况

    1 {
    2   "check": {
    3     "id": "scheck",
    4     "name": "scheck",
    5     "script": "/*.py", //必须
    6     "interval": "10s", //必须
    7     "timeout": "1s"
    8   }
    9 }

    ttl check:服务主动向consul报告自己的健康状况

    1 {
    2   "check": {
    3     "id": "scheck",
    4     "name": "scheck",
    5     "notes": "scheck",
    6     "ttl": "30s"
    7   }
    8 }

    http check:

    1 {
    2   "check": {
    3     "id": "scheck",
    4     "name": "scheck",
    5     "http": "http://host:port/health",
    6     "interval": "10s",
    7     "timeout": "1s"
    8   }
    9 }

    tcp check:

    1 {
    2   "check": {
    3     "id": "scheck",
    4     "name": "scheck",
    5     "tcp": "host:22",
    6     "interval": "10s",
    7     "timeout": "1s"
    8   }
    9 }
  • 服务注册:
    配置文件静态注册:
        /etc/consul.d/myserver.json
        添加如上服务配置
        重启consul,并将配置文件的路径给consul(指定参数:-config-dir /etc/consul.d)
    HTTP API接口来动态注册:
        /v1/agent/service/register http put方法注册
        url -X PUT -d '{"id": "myserver","name": "myserver","address": "serverhost","port": serverport,"tags": ["tag"],"checks": [{"http": "http://healthhost:port/health","interval": "5s"}]}' http://host:8500/v1/agent/service/register
  • maven dependency:
    1 <dependency>
    2     <groupId>com.orbitz.consul</groupId>
    3     <artifactId>consul-client</artifactId>
    4     <version>xxx</version>
    5 </dependency>
    1 <dependency>
    2     <groupId>com.ecwid.consul</groupId>
    3     <artifactId>consul-api</artifactId>
    4     <version>xxx</version>
    5 </dependency>

     

项目地址:https://github.com/windwant/windwant-service/tree/master/consul-service

转载于:https://www.cnblogs.com/niejunlei/p/5981963.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值