Fabio 是一個快速、現代、zero-conf 負載均衡 HTTP(S) 路由器,用於部署 Consul 管理的微服務。mysql
Fabio 由 eBay Classifieds Group 開發,用於處理 marktplaats.nl 和 kijiji.it 的流量。Marktplaats 全部的流量都通過 Fabio ,每秒有 250000 個請求,分發於數個 Fabio 實例,而且沒有出現任何延遲。linux
簡單流程圖(摘自 http://dockone.io/article/1567):ios
====== 服務註冊 ========= ========= A服務 <------> consul集羣 ----> 健康的 A/不健康的 A 集羣 ====== 健康檢查 ========= ========= ^ | 加入/移出路由表 | ======== fabio 集羣 ======== | | A服務 若是找到則成功路由不然返回錯誤 V http 請求
Mac OS 安裝命令(若是沒有安裝 Consul,會自動安裝):c++
$ brew install fabio Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> New Formulae crystal-icr gcc@4.7 gnome-autoar libchaos llvm@3.8 pdftoipe tomcat@8.0 dbt gcc@5 isl@0.12 libswiftnav mps-youtube recipes ucg gcc@4.6 gcc@6 lasso llvm@3.7 ndenv servus ==> Updated Formulae afflib dnscrypt-proxy handbrake mpv sassc ant doxygen hebcal mysql sbcl antigen duply highlight mysql-connector-c++ sqldiff antlr flex hugo mysql-sandbox sqlite apache-spark fluent-bit p_w_picpathmagick nagios sqlite-analyzer arangodb fobis ipv6calc nagios-plugins stunnel astyle fontforge jasper ncdc syncthing ats2-postiats fpp jenkins ncftp tarantool aws-sdk-cpp freetype lean-cli neo4j taylor awscli ganglia leptonica nvm tile38 bup gcc libass ooniprobe vim cdiff gcc@4.9 libgit2 opentsdb volatility cfr-decompiler geos libgit2-glib osm2pgsql vowpal-wabbit checkstyle git-annex libpng osquery weechat cloog git-lfs libxml2 pngpaste wine compcert gitg lmdb poco winetricks couchdb gitless m-cli pure-ftpd wireguard-tools cppcheck gmic macos*** qt5 x265 czmq gnome-builder menhir questdb yaz dbhash gnutls mg rclone youtube-dl dmd groonga mitmproxy re2 ==> Deleted Formulae cloog@0.18 qt tutum Error: Could not link: /usr/local/share/man/man1/brew.1 Please delete these paths and run `brew update`. ==> Installing dependencies for fabio: consul ==> Installing fabio dependency: consul ==> Downloading https://homebrew.bintray.com/bottles/consul-0.7.2.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring consul-0.7.2.sierra.bottle.tar.gz Error: The `brew link` step did not complete successfully The formula built, but is not symlinked into /usr/local Could not symlink bin/consul Target /usr/local/bin/consul already exists. You may want to remove it: rm '/usr/local/bin/consul' To force the link and overwrite all conflicting files: brew link --overwrite consul To list all files that would be deleted: brew link --overwrite --dry-run consul Possible conflicting files are: /usr/local/bin/consul ==> Caveats If consul was built with --with-web-ui, you can activate the UI by running consul with `-ui-dir /usr/local/Cellar/consul/0.7.2/share/consul/web-ui`. zsh completion has been installed to: /usr/local/share/zsh/site-functions To have launchd start consul now and restart at login: brew services start consul Or, if you don't want/need a background service you can just run: consul agent -dev -advertise 127.0.0.1 ==> Summary /usr/local/Cellar/consul/0.7.2: 5 files, 27.6M ==> Installing fabio ==> Downloading https://homebrew.bintray.com/bottles/fabio-1.3.5.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring fabio-1.3.5.sierra.bottle.tar.gz /usr/local/Cellar/fabio/1.3.5: 6 files, 10.1M
Ubuntu 安裝比較麻煩點,具體步驟:git
1. 建立fabio
目錄:github
$ mkdir /opt/fabio
2. 設置環境變量:web
$ export PATH=$PATH:/opt/fabio
3. 建立fabio.properties
文件(/opt/fabio
工做目錄下):sql
$ touch fabio.properties
4. 下載 Fabio 文件(地址:https://github.com/eBay/fabio/releases):macos
$ wget https://github.com/eBay/fabio/releases/download/v1.3.5/fabio-1.3.5-go1.7.3-linux_amd64
5. 賦予權限:apache
$ chmod a+x fabio-1.3.5-go1.7.3-linux_amd64
6. 設置fabio
命令:
$ ln -s fabio-1.3.5-go1.7.3-linux_amd64 fabio
安裝好以後,直接輸入命令(啓動 Fabio):
$ fabio 2017/01/04 03:04:44 [INFO] Runtime config { "Proxy": { "Strategy": "rnd", "Matcher": "prefix", "NoRouteStatus": 404, "MaxConn": 10000, "ShutdownWait": 0, "DialTimeout": 30000000000, "ResponseHeaderTimeout": 0, "KeepAliveTimeout": 0, "ReadTimeout": 0, "WriteTimeout": 0, "FlushInterval": 1000000000, "LocalIP": "10.0.2.15", "ClientIPHeader": "", "TLSHeader": "", "TLSHeaderValue": "", "GZIPContentTypesValue": "", "GZIPContentTypes": null }, "Registry": { "Backend": "consul", "Static": { "Routes": "" }, "File": { "Path": "" }, "Consul": { "Addr": "localhost:8500", "Scheme": "http", "Token": "", "KVPath": "/fabio/config", "TagPrefix": "urlprefix-", "Register": true, "ServiceAddr": ":9998", "ServiceName": "fabio", "ServiceTags": null, "ServiceStatus": [ "passing" ], "CheckInterval": 1000000000, "CheckTimeout": 3000000000 } }, "Listen": [ { "Addr": ":9999", "Proto": "http", "ReadTimeout": 0, "WriteTimeout": 0, "CertSource": { "Name": "", "Type": "", "CertPath": "", "KeyPath": "", "ClientCAPath": "", "CAUpgradeCN": "", "Refresh": 0, "Header": null }, "StrictMatch": false } ], "CertSources": {}, "Metrics": { "Target": "", "Prefix": "{{clean .Hostname}}.{{clean .Exec}}", "Names": "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}", "Interval": 30000000000, "GraphiteAddr": "", "StatsDAddr": "", "CirconusAPIKey": "", "CirconusAPIApp": "fabio", "CirconusAPIURL": "", "CirconusCheckID": "", "CirconusBrokerID": "" }, "UI": { "Addr": ":9998", "Color": "light-green", "Title": "" }, "Runtime": { "GOGC": 800, "GOMAXPROCS": 1 }, "ListenerValue": [ ":9999" ], "CertSourcesValue": null } 2017/01/04 03:04:44 [INFO] Version 1.3.5 starting 2017/01/04 03:04:44 [INFO] Go runtime is go1.7.3 2017/01/04 03:04:44 [INFO] Using routing strategy "rnd" 2017/01/04 03:04:44 [INFO] Using routing matching "prefix" 2017/01/04 03:04:44 [INFO] Setting GOGC=800 2017/01/04 03:04:44 [INFO] Setting GOMAXPROCS=1 2017/01/04 03:04:44 [INFO] Metrics disabled 2017/01/04 03:04:44 [INFO] consul: Connecting to "localhost:8500" in datacenter "dc1" 2017/01/04 03:04:44 [INFO] Admin server listening on ":9998" 2017/01/04 03:04:44 [INFO] HTTP proxy listening on :9999 2017/01/04 03:04:44 [INFO] consul: Using dynamic routes 2017/01/04 03:04:44 [INFO] consul: Using tag prefix "urlprefix-" 2017/01/04 03:04:44 [INFO] consul: Watching KV path "/fabio/config" 2017/01/04 03:04:44 [INFO] agent: Synced service 'fabio-vagrant-ubuntu-trusty-9998' 2017/01/04 03:04:44 [INFO] consul: Registered fabio with id "fabio-vagrant-ubuntu-trusty-9998" 2017/01/04 03:04:44 [INFO] consul: Registered fabio with address "10.0.2.15" 2017/01/04 03:04:44 [INFO] consul: Registered fabio with tags "" 2017/01/04 03:04:44 [INFO] consul: Registered fabio with health check to "http://[10.0.2.15]:9998/health" 2017/01/04 03:04:44 [INFO] consul: Health changed to #5878 2017/01/04 03:04:44 [INFO] consul: Manual config changed to #1 2017/01/04 03:04:44 [INFO] Updated config to 2017/01/04 03:04:44 [INFO] consul: Health changed to #5879 2017/01/04 03:04:44 [INFO] agent: Synced check 'service:fabio-vagrant-ubuntu-trusty-9998'
Fabio 啓動會鏈接本地的 Consul 服務,Fabio 經常使用地址(示例):
http://10.9.10.173:9998/health:服務能夠請求到這個地址,用於服務的健康檢查(Fabio 統一健康檢查)。
http://10.9.10.173:9999/{servicename}:服務的負載均衡地址,全部的服務調用都會請求到這個 Origin,Fabio 會進行服務分發。
http://10.9.10.173:9998:Fabio 後臺 UI 界面,能夠看到服務地址和權重,也能夠增長路由規則。
服務註冊的時候,須要以 Fabio 的方式進行註冊(SDK 進行實現)。
服務的路由須要添加{servicename}
(Fabio 識別具體的服務),好比:
[Route("server-test/values")] public class ValuesController : Controller { public string Get() { return "hello world"; } }
服務調用的時候,請求地址也須要添加{servicename}
(Fabio 識別具體的服務),好比:
var service = await _serviceRegistry.GetServiceInstanceAsync("server-test"); var serverUrl = $"{service.Address}:{service.Port}"; if (!serverUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) serverUrl = "http://" + serverUrl; var url = $"{serverUrl}/server-test/values"; var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url);
也能夠直接在瀏覽器中輸入服務負載均衡地址(測試 Fabio 是否正常),好比:
http://10.9.10.173:9999/server-test/values 200 hello world
Fabio 後臺 UI 界面,示例(http://10.9.10.173:9998/routes?filter=):