Fabio 安装和简单使用

4 篇文章 2 订阅
2 篇文章 0 订阅

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 实例,并且没有出现任何延迟。

简单流程图(摘自 http://dockone.io/article/1567):

======    服务注册     =========          =========
A服务     <------>    consul集群   ---->  健康的 A/不健康的 A 集群
======    健康检查     =========          =========
                                     ^
                                     |  加入/移出路由表
                                     |
                                  ========
                                   fabio 集群
                                  ========
                                     |
                                     | A服务   如果找到则成功路由否则返回错误
                                     V
                                    http 请求


Mac OS 安装命令(如果没有安装 Consul,会自动安装):

$ 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.0dbt             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             imagemagick            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                   macosvpn               qt5                    x265
czmq                   gnome-builder          menhir                 questdb                yaz
dbhash                 gnutls                 mg                     rclone                 youtube-dl
dmd                    groonga                mitmproxy              re2
==> Deleted Formulae
cloog@0.18                            qt                                    tutumError: Could not link:
/usr/local/share/man/man1/brew.1Please 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.gzError: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/localCould 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 consulTo list all files that would be deleted:
  brew link --overwrite --dry-run consulPossible conflicting files are:
/usr/local/bin/consul
==> CaveatsIf consul was built with --with-web-ui, you can activate the UI by runningconsul 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-functionsTo have launchd start consul now and restart at login:
  brew services start consulOr, 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 安装比较麻烦点,具体步骤:

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):

API server listening at: 127.0.0.1:33178
2018/10/30 15:05:24 [INFO] Setting log level to INFO
2018/10/30 15:05:55 [INFO] Runtime config
{
    "Proxy": {
        "Strategy": "rnd",
        "Matcher": "prefix",
        "NoRouteStatus": 404,
        "MaxConn": 10000,
        "ShutdownWait": 0,
        "DialTimeout": 30000000000,
        "ResponseHeaderTimeout": 0,
        "KeepAliveTimeout": 0,
        "FlushInterval": 1000000000,
        "GlobalFlushInterval": 0,
        "LocalIP": "10.128.23.4",
        "ClientIPHeader": "",
        "TLSHeader": "",
        "TLSHeaderValue": "",
        "GZIPContentTypes": null,
        "RequestID": "",
        "STSHeader": {
            "MaxAge": 0,
            "Subdomains": false,
            "Preload": false
        }
    },
    "Registry": {
        "Backend": "consul",
        "Static": {
            "NoRouteHTML": "",
            "Routes": ""
        },
        "File": {
            "NoRouteHTMLPath": "",
            "RoutesPath": ""
        },
        "Consul": {
            "Addr": "registd.fcae.io",
            "Scheme": "http",
            "Token": "",
            "KVPath": "/fabio/config",
            "NoRouteHTMLPath": "/fabio/noroute.html",
            "TagPrefix": "urlprefix-",
            "Register": true,
            "ServiceAddr": ":9998",
            "ServiceName": "fabio",
            "ServiceTags": null,
            "ServiceStatus": [
                "passing"
            ],
            "CheckInterval": 1000000000,
            "CheckTimeout": 3000000000,
            "CheckScheme": "http",
            "CheckTLSSkipVerify": false,
            "CheckDeregisterCriticalServiceAfter": "90m",
            "ChecksRequired": "one"
        },
        "Timeout": 10000000000,
        "Retry": 500000000
    },
    "Listen": [
        {
            "Addr": ":9999",
            "Proto": "http",
            "ReadTimeout": 0,
            "WriteTimeout": 0,
            "CertSource": {
                "Name": "",
                "Type": "",
                "CertPath": "",
                "KeyPath": "",
                "ClientCAPath": "",
                "CAUpgradeCN": "",
                "Refresh": 0,
                "Header": null
            },
            "StrictMatch": false,
            "TLSMinVersion": 0,
            "TLSMaxVersion": 0,
            "TLSCiphers": null
        }
    ],
    "Log": {
        "AccessFormat": "common",
        "AccessTarget": "",
        "RoutesFormat": "delta",
        "Level": "INFO"
    },
    "Metrics": {
        "Target": "",
        "Prefix": "{{clean .Hostname}}.{{clean .Exec}}",
        "Names": "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
        "Interval": 30000000000,
        "Timeout": 10000000000,
        "Retry": 500000000,
        "GraphiteAddr": "",
        "StatsDAddr": "",
        "Circonus": {
            "APIKey": "",
            "APIApp": "fabio",
            "APIURL": "",
            "CheckID": "",
            "BrokerID": ""
        }
    },
    "UI": {
        "Listen": {
            "Addr": ":9998",
            "Proto": "http",
            "ReadTimeout": 0,
            "WriteTimeout": 0,
            "CertSource": {
                "Name": "",
                "Type": "",
                "CertPath": "",
                "KeyPath": "",
                "ClientCAPath": "",
                "CAUpgradeCN": "",
                "Refresh": 0,
                "Header": null
            },
            "StrictMatch": false,
            "TLSMinVersion": 0,
            "TLSMaxVersion": 0,
            "TLSCiphers": null
        },
        "Color": "light-green",
        "Title": "",
        "Access": "rw"
    },
    "Runtime": {
        "GOGC": 800,
        "GOMAXPROCS": 4
    },
    "Tracing": {
        "TracingEnabled": false,
        "CollectorType": "http",
        "ConnectString": "http://localhost:9411/api/v1/spans",
        "ServiceName": "Fabiolb",
        "Topic": "Fabiolb-Kafka-Topic",
        "SamplerRate": -1,
        "SpanHost": "localhost:9998"
    },
    "ProfileMode": "",
    "ProfilePath": "C:\\Users\\lenovo\\AppData\\Local\\Temp",
    "Insecure": false,
    "GlobMatchingDisabled": false
}
2018/10/30 15:17:19 [INFO] Version 1.5.10 starting
2018/10/30 15:17:22 [INFO] Go runtime is go1.11
2018/10/30 15:17:29 [INFO] Metrics disabled
2018/10/30 15:17:29 [INFO] Setting GOGC=800
2018/10/30 15:17:29 [INFO] Setting GOMAXPROCS=4
2018/10/30 15:17:32 [INFO] consul: Connecting to "registd.fcae.io" in datacenter "mars"
2018/10/30 15:17:32 [INFO] Admin server access mode "rw"
2018/10/30 15:17:32 [INFO] Admin server listening on ":9998"
2018/10/30 15:17:32 [INFO] Waiting for first routing table
2018/10/30 15:17:32 [INFO] consul: Using dynamic routes
2018/10/30 15:17:32 [INFO] consul: Using tag prefix "urlprefix-"
2018/10/30 15:17:32 [INFO] consul: Watching KV path "/fabio/config"
2018/10/30 15:17:32 [INFO] consul: Watching KV path "/fabio/noroute.html"
2018/10/30 15:17:32 [INFO] consul: Registered fabio as "fabio"
2018/10/30 15:17:32 [INFO] consul: Registered fabio with id "fabio-lenovo-9998"
2018/10/30 15:17:32 [INFO] consul: Registered fabio with address "10.128.23.4"
2018/10/30 15:17:32 [INFO] consul: Registered fabio with tags ""
2018/10/30 15:17:32 [INFO] consul: Registered fabio with health check to "http://[10.128.23.4]:9998/health"
2018/10/30 15:17:32 [INFO] Config updates


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/values200hello world
Fabio 后台 UI 界面,示例(http://10.9.10.173:9998/routes?filter=):

640?wx_fmt=png

 

相关文章: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值