gorouter調研
最近在公司調研docker集群方案,涉及到 router這一層,有兩個可選方案,源于cloudfoundry的 gorouter & 源于dotcloud的 hipache , 因為對golang實現的gorouter比較有好感,就主要調研了下。
gorouter介紹
項目地址: https://github.com/cloudfoundry/gorouter/
Gorouter來源于CloudFoundry,后文簡稱為router。它是整個平臺的流量入口,負責分發所有的http請求到對應的 instance。它在內存中維護了一張路由表,記錄了域名與實例的對應關系,所謂的實例自動遷移,靠得就是這張路由表,某實例宕掉了,就從路由表中剔 除,新實例創建了,就加入路由表。
Gorouter依賴
Gorouter架構中所處的位置
無論是在cloudfoundry還是在我們設計的容器體系中,都是作為流量入口存在。
- 在CloudFoundry架構中的位置:
- 在設計的容器方案中的位置:
Gorouter性能
需要了解兩個組件的性能,一個是gorouter本身,另一個是他依賴的Gnatsd,總體感覺性能不錯。
Gorouter,官網沒有它的proxy性能數據,只是說它的邏輯簡單,性能很好,后期可以專門對它的轉發性能做一下測試。
Gnatsd:性能數據來自其官方:
With gnatsd (Golang-based server), NATS can send up to 6 MILLION MESSAGES PER SECOND.
Here's a detailed Performance Comparison between NATS, Redis, NSQ, RabbitMQ, and more. The below chart compares throughput for 4k payloads:

Gorouter部署
一個比較典型的gorouter部署架構為:

其中,需要關注的是RouteFlush這一塊,他的作用是將需要進行proxy的uri rule publish給gnatsd,從而使gorouter可以從gnatsd處sub到&生效,同時,以一定的頻率對現有rule進行 publish 刷新,因為gorouter只對rule保留時間T(在config中配置,默認120s)。
Routeflush需要自行實現。
Gorouter使用
-
Goroute監聽router.register、router.unregister等幾個頻道。
Publish router.register&router.unregister的數據體格式為:
{ "host": "127.0.0.1", //后端映射的host "port": 4567, //后端映射的port "uris": [ "my_first_url.vcap.me", //對應的域名1 "my_second_url.vcap.me" //對應的域名2 ], "tags": { "another_key": "another_value", "some_key": "some_value" }, "app": "some_app_guid",//app id "private_instance_id": "some_app_instance_id" // instance id }
-
gorouter自帶兩個http終端:
/varz: 自身狀態監控
/routes: 目前承載的proxy rules list
具體說明&config說明見官方說明: https://github.com/cloudfoundry/gorouter