nginx 负载均衡和node接口压测工具autocannon 插件的使用

278 阅读1分钟

nginx配置部分代码 默认4个服务器平均分配请求

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream node {
      server xx.xx.xxx.xx;
      server xx.xx.xxx.xx;
      server xx.xx.xxx.xx;
      server xx.xx.xxx.xx;
    }
    server {
        listen       8080;
        server_name  localhost;

        location / {
            proxy_pass http://node;
             #root   html;
            #index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

upstream 模块做负载均衡 fail_timeout和backup配合使用表示主服务最大延迟3秒钟,超过3秒则使用backup备用服务

upstream node {
  server xx.xx.xxx.xx fail_timeout=3;
  server xx.xx.xxx.xx fail_timeout=3;
  server xx.xx.xxx.xx fail_timeout=3;
  server xx.xx.xxx.xx backup;
}

使用服务器权重,表示每10次请求,第一个服务使用4次,第二个使用3次,以此类推

upstream node {
  server xx.xx.xxx.xx weight=4;
  server xx.xx.xxx.xx weight=3;
  server xx.xx.xxx.xx weight=2;
  server xx.xx.xxx.xx weight=1;
}

另外使用autocannon插件做接口压测

npm install -G autocannon

autocannon -c 210 -d 10 https://xxx.xxx.com/api/xxx/xxxx 
-c 是链接数量 -d持续压测时间 后面跟的是接口地址(如果测试需要参数,提前在接口写好默认参数)
-c/--connections NUM 并发连接的数量,默认10 
-p/--pipelining NUM 每个连接的流水线请求请求数。默认1
-d/--duration SEC 执行的时间,单位秒 
-m/--method METHOD 请求类型 默认GET 
-b/--body BODY 请求报文体

WeChat709d62a52e644a8e2948109e8b3f6c0f.png

初学nginx 如有错误,欢迎指正