1,关于consul


dubbo的注册中心是zookeeper,redis。 
motan的注册中心是zookeeper,consul。 
kubernetes的注册中心是 etcd。 
使用consul的好处是服务发现啥的都支持了。 
可以使用域名进行负载均衡。 
也是一个不错的 Server-Side Discovery Pattern 。

2,启动consul服务,调用接口


首先要在服务器安装一个consul服务: 
http://blog.csdn.net/freewebsys/article/details/56296013 
然后下载go的客户端。 
go get github.com/hashicorp/consul 
然后就可以使用 consul api服务了。

package src

import (
    "fmt"
    consulapi "github.com/hashicorp/consul/api"
    "log"
    "testing"
)

const Id = "1234567890"

func TestRegister(t *testing.T) {

    fmt.Println("test begin .")
    config := consulapi.DefaultConfig()
    //config.Address = "localhost"
    fmt.Println("defautl config : ", config)
    client, err := consulapi.NewClient(config)
    if err != nil {
        log.Fatal("consul client error : ", err)
    }
    //创建一个新服务。
    registration := new(consulapi.AgentServiceRegistration)
    registration.ID = Id
    registration.Name = "user-tomcat"
    registration.Port = 8080
    registration.Tags = []string{"user-tomcat"}
    registration.Address = "127.0.0.1"

    //增加check。
    check := new(consulapi.AgentServiceCheck)
    check.HTTP = fmt.Sprintf("http://%s:%d%s", registration.Address, registration.Port, "/check")
    //设置超时 5s。
    check.Timeout = "5s"
    //设置间隔 5s。
    check.Interval = "5s"
    //注册check服务。
    registration.Check = check
    log.Println("get check.HTTP:",check)

    err = client.Agent().ServiceRegister(registration)



    if err != nil {
        log.Fatal("register server error : ", err)
    }

}

func TestDregister(t *testing.T){


    fmt.Println("test begin .")
    config := consulapi.DefaultConfig()
    //config.Address = "localhost"
    fmt.Println("defautl config : ", config)
    client, err := consulapi.NewClient(config)
    if err != nil {
        log.Fatal("consul client error : ", err)
    }

    err = client.Agent().ServiceDeregister(Id)
    if err != nil {
        log.Fatal("register server error : ", err)
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

代码很简单,创建了一个consul的服务,说明tomcat的服务端口,ip。并且声明了一个check方法用来检查服务是否可用。

可以通过ui界面观察服务注册情况: 

服务可用。 

check失败服务不可用。

3,使用dig命令检查服务


yum install bind-utils
  • 1

在服务器上面直接查看user-tomcat 服务情况:

# dig @10.0.2.15 -p 8600 user-tomcat.service.consul SRV

; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> @10.0.2.15 -p 8600 user-tomcat.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17543
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;user-tomcat.service.consul.    IN      SRV

;; ANSWER SECTION:
user-tomcat.service.consul. 0   IN      SRV     1 1 8080 consul-dev.node.dc1.consul.

;; ADDITIONAL SECTION:
consul-dev.node.dc1.consul. 0   IN      A       127.0.0.1

;; Query time: 0 msec
;; SERVER: 10.0.2.15#8600(10.0.2.15)
;; WHEN: Sun Mar 05 03:06:06 EST 2017
;; MSG SIZE  rcvd: 100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

consul-dev.node.dc1.consul. 0 IN A 127.0.0.1 
可以查询到一个域名节点。

4,总结


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/60466381 
未经博主允许不得转载。 
博主地址是:http://blog.csdn.net/freewebsys

总体上感觉 consul 还是非常简单实用的。 
在做 Server-side Discovery 的时候是非常的方便的。 
可以降低client的代码逻辑。

Logo

开源、云原生的融合云平台

更多推荐