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
参数,我们可以在 172.28.128.3:8500/ui
访问到consul的web ui管理界面,看到各个服务的状态.
对比
注册容器外IP:
每个注册的service的port都是变化的,并且因为映射内部port到了host,外部可以随意访问,私密性较弱。
注册容器内IP:
每个注册的service的port都是固定的,只能从容器内部访问。如果用 flannel,可能有一些性能损失。
DNS服务发现
查了一下如何利用DNS SRV类型来发现服务。本来以为可以用类似 Dial("hello", SRV)
的魔法 (我们都是膜法师,+1s), 查了一些资料貌似没有这么方便。看了下golang的net包,发现了两个方法 LookupSRV
, LookupHost
, 于是测试了一下,看下结果,大家知道该怎么用了吧,嘿嘿。
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>)