docker run -d -p 9999:9999 -p 9998:9998 -v $PWD/fabio.properties:/etc/fabio/fabio.properties magiconair/fabio
測試gateway:
curl 172.28.128.3:9999/foo/barcurl 172.28.128.3:9999/foo/bar -H "Host: mysite.com"

Health Check
sudo ifdown eth1curl http://localhost:8500/v1/health/state/critical[{ "Node":"localhost.localdomain", "CheckID":"service:afa2769cd049:loving_shannon:9090", "Name":"Service 'hello' check", "Status":"critical", "Notes":"", "Output":"Get http://172.28.128.6:32768/foo/healthcheck: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)", "ServiceID":"afa2769cd049:loving_shannon:9090", "ServiceName":"hello", "CreateIndex":379, "ModifyIndex":457}]sudo ifup eth1
}
在啟動 consul的時候,我們使用了-ui
參數(shù),我們可以在 172.28.128.3:8500/ui
訪問到consul的web ui管理界面,看到各個服務(wù)的狀態(tài).

對比
注冊容器外IP:
每個注冊的service的port都是變化的,并且因為映射內(nèi)部port到了host,外部可以隨意訪問,私密性較弱。
注冊容器內(nèi)IP:
每個注冊的service的port都是固定的,只能從容器內(nèi)部訪問。如果用 flannel,可能有一些性能損失。
DNS服務(wù)發(fā)現(xiàn)
查了一下如何利用DNS SRV類型來發(fā)現(xiàn)服務(wù)。本來以為可以用類似 Dial("hello", SRV)
的魔法 (我們都是膜法師,+1s), 查了一些資料貌似沒有這么方便??戳讼耮olang的net包,發(fā)現(xiàn)了兩個方法 LookupSRV
, LookupHost
, 于是測試了一下,看下結(jié)果,大家知道該怎么用了吧,嘿嘿。
golangcname, addrs, err := net.LookupSRV("", "", "hello.service.consul")fmt.Printf("%s, %#v, %s \n", cname, addrs, err)for _, srv := range addrs {fmt.Printf("%#v \n", *srv)}newAddrs, err := net.LookupHost("hello.service.consul")fmt.Printf("%#v, %s \n", newAddrs, err)
//output[[email protected] dns]$ go run mx.gohello.service.consul., []*net.SRV{(*net.SRV)(0xc420010980), (*net.SRV)(0xc4200109a0)}, %!s(<nil>)net.SRV{Target:"bogon.node.dc1.consul.", Port:0x8003, Priority:0x1, Weight:0x1}net.SRV{Target:"bogon.node.dc1.consul.", Port:0x8000, Priority:0x1, Weight:0x1}[]string{"172.28.128.3", "172.28.128.4"}, %!s(<nil>)