八、负载均衡
8.1 轮询策略(默认)
8.1.1 准备2台服务器
http://www.learndemo.com:8090/UserController/getById?id=6
http://www.learndemo.com:8092/UserController/getById?id=6
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4c8293305867b409adc180014d1d2887.png#pic_center)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ee6b13e1fe19f6baa7095806e9184958.png#pic_center)
8.1.2 配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#自定义格式
log_format myFormat '客户端IP地址:$remote_addr,请求状态:$status,请求处理时间:$request_time,发送给客户端总字节数:$bytes_sent,上游服务器:$upstream_addr';
#定义日志级别,日志位置
access_log logs/access.log myFormat;
server {
listen 7000;
server_name www.test.com www.test1.com;
root html/www/test;
location / {
index test.html;
}
location ~*\.(jpg|png|gif|js|css)$ {
root html/www/static;
}
}
upstream learnDemo{
server 60.205.188.229:8090;
server 60.205.188.229:8092;
}
server {
listen 8088;
server_name www.learnDemo.com;
location / {
proxy_pass http://learnDemo;
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0acd9994f986ca5d32d0d9cc3b63b4b6.png#pic_center)
- 请求www.learnDemo.com:8088 —代理—> learnDemo
- learnDemo有2个服务器,默认轮询服务器
8.1.3 刷新请求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/54d4071d47786eb3dde533e14fd8bd33.png#pic_center)
8.1.4 查看结果日志
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2877213d8d338d298e9c9e543b595b7b.png#pic_center)
8.2 轮询+权重策略
8.2.1 配置
upstream learnDemo{
server 60.205.188.229:8090 weight=1;
server 60.205.188.229:8092 weight=2;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5e8b8c3543a8d43f9c761277c8f5d0a6.png#pic_center)
8.2.2 结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b32fd9406c58c0525176b51b054a6428.png#pic_center)