• 2.8 Nginx负载均衡之ip_hash


             

    1. 作用: 是同一个用户(状态不更改的情况下: 例如ip),的所有请求会落到一台服务器上;

    2. 目的: 提高用户请求性能,提高程序吞吐性;

    3. 原理: (和HashMap类似)

          将用户的ip,通过hash算法得到的值 去 % 当前能够使用的服务器数量得到一个下标值;

          然后该下标值就决定用户请求落到哪一台服务器上;

    4. 使用方法: upstream 中 配置 ip_hash;

    5. 缺陷: 当使用ip_hash ,如果有一台用户机向,服务器发送大量请求;这台用户机的请求全部落到某一台服务器上,可能会导致这台服务器宕机;

    备注: 当使用 ip_hash, 需要停掉某一台服务器,不能直接删除,应该使用 down

    官网说明:

    Syntax:ip_hash;
    Default:
    Context:upstream

    Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.

    IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2.

    If one of the servers needs to be temporarily removed, it should be marked with the down parameter in order to preserve the current hashing of client IP addresses.

    Example:

    upstream backend {
        ip_hash;
    
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com down;
        server backend4.example.com;
    }
    
    
    Until versions 1.3.1 and 1.2.2, it was not possible to specify a weight for servers using the  ip_hash load balancing method.

    测试配置如下:

    1. #配置上游服务器
    2. upstream tomcats {
    3. ip_hash;
    4. server 192.168.93.129:8080;
    5. server 192.168.93.130:8080;
    6. server 192.168.93.131:8080;
    7. }
    1. server {
    2. listen 80;
    3. server_name 192.168.93.128;
    4. location / {
    5. proxy_pass http://tomcats;
    6. }
    7. }

  • 相关阅读:
    计算机网络体系结构(1.2)
    Spring 事务执行流程及源码分析
    如何使用 JMeter 进行 HTTPS 请求测试?
    Prompt之美:如何设计提示词让大模型变“聪明”
    卡尔曼滤波(Kalman Filter)原理浅析-数学理论推导-1
    基于FPGA的频率计与串口通信(一)
    华为网络设备高频命令
    Spring boot整合Activemq的原理
    【SpringBoot整合NoSql】-----ElasticSearch的安装与操作篇
    深圳市罗湖、龙岗、南山等7各区的研发补贴汇总
  • 原文地址:https://blog.csdn.net/Xx13624558575/article/details/126339372