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的IdSystem.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 被轮训调用。

集群调度: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_haship_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 {#调度方式 Weightserver 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_hashhash $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;}}}

转载于:https://www.cnblogs.com/devan/p/11237016.html

Niginx 集群负载均衡策略相关推荐

  1. 搭建集群负载均衡系统

    声明:本文是参考大量网上资料以及tigerlei自己的实际操作而写的笔记,仅供大家参考,绝非原创. 搭建集群负载均衡系统 负载均衡集群是在应用服务器高负载的情况下,由多台节点提供可伸缩的,高负载的服务 ...

  2. 服务器集群负载均衡(F5,LVS,DNS,CDN)区别以及选型

    服务器集群负载均衡(F5,LVS,DNS,CDN)区别以及选型 下面是"黑夜路人"的<大型网站架构优化(PHP)与相关开源软件使用建议> =============== ...

  3. Apache+Tomcat +mod_proxy集群负载均衡及session

      序言: 在玩Apache+Tomcat +mod_jk集群负载均衡及session的时候发现,还有一种方式可以实现,就是网上各位大牛们说的mod_proxy反向代理. 实在弄的我的知识细胞洋洋.实 ...

  4. 全面讲述linux集群负载均衡

    学习linux时,你可能会遇到linux集群的问题,这里将介绍linux集群负载均衡的方法,经过仔细整理,在这里拿出来和大家分享一下,希望本文能教会你更多东西. 集群原理 linux集群系统包括集群节 ...

  5. 前后端分离 集群负载均衡 分布式 微服务

    一.前后端分离 1.为什么要前后端分离 在以前传统的网站开发中,前端一般扮演的只是切图的工作,只是简单地将UI设计师提供的原型图实现成静态的HTML页面,而具体的页面交互逻辑,比如与后台的数据交互工作 ...

  6. 图文解说:Nginx+tomcat配置集群负载均衡

    图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用  作者:niumd Blog:http://ari.iteye ...

  7. nginx+双tomcat集群负载均衡(一台机器)

    nginx简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...

  8. 网络技术沙龙:主题:数据库优化、CDN、集群负载均衡(1.9日技术聚会召集)

    http://bbs.chinaunix.net/thread-1325584-1-1.html 沙龙:企业网络优化技术论坛(网络运维架构.负载均衡.数据库优化.CDN)(1.9日技术聚会召集) 前不 ...

  9. turbolinux mysql 5.0 cluste_--mysql 5.0 集群负载均衡—经过测试

    一.介绍 ======== 测试环境: Server1:ndbd 192.168.0.11 Server2:ndbd 192.168.0.12 Server3:mysqld --ndb-cluster ...

最新文章

  1. IT经理世界:专注莫如史玉柱
  2. /proc/asound详细介绍
  3. 28行代码AC——Minimum Sum LCM UVA - 10791(最大质因子)
  4. Faster R-CNN的安装及测试(Python版本和Matlab版本)
  5. 计算机视觉论文doc,嘉炬-计算机视觉论文资料.doc
  6. 第一次使用 Blog
  7. 解决DesignMode不能正确反应是否处于设计模式的问题
  8. mysql安全方面_MySQL数据库在网络安全方面功能有哪些呢?
  9. Computing Platform------系统平台及其系列
  10. linux内核C -- 第04课:Linux内核第一宏——container_of
  11. 学计算机的话美国社区大学对应课程,美国加州社区大学转UC伯克利计算机专业有什么课程上的要求?...
  12. easypoi 合并单元格 横向 纵向
  13. Qt编写安防视频监控系统65-子模块9数据调试
  14. 简单了解logger.debug
  15. windows打不开应用商店,edge浏览器不能登录同步
  16. Android传感器系列介绍-刘宇
  17. 计算机的标准输入法,计算机操作系统标准教程 第4章 五笔字型输入法.pdf
  18. 计算机毕业设计Java印染公司信息管理系统(系统+程序+mysql数据库+Lw文档)
  19. 软考分类精讲-计算机网络
  20. rt-link源码笔记,适用于自定义点对点的通信协议

热门文章

  1. windows上运行MapReduce出错(Failed to set permissions of path)
  2. linux-shell命令之file【辨识文件类型】
  3. [二叉树]二叉搜索树转换为双向链表(剑指Offer26)
  4. 什么叫临界资源和临界区?
  5. (原创总结) Quartus II 的在线调试方法
  6. 建立数据库的原则(怎样建立一个好的数据库)
  7. Python中从头开始实现神经网络 - 介绍
  8. MOSSE目标跟踪算法的理解
  9. 爬虫goodreads数据_使用Python从Goodreads数据中预测好书
  10. 完美下巴标准_平行下颚抓