最近使用到了记录一下

准备

启动两个springBoot:
localhost:8001
localhost:8002

Nginx配置

nginx.conf

worker_processes  1;
events {
  worker_connections  1024;
}
http {
   upstream  dalaoyang-server {
     server    localhost:8001;
     server    localhost:8002;
   }

  server {
    listen       8000;
    server_name  localhost;

    location / {
      proxy_pass http://dalaoyang-server;
      proxy_redirect default;
    }
  }
}

轮询
Nginx负载默认的方式,如果服务Down掉,可以自动剔除

upstream  dalaoyang-server {
   server    localhost:8001;
   server    localhost:8002;
}

权重
通过设置weight实现,通常用于服务器性能不统一,将性能好的分配高权重,发挥服务器最大性能

upstream  dalaoyang-server {
   server    localhost:8001 weight=1;
   server    localhost:8002 weight=2;
}

fair
根据机器的响应时间分配,响应时间短的优先

upstream  dalaoyang-server {
   server    localhost:8001 weight=1;
   server    localhost:8002 weight=2;
   fair;  
}

最少连接
将访问的请求发配给连接最少的机器上

upstream  dalaoyang-server {
   least_conn;
   server    localhost:8001 weight=1;
   server    localhost:8002 weight=2;
}

iphash
通过ip和hash进行分配,一般用于每个访客访问一台机器上。可以结合weight使用

upstream  dalaoyang-server {
   ip_hash; 
   server    localhost:8001 weight=1;
   server    localhost:8002 weight=2;
}

四层负载基于ip加端口,七层负载是在四层的基础上对url等应用层的分析实现流量智能化

Q.E.D.