grpc(4):使用 golang 調用consul api 介面,註冊user-tomcat服務

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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 srcimport (    "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)    }}

代碼很簡單,建立了一個consul的服務,說明tomcat的服務連接埠,ip。並且聲明了一個check方法用來檢查服務是否可用。

可以通過ui介面觀察服務註冊情況:

服務可用。

check失敗服務不可用。

3,使用dig命令檢查服務

yum install bind-utils

在伺服器上面直接查看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

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的代碼邏輯。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.