Fabio(Go 语言):https://github.com/eBay/fabio
Fabio 是一个快速、现代、zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的微服务。
Fabio 由 eBay Classifieds Group 开发,用于处理 marktplaats.nl 和 kijiji.it 的流量。Marktplaats 所有的流量都经过 Fabio ,每秒有 250000 个请求,分发于数个 Fabio 实例,并且没有出现任何延迟。
简单流程图:
Mac OS 安装命令(如果没有安装 Consul,会自动安装):
$ brew install fabio
Ubuntu 安装比较麻烦点,具体步骤:
1.创建fabio目录:
$ mkdir /opt/fabio
2.设置环境变量:
$ export PATH=$PATH:/opt/fabio
3.创建fabio.properties文件(/opt/fabio工作目录下):
$ touch fabio.properties
4.下载 Fabio 文件(地址:https://github.com/eBay/fabio/releases):
$ wget https://github.com/eBay/fabio/releases/download/v1.3.5/fabio-1.3.5-go1.7.3-linux_amd64
5.赋予权限:
$ 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
[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
}
[INFO] Version 1.3.5 starting
[INFO] Go runtime is go1.7.3
[INFO] Using routing strategy "rnd"
[INFO] Using routing matching "prefix"
[INFO] Setting GOGC=800
[INFO] Setting GOMAXPROCS=1
[INFO] Metrics disabled
[INFO] consul: Connecting to "localhost:8500" in datacenter "dc1"
[INFO] Admin server listening on ":9998"
[INFO] HTTP proxy listening on :9999
[INFO] consul: Using dynamic routes
[INFO] consul: Using tag prefix "urlprefix-"
[INFO] consul: Watching KV path "/fabio/config"
[INFO] agent: Synced service 'fabio-vagrant-ubuntu-trusty-9998'
[INFO] consul: Registered fabio with id "fabio-vagrant-ubuntu-trusty-9998"
[INFO] consul: Registered fabio with address "10.0.2.15"
[INFO] consul: Registered fabio with tags ""
[INFO] consul: Registered fabio with health check to "http://[10.0.2.15]:9998/health"
[INFO] consul: Health changed to #5878
[INFO] consul: Manual config changed to #1
[INFO] Updated config to
[INFO] consul: Health changed to #5879
[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=):