Niginx 集群负载均衡策略

Niginx 集群负载均衡策略

 

 所需物料

1.Nginx服务

步骤略

本人 nginx version: nginx/1.16.0

 

2.Java Servlet 测试项目

新建java web 项目,项目名称为:tt

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public IndexServlet() { }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //输出Session的Id
        System.out.println("[session-id]\t"+request.getSession().getId());
        //制造网络请求延迟效果
        try {
            System.out.println("[-线程睡眠中-]");
            Thread.sleep(3000);
            System.out.println("[-线程睡眠结束-]");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("");
        System.out.println("");
        System.out.println("");
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3.tomcat部署服务

共启用了3个Tomcat,服务端口分别是:8081、8082、8083;

分别访问 http://localhost:8081/tt/index

分别访问 http://localhost:8082/tt/index

分别访问 http://localhost:8083/tt/index

进行服务验证,看服务是否可以正常访问

 

 

 ----------------------------好戏开始了----------------------------

集群调度:轮询(默认)

调度规则:轮询调取集群中的服务;

修改 nginx.conf 配置文件,重启Nginx;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #配置集群集合
    upstream tomcatserver1 {
        server 127.0.0.1:8081;
        server 127.0.0.1:8082;   
        server 127.0.0.1:8083;     
    } 
    
    server {  
        listen       80;  
        server_name  localhost;  
        location / {  
            proxy_pass   http://tomcatserver1;  
            index  index.html index.htm;  
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

浏览器多次访问  http://localhost/tt/index 进行测试。会看到3个Tomcat 被轮训调用。

Niginx 集群负载均衡策略_第1张图片

 

集群调度:ip_hash

调度规则:同一个session会被分配到同一个 服务中,主要解决集群session问题;

修改 nginx.conf 配置文件,重启Nginx;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #配置集群集合
    upstream tomcatserver1 {
        #调度方式 ip_hash
        ip_hash; 
        server 127.0.0.1:8081;
        server 127.0.0.1:8082;   
        server 127.0.0.1:8083;     
    } 
    
    server {  
        listen       80;  
        server_name  localhost;  
        location / {  
            proxy_pass   http://tomcatserver1;  
            index  index.html index.htm;  
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

集群调度:Weight

调度规则:根据权重来处理,权重越高被调用的概率越高,主要用于后端服务器性能不均的情况;

修改 nginx.conf 配置文件,重启Nginx;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #配置集群集合
    upstream tomcatserver1 {
        #调度方式 Weight
        server 127.0.0.1:8081 weight=3;
        server 127.0.0.1:8082 weight=2;   
        server 127.0.0.1:8083 weight=1;     
    } 
    
    server {  
        listen       80;  
        server_name  localhost;  
        location / {  
            proxy_pass   http://tomcatserver1;  
            index  index.html index.htm;  
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

集群调度:url_hash

调度规则:相同的url地址会被分配到同一个服务器,用于缓存数据,如:我将系统图片都缓存在了8081、8082服务器,每当我请求图片的时候必须去请求8081 或 8082服务器;

修改 nginx.conf 配置文件,重启Nginx;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

	#配置集群集合
    upstream tomcatserver1 {
		#调度方式 url_hash
		hash $request_uri;
		server 127.0.0.1:8081;
		server 127.0.0.1:8082;   
		server 127.0.0.1:8083;     
	} 
	
	server {  
        listen       80;  
        server_name  localhost;
	# 假如请求静态资源的路径格式是 localhost:80/tt/state/xx/xx/……		
        location /tt/state {  
            proxy_pass   http://tomcatserver1;  
            index  index.html index.htm;  
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

 

 

你可能感兴趣的:(Niginx 集群负载均衡策略)