V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Mei99
V2EX  ›  NGINX

为什么用 Nginx 做了负载均衡之后 QPS 还不如直接压测 Tomcat?

  •  
  •   Mei99 · 2019-09-02 15:48:45 +08:00 · 8190 次点击
    这是一个创建于 1696 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Tomcat 负责收 POST 请求并写单机的 Redis,两个 Tomcat 前面挂了一个 Nginx 做转发,配置都是 6 核 4G 内存独立机器,和压测机都在一个局域网内,用 Jmeter 直接压测 Tomcat 能跑到 8000QPS,压测 Nginx 只有 5000 多,我该怎么做?
    31 条回复    2019-09-03 11:44:53 +08:00
    BBCCBB
        1
    BBCCBB  
       2019-09-02 15:52:10 +08:00
    这是肯定的, 挂个 nginx 中间多了一层转发消耗, 挂 nginx 是为了其他的功能,比如多实例水平扩展, 负载均衡, 做统一的权限认证等.
    opengps
        2
    opengps  
       2019-09-02 15:54:56 +08:00
    nginx 是为了多台同职责机器对外统一服务入口,多台机器做负载均衡,不是提高单机性能
    Mei99
        3
    Mei99  
    OP
       2019-09-02 15:56:13 +08:00
    @opengps 可是 Nginx 不是说轻松达到几万 QPS 吗?那压测 Nginx 的话不应该是接近 Tomcat 的 QPS*2 吗?
    opengps
        4
    opengps  
       2019-09-02 15:56:37 +08:00
    等等,楼主意思是负载均衡后 2 台才 5000,不如一台的 8000 ?
    Mei99
        5
    Mei99  
    OP
       2019-09-02 15:56:54 +08:00
    @BBCCBB 我挂 Nginx 就是为了给后面多个 Tomcat 做负载均衡啊....性能反而还下降了
    opengps
        6
    opengps  
       2019-09-02 15:58:57 +08:00
    关注下 Nginx 机器的网卡使用情况,压测时候是不是占满了带宽
    Mei99
        7
    Mei99  
    OP
       2019-09-02 16:14:39 +08:00
    @opengps 带宽应该没啥问题,内网千兆互联,看 Jmeter 最大发送速度才 2.5MB/s,Nginx 机器的 CPU 也没有跑满,压测机是 i9 9900K
    akira
        8
    akira  
       2019-09-02 16:21:03 +08:00
    是不是都只转发到一边去了。。
    gstqc
        9
    gstqc  
       2019-09-02 16:23:35 +08:00 via Android
    jmeter 有性能问题
    用 wrk 压吧
    zjlletian
        10
    zjlletian  
       2019-09-02 16:26:29 +08:00
    这就太多了要考虑的了,改下 nginx 线程数,连接数配置?
    zpfhbyx
        11
    zpfhbyx  
       2019-09-02 16:29:36 +08:00
    贴下 nginx 配置?
    Mei99
        12
    Mei99  
    OP
       2019-09-02 16:29:40 +08:00
    @zjlletian 线程和连接数一般多少合适?我改成和 ulimit 一样的 65535 了
    Mei99
        13
    Mei99  
    OP
       2019-09-02 16:30:05 +08:00
    @akira 只转到一边也不会比直接压测 Tomcat 性能还低吧....
    Mei99
        14
    Mei99  
    OP
       2019-09-02 16:39:05 +08:00
    @zpfhbyx 6 核的 i5 8500,6 个 worker 进程
    zpfhbyx
        15
    zpfhbyx  
       2019-09-02 16:41:09 +08:00
    @Mei99 额 我意思是配置文件..
    LeeSeoung
        16
    LeeSeoung  
       2019-09-02 17:35:51 +08:00
    看 nginx log 看 tomcat log
    Xusually
        17
    Xusually  
       2019-09-02 17:39:37 +08:00
    一般情况下不会的。。大概率是 NGINX 配置问题或者 NGINX 那台机器的内核参数配置问题。
    cloudyplain
        18
    cloudyplain  
       2019-09-02 17:41:56 +08:00
    可能是 nginx 配置的问题,是否是长连接、upstream 连接池是否足够大、网络问题等等
    janxin
        19
    janxin  
       2019-09-02 17:51:13 +08:00
    我盲猜可能是 nginx 相关配置的问题
    gz911122
        20
    gz911122  
       2019-09-02 17:54:05 +08:00
    盲猜两个 tomcat 在一个机器上
    chendy
        21
    chendy  
       2019-09-02 18:15:38 +08:00
    盲猜两个 tomcat 在一个机器上 +1
    lishunan246
        22
    lishunan246  
       2019-09-02 18:22:49 +08:00
    有可能是你 Jmeter 压测的时候线程数固定而且顺序发请求。
    由于多路过一台机器请求完成时间增加了 60%,比如说 1ms 变成了 1.6ms 。
    qps 自然就变成了原来的 5/8。
    jsy123392550
        23
    jsy123392550  
       2019-09-02 18:23:39 +08:00
    盲猜两个 tomcat 在一个机器上 +2
    stupil
        24
    stupil  
       2019-09-02 18:28:34 +08:00
    盲猜两个 tomcat 在一个机器上 +3
    autogen
        25
    autogen  
       2019-09-02 18:44:39 +08:00
    你有几台机器?
    janxin
        26
    janxin  
       2019-09-02 18:59:00 +08:00   ❤️ 1
    盲猜 Tomcat 在一个机器上你们是真没看吧...
    Mei99
        27
    Mei99  
    OP
       2019-09-02 19:05:01 +08:00
    nginx.conf:
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    worker_rlimit_nofile 65535;
    events {
    use epoll;
    worker_connections 65535;
    #multi_accept on;

    }

    http {
    upstream tomcat{
    server 192.168.50.166:8080;
    server 192.168.50.99:8080;
    keepalive 20480;
    }
    server{
    listen 8080;

    location / {
    proxy_pass http://tomcat;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    }
    }
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    server_tokens off;
    client_header_buffer_size 4k;
    client_max_body_size 10m;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    lishunan246
        28
    lishunan246  
       2019-09-02 19:18:11 +08:00
    预期能有 16000qps 现在只有 5000 的话,可以试试线程数*3 再压
    zhuisui
        29
    zhuisui  
       2019-09-02 19:42:33 +08:00
    worker_connections 和 keepalive 不是越高越好
    看看连接的状态
    realpg
        30
    realpg  
       2019-09-03 06:40:41 +08:00
    如果没有硬件瓶颈问题
    基本是 nginx 没配好
    我的低配机器 nginx 做入口,都能上万 QPS
    nekoyaki
        31
    nekoyaki  
       2019-09-03 11:44:53 +08:00
    楼主你的 192.168.50.166 和 192.168.50.99 这两台机器和 nginx 所在服务器之间的网络是什么网络? 网络延迟可能是一个比较大的因素,如果中间有路由器 /交换机,可能也得查查负载和延迟
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1493 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:57 · PVG 07:57 · LAX 16:57 · JFK 19:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.