栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

细说 Nginx: 负载均衡 Load Balance

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

细说 Nginx: 负载均衡 Load Balance

细说 Nginx: 负载均衡 Load Balance

文章目录

细说 Nginx: 负载均衡 Load Balance准备服务负载均衡配置项

负载均衡策略更多配置项 示例

ip_hash轮询 + 权重 参考连接完整代码示例

准备服务

首先我们先准备三个后端服务,起在 8081~8083 端口上

server.js

const express = require('express');

const createServer = (ports) => {
  const countMap = {}; // port => count

  ports.forEach((port, i) => {
    const id = i + 1;

    const app = express();

    let count = 0;
    app.get('/', (req, res) => {
      count += 1;
      countMap[id] = count;
      res.send({
        server: id,
        count: countMap,
      });
    });

    app.listen(port, () => {
      console.log(`server${id} listen on http://localhost:${port}`);
    });
  });
};

createServer([8081, 8082, 8083]);

我们使用 countMap 来记录每个服务响应请求的次数

负载均衡配置项

接下来是 nginx 的配置项,核心的指令是 upstream

http {
    upstream hello_server {
        server localhost:8081;
        server localhost:8082;
        server localhost:8083;
    }

    server {
        listen          8999;
        server_name     localhost;

        location / {
          proxy_pass  http://hello_server;
        }
    }
}

我们在 http 块里面添加 upstream 块,upstream 块内部再用 server 声明候选服务的域;最后再将 8999 代理到该服务上能够实现负载均衡

负载均衡策略

默认的情况下 nginx 采用轮询每个单体服务的方式,我们也可以指定负载策略

最少连接数优先(least connection)

upstream hello_server {
    least_conn;

    # servers ...
}

改策略可以从连接数量上平衡请求

ip 映射

upstream hello_server {
    ip_hash;

    # servers ...
}

有些时候需要保证同一个 client 持续跟同一个 server 对接(可能使用 session 持久化等),这时候就使用 ip_hash 来保证多次连接使用同一个服务器

更多配置项

nginx 还提供了更多关于负载均衡的配置项,例如为 server 配置权重(weight)、server 的健康检查(失败次数限制 max_fails、超时检测 fail_timeout 等),需要用到再去配配看

传送门:Using nginx as HTTP load balancer - Health checks

示例

下面我们演示几个例子,直接使用上面的配置项并稍微做一点点的修改

ip_hash
upstream hello_server {
    ip_hash;

    server localhost:8081;
    server localhost:8082;
    server localhost:8083;
}

使用 ip_hash 之后我们可以看到多次请求之后一直都是同一个 server 被选中

轮询 + 权重
upstream hello_server {
    server localhost:8081 weight=3;
    server localhost:8082;
    server localhost:8083;
}

默认的轮询策略下同时为 server:8081 加上权重 3,可以看到连接的处理次数大致是其他服务的三倍


参考连接
Titlelink
Using nginx as HTTP load balancerhttps://nginx.org/en/docs/http/load_balancing.html
完整代码示例

https://github.com/superfreeeee/Blog-code/tree/main/deployment/nginx/nginx_detail_load_balancer

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/748452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号