準備
服務器
搭建三臺用于測驗的虛擬機
| 名稱 | IP | 服務 |
|---|---|---|
| node01 | 192.168.198.131 | Nginx、模擬業務(8080) |
| node02 | 192.168.198.130 | 模擬業務(8080) |
| node03 | 192.168.198.132 | 模擬業務(8080) |
修改hostname和hosts
$ vim /etc/hosts
192.168.198.131 node01
$ vim /etc/hostname
node01
$ reboot
## 其余兩臺也改下,并重啟使配置生效
在node01上安裝Nginx服務
$ echo -e "deb http://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx\ndeb-src http://nginx.org/packages/ubuntu/ $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$ wget -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install nginx
模擬業務
使用https://start.spring.io快速新建Spring Boot專案,添加Web模塊,并撰寫以下代碼:
@RestController
@RequestMapping("/test")
public class DemoController {
@GetMapping
public String test() throws UnknownHostException {
return "this is : " + Inet4Address.getLocalHost();
}
}
打包并部署到服務器,我使用的是The Application Plugin,部署完畢啟動

測驗下:
## node01
$ curl 192.168.198.131:8080/test
...
this is : node01/192.168.198.131
## node02
$ curl 192.168.198.130:8080/test
...
this is : node02/192.168.198.130
## node03
$ curl 192.168.198.132:8080/test
...
this is : node03/192.168.198.132
Nginx負載均衡
Round Robin(輪詢)
請求在服務器之間均勻分布,可以設定服務器權重,
$ vim /etc/nginx/conf/demo.conf
upstream backend {
server 192.168.198.131:8080;
server 192.168.198.132:8080;
server 192.168.198.130:8080;
}
server {
listen 80;
server_name 192.168.198.131;
location / {
proxy_pass http://backend;
}
}
$ service nginx restart
測驗下
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131 # node01
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132 # node03
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.130 # node02
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131 # node01
可以看到,每臺服務器訪問到的次數是相等的,
Least Connections
請求分配到連接數最少的服務器,可以設定服務器權重,
$ vim /etc/nginx/conf/demo.conf
upstream backend {
least_conn;
server 192.168.198.131:8080;
server 192.168.198.132:8080;
server 192.168.198.130:8080;
}
$ service nginx restart
這個不知道怎么模擬出連接數最少場景,
IP Hash
從客戶端的IP地址來確定請求應該發送給哪臺服務器,在這種情況下,使用IPv4地址的前三個八位位元組或整個IPv6地址來計算散列值,該方法能保證來自同一地址的請求分配到同一臺服務器,除非該服務器不可用,
$ vim /etc/nginx/conf/demo.conf
upstream backend {
ip_hash;
server 192.168.198.131:8080;
server 192.168.198.132:8080;
server 192.168.198.130:8080;
}
$ service nginx restart
測驗下
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
可以看到,請求都被分配到node01節點,
接下來,將node01節點關閉,看看會發生什么:
$ ps -ef | grep demo
root 3343 1764 0 11:52 pts/0 00:00:23 java -jar /home/demo/demo-boot-0.0.1-SNAPSHOT/lib/demo-0.0.1-SNAPSHOT.jar
root 4529 1764 0 13:11 pts/0 00:00:00 grep --color=auto demo
$ kill -9 3343
$ ps -ef | grep demo
root 4529 1764 0 13:11 pts/0 00:00:00 grep --color=auto demo
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
由于node01節點不可用,請求都被分配到node03節點,
Generic Hash
與上面的IP_HASH類似,通用HASH按照用戶定義的引數來計算散列值,引數可以是文本字串,變數或組合,例如,引數可以是遠端地址:
$ vim /etc/nginx/conf/demo.conf
upstream backend {
hash $remote_addr consistent;
server 192.168.198.131:8080;
server 192.168.198.132:8080;
server 192.168.198.130:8080;
}
$ service nginx restart
測驗下
$ curl 192.168.198.131/test
...
this is : node02/192.168.198.130
$ curl 192.168.198.131/test
...
this is : node02/192.168.198.130
$ curl 192.168.198.131/test
...
this is : node02/192.168.198.130
可以看到,請求都被分配到了node02節點,
??上面的consistent是可選引數,如果設定了,將采用Ketama一致性hash演算法計算散列值,
關于一致性Hash,可以查看我的另一篇博客:理解一致性Hash演算法
Random
請求會被隨機分配到一臺服務器,可以設定服務器權重,
$ vim /etc/nginx/conf/demo.conf
upstream backend {
random;
server 192.168.198.131:8080;
server 192.168.198.132:8080;
server 192.168.198.130:8080;
}
$ service nginx restart
測驗下
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
$ curl 192.168.198.131/test
...
this is : node02/192.168.198.130
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.130
可以看到,請求是被隨機分配到三臺服務器的,
Weights
除了設定負載均衡演算法,我們還可以為服務器設定權重,權重默認值是1
$ vim /etc/nginx/conf/demo.conf
upstream backend {
server 192.168.198.131:8080 weight=5;
server 192.168.198.132:8080 weight=10;
server 192.168.198.130:8080;
}
$ service nginx restart
測驗下
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node03/192.168.198.132
$ curl 192.168.198.131/test
...
this is : node01/192.168.198.131
可以看到,5次請求中,node03(weight=10)占了3次,node01(weight=5)占了2次,node02(weight=1)1次都沒有,
理論上來說,上面的配置,訪問16次,node03應被分配10次,node01應被分配5次,node02應被分配1次,
參考資料
http-load-balancer
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279202.html
標籤:其他
下一篇:二叉樹的重建
