学完了Nginx和Tomcat,我们最期待的重点部分终于来了----------Nginx+Tomcat负载均衡。
准备条件:
1台负载均衡服务器
NFS 10.0.0.31 172.16.1.31
3台web服务器,组建出web服务器集群
web01 10.0.0.32 172.16.1.32
web02 10.0.0.33 172.16.1.33
web03 10.0.0.34 172.16.1.34
- 编辑NFS 的 nginx.conf 配置文件。
# upstream 模块就类似定一个一个地址池或者说定一个web服务器组worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; 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; upstream firstSwitch{ //upstream模块 server 172.16.1.32:8080; server 172.16.1.33:8080; server 172.16.1.34:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://firstSwitch; //proxy_pass } } }
& weight 实现权重值负载访问功能: weight=3
& max_fails 定义后端访问的失败次数:max_fails=3
& fail_timeout 定义后端失败重试的间隔:fail_timeout=10s
& 定义后端服务的热备节点-backup(负载节点服务器都挂了,使用备份)
例如 :#upstream 模块常用功能 upstream firstSwitch{ server 172.16.1.32:8080 weight=3 max_fails=3 fail_timeout=10s; server 172.16.1.33:8080 weight=1 max_fails=3; server 172.16.1.34:8080 weight=1 max_fails=3; server 172.16.1.35:8080 backup; } #木块调度算法 1. 定义轮询调度算法-rr-默认调度算法 2. 定义权重调度算法-wrr 3. 定义静态调度算法-ip_hash (将请求固定到同一个服务器上,不能和backup与weight参数通同时使用) 4. 定义最小的连接数-least_conn (将请求交给连接数较小的服务器处理)
# proxy_pass 主要用于进行抛送用户访问请求给upstream模块中的相应节点服务器
这里我们通过proxy_pass 和upstream模块相互配合,将客户端request转发到对应的服务器web01,web02,web03上。 - 检测配置文件并启动Nginx。
[root@NFS ~]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful [root@NFS ~]# /application/nginx/sbin/nginx [root@NFS ~]# ps -ef|grep nginx root 1642 1 0 20:57 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 1643 1642 0 20:57 ? 00:00:00 nginx: worker process root 1645 1513 0 20:57 pts/0 00:00:00 grep --color=auto nginx [root@NFS ~]#
- 分别编辑web01,web02,web03的Tomcat,并启动服务
web01(10.0.0.32 172.16.1.32)[root@web01 ~]# cd /application/tomcat/webapps/ [root@web01 webapps]# mkdir www [root@web01 webapps]# echo "172.16.1.32 web01">www/index.html [root@web01 webapps]# [root@web01 webapps]# [root@web01 webapps]# ../bin/startup.sh Using CATALINA_BASE: /application/tomcat Using CATALINA_HOME: /application/tomcat Using CATALINA_TMPDIR: /application/tomcat/temp Using JRE_HOME: /application/jdk Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar Tomcat started. [root@web01 webapps]#
web02(10.0.0.33 172.16.1.33)[root@web02 ~]# cd /application/tomcat/webapps/ [root@web02 webapps]# mkdir www [root@web02 webapps]# echo "172.16.1.33 web01">www/index.html [root@web02 webapps]# [root@web02 webapps]# [root@web02 webapps]# ../bin/startup.sh Using CATALINA_BASE: /application/tomcat Using CATALINA_HOME: /application/tomcat Using CATALINA_TMPDIR: /application/tomcat/temp Using JRE_HOME: /application/jdk Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar Tomcat started. [root@web01 webapps]#
web03(10.0.0.34 172.16.1.34)[root@web03 ~]# cd /application/tomcat/webapps/ [root@web03 webapps]# mkdir www [root@web03 webapps]# echo "172.16.1.34 web01">www/index.html [root@web03 webapps]# [root@web03 webapps]# [root@web03 webapps]# ../bin/startup.sh Using CATALINA_BASE: /application/tomcat Using CATALINA_HOME: /application/tomcat Using CATALINA_TMPDIR: /application/tomcat/temp Using JRE_HOME: /application/jdk Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar Tomcat started. [root@web01 webapps]#
- 用curl命令测试或浏览器测试
[root@web01 webapps]# curl -L http://10.0.0.31/www/index.html 172.16.1.32 web01 [root@web01 webapps]# curl -L http://10.0.0.31/www/index.html 172.16.33 web02 [root@web01 webapps]# curl -L http://10.0.0.31/www/index.html 172.16.1.34 web03 [root@web01 webapps]#
通过curl命令访问测试,可以看见效果,我们每次访问的结果可能都不一样。由于Nginx将request转发到不同的Tomcat服务, 而配置文件中的upstream模块没有做多余的配置,因此每次的转发都是平均分配。
附:如果采用浏览器访问测试,先需要进行hosts解析 http://www.daydayup.com/www/index.html ,务必利用ctrl+F5刷新测试,检查是否进行负载调度
最后修改于 2019-12-13 15:06:22
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

