gorouter 安装部署

刘阳龙Herman

本文记录下 gorouter的 部署 & 启动 过程。

部署

  1. 先部署golang环境,下载golang 包:https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz,解压到 /home/work/local/go
  2. 增加环境变量:

    *echo "export GOROOT=/home/work/local/go" >> ~/.bashrc
    echo "export GOPATH=/home/work/local/golang" >> ~/.bashrc
    echo "export GOBIN=$GOROOT/bin" >> ~/.bashrc
    echo "export PATH=$PATH:$GOROOT/bin:$GOPATH/bin" >> ~/.bashrc*
    

    重新登录生效
    注意:如果没有GOPATH的路径,需要提前mkdir
    GOROOT标示 golang可执行程序本身的路径;GOPATH标示下载的golang库的路径;

  3. 下载部署 gnatsd:

        go get -v github.com/apcera/gnatsd
    

    顺便编译test工具:

        go get -v github.com/apcera/nats
        cd $GOPATH/src/github.com/apcera/nats/examples
        go install nats-pub.go
        go install nats-sub.go
    
    1. 部署安装gorouter
    go get -v github.com/cloudfoundry/gorouter
    

    注意:由于master版本中与依赖cf-debug-server 不太兼容的因故,会出现error:not enough arguments in call to cf_debug_server.Run

    cd $GOPATH/src/github.com/cloudfoundry-incubator/cf-debug-server
    git reset --hard 6e8c6102596e3fb6a17511f9f39fae64c89144a7
    go get -v github.com/cloudfoundry/gorouter
    

启动

1.启动gnatsd,cluster配置文件内容为:

# Cluster config file
port: 4222
#net: apcera.me # net interface
http_port: 8222
authorization {
  user:     admin
  password: admin
  timeout:  1
}
pid_file: '/tmp/nats_cluster_test.pid'
log_file: '/tmp/nats_cluster_test.log'
cluster {
  host: '0.0.0.0'
  port: 4244
  authorization {
    user: work
    password: homelink
    timeout: 1
  }
  # Routes are actively solicited and connected to from this server.
  # Other servers can connect to us if they supply the correct credentials
  # in their routes definitions from above.
  routes = [
    nats-route://admin:[email protected]:4222   #与其他server相通讯
    #nats-route://admin:[email protected]:4222  #本机地址,注意,一定不要加上,此行为注释行,否则会出现消息循环
  ]
}

启动命令:

gnatsd -c /home/work/local/gnates/conf/cluster.conf &

2.启动gorouter
配置文件格式为yaml,内容如下:

status:
  port: 8082
  user: admin
  pass: admin

nats:
  - host: "172.16.5.46"
    port: 4222
    user: admin
    pass: admin

logging:
  file: /home/work/var/log/gorouter.log
  syslog:
  level: debug

port: 8081
index: 0

go_max_procs: 8

publish_start_message_interval: 30
prune_stale_droplets_interval: 30
droplet_stale_threshold: 31536000
secure_cookies: true

启动命令:

gorouter -c /home/work/local/gorouter/conf/config.yml & 

注册路由

1.注册路由规则:

nats-pub  -s nats://admin:[email protected]:4222  'router.register'  '{"host":"172.16.5.44","port":49158,"uris":["puppet-master.lianjia.com"],"app": "puppet-master","private_instance_id": "1"}'

2.取消注册路由:

nats-pub  -s nats://admin:[email protected]:4222  'router.unregister'  '{"host":"172.16.5.44","port":49158,"uris":["puppet-master.lianjia.com"],"app": "puppet-master","private_instance_id": "1"}'

gorouter状态监控

curl http://admin:[email protected]:8082/healthz 返回 “ok”
curl http://admin:[email protected]:8082/routes 返回当前维护的路由,如:{"puppet-master.lianjia.com":["172.16.5.44:49158"]}
curl http://admin:[email protected]:8082/varz 返回典型值为:

{
    "bad_gateways": 191,
    "bad_requests": 11,
    "cpu": 0.034995,
    "credentials": [
        "admin",
        "admin"
    ],
    "droplets": 8,
    "host": "172.16.5.36:8082",
    "index": 0,
    "latency": {
        "50": 0.0239943865,
        "75": 0.25981205525,
        "90": 1.4904427468000003,
        "95": 3.3422940459999895,
        "99": 34.06178940115,
        "samples": 1,
        "value": 5e-7
    },
    "log_counts": {
        "debug": 25834,
        "info": 3,
        "warn": 959
    },
    "mem": 26556,
    "ms_since_last_registry_update": 167566713,
    "num_cores": 24,
    "rate": [
        3.3210746674119997,
        4.02630180149327,
        4.087743383607298
    ],
    "requests": 298078,
    "requests_per_sec": 3.3210746674119997,
    "responses_2xx": 182384,
    "responses_3xx": 87039,
    "responses_4xx": 27989,
    "responses_5xx": 99,
    "responses_xxx": 191,
    "start": "2015-05-13 20:53:56 +0800",
    "tags": {
        "component": {}
    },
    "top10_app_requests": [
        {
            "application_id": "8.lianjia.com",
            "rpm": 171,
            "rps": 2
        },
        {
            "application_id": "m.lianjia.com",
            "rpm": 40,
            "rps": 0
        }
    ],
    "type": "Router",
    "uptime": "0d:0h:0m:0s",
    "urls": 3,
    "uuid": "0-098bea5f-bc28-46bf-429c-ffed768e17a1"
}

gorouter性能测试

使用ab进行测试,请求单个html,在后端不变的情况下,请求参数分别为:

单并发2万请求; 
50并发2万请求;
100并发2万请求;
200并发2万请求;
300并发2万请求;
400并发2万请求;

略过数据贴结论:
结论,单个gorouter的请求量在并发200-300为最佳,性能为直接请求的三分之二左右。

注意:目前gorouter还不支持https、ssl/tls ,类似功能可用 iPtables/lvs代替。

阅读 5.5k

Herman.Liu
持续关注devops

Herman.Liu.1988.avi

669 声望
106 粉丝
0 条评论

Herman.Liu.1988.avi

669 声望
106 粉丝
文章目录
宣传栏