为什么要负载均衡?
Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过500的并发数会出现Tomcat不能响应新的请求的情况,严重影响网站的运行。同时如果访问量非常大的情况下,Tomcat的线程数会不断增加。因此会占据大量内存,严重时出现内存溢出的现象,这时需要重启Tomcat以释放内存,阻断了网站的运行。
所以对Tomcat做负载均衡便很有必要。目前可以和Tomcat做负载均衡的主流服务器是Apache,但是Nginx由于功能多、配置简单等优点成为很多负载均衡服务器的首选。Nginx的并发数可达到50000,所以理论上可以和Tomcat以1:100的比例来配置,这边可以很好的解决网站并发瓶颈问题。
实验环境
Nginx1.10.2 +Tomcat 8.0.35(3个)、Win 10
一、安装Nginx
如果启动成功后,可以通过浏览器:
输入http://localhost 或者http://localhost:80 来验证是否成功
二、安装Tomcat
分别复制三个Tomcat修改三个启动端口,使三个Tomcat能在一台计算机上启动。
2.1 第一步:修改 端口号
默认值8005,加上2 ,变成 8007
默认值是 8080 ;加上2;变成 8082
默认值是 8009;加上2;变成8011
2.2第二步:修改tomcat-users.xml
添加网页上面,管理app的能力
添加如下内容(Password部分自己指定,或者就使用默认的即可,方便大家交流记忆。):
2.3第三步:添加jar包
启动tomcat 8082实例
2.4第四步:修改context.xml
添加如下内容:
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.81.137"
port="6379"
database="0"
password="admin"
maxInactiveInterval="60" />
如果redis已经设置了,要求输入授权密码的,需要配置password选项,否则会报下面的错误2,NOAUTH错误
2.5 第五步:验证是否成功
重复上述步骤:
启动tomcat 8083,8084
三、tomcat中添加测试项目来验证tomcat session的共享
在tomcat 8082中添加web项目:
在webapps目录下,就简单建一个文件夹sharesesson,并且在下面新建两个页面write.jsp
read.jsp ;内容如下:
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<h1>tomcat8082</h1>
<%String s = session.getId(); //获取session ID号 %>
<%=s %>
<%
session.setAttribute("cyp", "123456");
%>
</body>
</html>
read.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<h1>tomcat8082</h1>
<%String s = session.getId(); //获取session ID号 %>
<%=s %>
<br/>
<br/>
<%=(String)session.getAttribute("cyp")%>
</body>
</html>
重复上面步骤:分别在tomcat8083,tomcat8084下面分别添加sharesessionweb项目;内容跟上面的write.jsp 和read.jsp 内容一样。
只是注意<h1>tomcat8082</h1>这句话,修改为相应的tomcat对应的内容:
Tomcat8083–> <h1>tomcat8083</h1>
Tomcat8084–> <h1>tomcat8084</h1>
3.1第一步:
访问http://localhost:8082/sharesession/write.jsp
让session生效
访问http://localhost:8082/sharesession/read.jsp
读取session内容:
3.3 第三步:
访问tomcat8083的read.jsp
http://localhost:8083/sharesession/read.jsp
session的内容正常读取出来,已经达到的在tomcat8082上面写session,通过redis的共享内容,在tomcat8083 上面读session;
3.4 第四步:
访问tomcat804的read.jsp
http://localhost:8084/sharesession/read.jsp
session的内容正常读取出来,已经达到的在tomcat8082上面写session,通过redis的共享内容,在tomcat8084 上面读session;
总结实现方式:
Tomcat8082 –> redis (写)
Tomcat8083 <- redis(读)
Tomcat8084 <- redis(读)
以上所有步骤只能解决了tomcat的session同享的问题。
四、让nginx 反向代理 tomcat的服务修改nginx的位置文件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;
upstream localhost {
server localhost:8082 weight=1;
server localhost:8083 weight=2;
server localhost:8084 weight=3;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#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;
# }
#}
}
五、验证负载均衡是否成功:
双击nginx根目录下nginx.exe文件
或者使用start nginx启动(关闭是: nginx -s stop)
打开浏览器,
输入地址:
http://localhost/sharesession/write.jsp
http://localhos/sharesession/read.jsp

不停的刷新,会访问到不同的tomcat

六、Nignx 常用命令
常用nginx 命令:
停止:nginx -s stop
nginx.exe -s quit:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。
修改配置使配置生效:nginx -s reload
检查配 nginx -t