nginx+tomcat实现Windows系统下的负载均衡搭建的案例
刚入行没多久就听过‘负载均衡’的大名,到现在因为工作接触的少,所以没什么太多的认识。但自己又对其非常的好奇,所以前两天通过查资料,在自己的笔记本上就搭建了一个超简单的案例(工作中没有时间,晚上到家了条件又不够,只能用自己的笔记本将就一下了,重在理解思想。。)
通俗点将,负载均衡就是因为访问流量太大,导致项目访问不流畅、甚至宕掉,所以通过一种分流的方式来缓解这种情况。
首先,安装两个tomcat,可以是同一个复制成两个,也可以下载两个不同版本的tomcat,我就是下载了两个不同版本的。下载地址:https://tomcat.apache.org/download-80.cgi
(这是8.0版本的,随便找两个不是特别老的版本的就行)。
然后启动两个tomcat,在启动前,先更改其中一个的端口号,使得两个tomcat启动时不会端口冲突,一个是本身的8080端口,一个是改成了9080端口。配好以后,打开cmd命令窗口,我的tomcat一个放在D:\software\apache-tomcat-8.5.24目录下,按照如下命令即可启动,启动成功会弹出另一个窗口,显示如下:
打开浏览器,输入http://localhost:9080/,出现如下界面即tomcat启动成功。另一个采用同样的步骤即可。
图1:tomcat8 图2:tomcat7
再之后,安装一个nginx,我装的是稳定版的nginx,下载地址:http://nginx.org/download/nginx-1.12.2.zip,解压即可使用
在启动前,必须要对nginx进行一下配置才可实现负载均衡的功能,打开conf文件夹,下面有一个nginx.conf文件,配置如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
#以下四行是新添加的,两个IP是两个tomcat的访问地址,weight表示分给该服务器的请求比重,两个都是1,则按照1:1来分配,
upstream netitcast.com{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#一下两行是进行修改的,http://netitcast.com和上面添加的要保持一致
location / {
proxy_pass http://netitcast.com;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
还是打开cmd窗口,进入到以上目录,执行命令:start nginx 即启动成功,然后输入网址:http://localhost/index.jsp,不断的进行访问,就会发现,上方显示的图1和图2在交互的显示。因为以上的配置weight是以1:2的比重来分配的,所以9080端口的比重就大一些,访问到图一(9080端口)的几率就比较大,访问到图二(8080端口)的几率比较小,这个概率一个是三分之二,一个是三分之一。
当然,这只是一个简单的示例,在生产环境中开发,肯定还要解决许多问题,比如‘会话保持’等,这个以后明白了再来补充。写这个的目的就是让自己对负载均衡更加熟悉,希望大家以后一起努力,加油!!!
nginx+tomcat实现Windows系统下的负载均衡搭建的案例的更多相关文章
- 通过Nginx+tomcat+redis实现反向代理 、负载均衡及session同步
一直对于负载均衡比较陌生,今天尝试着去了解了一下,并做了一个小的实验,对于这个概念有一些认识,在此做一个简单的总结 什么是负载均衡 负载均衡,英文 名称为Load Balance,指由多台服务器以对称 ...
- Windows系统下Android开发环境搭建
“工具善其事,必先利其器”.要想学好Android,搭建好Android开发环境是一个良好的开端. Windows系统下Android开发环境主要有4个大的步骤.分别是: 1.JDK的安装 2.ecl ...
- windows系统下Python环境的搭建及Selenium的安装
1.首先访问http://www.python.org/download/去下载最新的python版本: 2.下载安装包,一路安装完毕: 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...
- (转)windows系统下Python环境的搭建
原博文地址:http://www.cnblogs.com/windinsky/archive/2012/09/20/2695520.html 这段时间在做python,觉得这个配置环境的帖子还不错,分 ...
- Windows系统下JAVA开发环境搭建
首先我们需要下载JDK(JAVA Development Kit),JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. 下载地址:http://www.or ...
- windows系统下Python环境的搭建
1.下载最新的Python版本3.5.0.
- tomact在windows系统下安装
一.下载 下载地址: https://tomcat.apache.org/download-90.cgi 7,8,9的版本都可以下,这里下载最新版本 注意:Binary是编译好的,可以直接使用的版本, ...
- windows系统下nginx+tomcat+redis做负载均衡和session粘滞附整套解决方案
Nginx: 在nginx-1.8.0\conf目录下找到nginx.conf文件,打开文件修改文件中http{}中的内容,在http{}中加入 upstream localhost { serve ...
- Nginx + Tomcat Windows下的负载均衡配置
Nginx + Tomcat Windows下的负载均衡配置 一.为什么需要对Tomcat服务器做负载均衡? Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过50 ...
随机推荐
- perl(ExtUtils::Embed)依赖包
perl(ExtUtils::Embed) 被 ****需要 yum install perl-ExtUtils-Embed即可
- 蒲公英(bzoj2724)(分块+区间众数)
Input Output Sample Input 6 3 1 2 3 2 1 2 1 5 3 6 1 5 Sample Output 1 2 1 HINT \(n <= 40000\),$ m ...
- 【wireshark】插件开发(四):Lua插件Post-dissector和Listener
1. Post-dissector post-dissector和dissector不同,它会在所有dissectors都执行过后再被执行,这也就post前缀的由来.post-dissector的构建 ...
- iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】
前言 为什么要说 WKWebview,在之前做电子书笔记时已经提过 WKWebview 在iOS8之后已完全替代 Webview,原因就不多说了,主要还是内存过大: 封装 封装一个基于 UIViewC ...
- mapreduce程序的按照key值从大到小降序排列
在近期的Hadoop的学习中,在学习mapreduce时遇到问题:让求所给数据的top10,们我们指导mapreduce中是有默认的排列机制的,是按照key的升序从大到小排列的 然而top10问题的求 ...
- 支付宝小程序自定义弹窗插件|支付宝dialog插件|model插件
支付宝小程序自定义弹窗组件wcPop|小程序自定义对话框|actionSheet弹窗模板 支付宝小程序官方提供的alert提示框.dialog对话框.model弹窗功能比较有限,有些都不能随意自定义修 ...
- J06-Java IO流总结六 《 BufferedReader和BufferedWriter 》
1. 概念简介 与字节缓冲流BufferedInputStream和BufferedOutputStream对应的,我们还有字符缓冲流BufferedReader和BufferedWriter,顾名思 ...
- Python 1行代码实现文本分类(实战笔记),含代码详细说明及运行结果
Python 1行代码实现文本分类(实战笔记),含代码详细说明及运行结果 一.详细说明及代码 tc.py =============================================== ...
- SELECT列表中的标量子查询
发现了一种表连接新的写法,以前还没有这样写过或者见别人写过.跟同学聊天他们公司却很多人这样写,看来真的要学学sql了 表 CREATE TABLE `t_book` ( `FId` ) NOT NUL ...
- Spring Security构建Rest服务-0700-SpringSecurity开发基于表单的认证
自定义用户认证逻辑: 1,处理用户信息获取,2,用户校验,3密码的加密解密 新建:MyUserDetailService类,实现UserDetailsService接口. UserDetailsSer ...