1、部署两个tomcat服务器,如果在同一台虚拟机上,则需要修改其中一台tomcat的shutdown端口、http连接器端口、ajp连接器端口,然后部署应用,启动tomcat,浏览器查看,是否都可以访问
我的两台服务如下:
119.254.166.236 8080
119.254.166.236 8090
2、nginx下载、按照,参照:Linux(Centos)之安装Nginx及注意事项 或者 Linux 安装Nginx详细图解教程
启动、停止命令补充,进入/usr/local/nginx/sbin/目录:
停止服务: ./nginx -s stop
启动服务: ./nginx -c /usr/local/nginx/conf/nginx.conf
配置文件重新加载: nginx -s reload
校验nginx.conf文件的语法格式:nginx -t
3、修改Nginx 配置文件,配置集群负载均衡:
worker_processes 1;#工作进程的个数,一般与计算机的cpu核数一致
events {
worker_connections 1024;#单个进程最大连接数(最大连接数=连接数*进程数)
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
keepalive_timeout 65; #长连接超时时间,单位是秒
gzip on;#启用Gizp压缩
#服务器的集群
upstream testApp { #服务器集群名字,与下面server里面location的proxy_pass的域名值对应
ip_hash;#对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题
server 119.254.166.236:8080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
server 119.254.166.236:8090 weight=1;
}
#当前的Nginx的配置
server {
listen 80;#监听80端口,可以改成其他端口
server_name localhost;############## 当前服务的域名
location / {
#访问ROOT项目
proxy_pass http://testApp;
proxy_redirect default;
}
location /TestApp {
#访问指定路径项目
proxy_pass http://testApp/TestApp/;
proxy_redirect default;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
关于nginx负载均衡算法实现可查看http://www.cnblogs.com/knowledgesea/p/5199046.html
重新加载配置文件:
[root@localhost sbin]# ./nginx -s reload
浏览器访问,并查看tomcat后台日志情况,是否实现了负载均衡
参考:http://blog.csdn.net/wang379275614/article/details/47778201