厌伴老儒烹瓠叶,强随举子踏槐花。这篇文章主要讲述nginx+Tomcat 负载均衡相关的知识,希望能为你提供帮助。
实验环境
实验准备实现三台设备的局域网互通三台虚拟机之间使用 VMnet3 对接,只需要配置IP地址,不需要配置网关
nginx
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens33
ONBOOT=yse
IPADDR=172.16.1.1
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown ens33
^[[ADevice ens33 successfully disconnected.
[root@bobi ~]# ifdown ens33
[root@bobi ~]# ifup ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@bobi ~]#
[root@bobi ~]# ipaddr
bash: ipaddr: command not found...
[root@bobi ~]# ip addr
1: lo: <
LOOPBACK,UP,LOWER_UP>
mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f4:e9:60 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.1/24 brd 172.16.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fef4:e960/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <
BROADCAST,MULTICAST>
mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:bd:8a:7a brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <
BROADCAST,MULTICAST>
mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:bd:8a:7a brd ff:ff:ff:ff:ff:ff
5: ens37: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f4:e9:6a brd ff:ff:ff:ff:ff:ff
inet 192.168.192.144/24 brd 192.168.192.255 scope global noprefixroute dynamic ens37
valid_lft 1268sec preferred_lft 1268sec
inet6 fe80::b056:e73b:2369:6dce/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
tomcat1
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.10
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown eth0
Device eth0 successfully disconnected.
[root@bobi ~]# ifup eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@bobi ~]#
[root@bobi ~]# ip addr
1: lo: <
LOOPBACK,UP,LOWER_UP>
mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:57:b3:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.1.10/24 brd 172.16.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe57:b32f/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:57:b3:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.192.146/24 brd 192.168.192.255 scope global noprefixroute dynamic eth1
valid_lft 1638sec preferred_lft 1638sec
inet6 fe80::f43e:7f0c:bfff:d907/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
tomcat2
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.20
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown eth0
Device eth0 successfully disconnected.
[root@bobi ~]# ifup eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@bobi ~]#
[root@bobi ~]# ip addr
1: lo: <
LOOPBACK,UP,LOWER_UP>
mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:69:75 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.20/24 brd 172.16.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe02:6975/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <
BROADCAST,MULTICAST,UP,LOWER_UP>
mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:69:7f brd ff:ff:ff:ff:ff:ff
inet 192.168.192.145/24 brd 192.168.192.255 scope global noprefixroute dynamic eth1
valid_lft 1155sec preferred_lft 1155sec
inet6 fe80::f59b:e22f:f08a:8728/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
测试
nginx 和 tomcat
tomcat 之间
网络互通
开启 Nginx 服务
tomcat1
[root@bobi webapps]# ls
docsexampleshost-managermanagerROOT
[root@bobi webapps]# mv ROOT/ ROOTBAK
[root@bobi webapps]# ls
docsexampleshost-managermanagerROOTBAK
[root@bobi webapps]#
[root@bobi webapps]# mkdir ROOT
[root@bobi webapps]# cd ROOT
[root@bobi ROOT]# ls
[root@bobi ROOT]# vim index.jsp
[root@bobi ROOT]#
[root@bobi ROOT]# cat index.jsp
<
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<
html>
<
head>
<
title>
JSP TEST1 page<
/title>
<
/head>
<
body>
<
% out.println("Welcome to TEST1 web,http:/www.test1.com");
%>
<
/body>
<
/html>
tomcat2
[root@bobi webapps]# ls
docsexampleshost-managermanagerROOT
[root@bobi webapps]# mv ROOT/ ROOTBAK
[root@bobi webapps]# ls
docsexampleshost-managermanagerROOTBAK
[root@bobi webapps]#
[root@bobi webapps]# mkdir ROOT
[root@bobi webapps]# cd ROOT
[root@bobi ROOT]# ls
[root@bobi ROOT]# vim index.jsp
[root@bobi ROOT]#
[root@bobi ROOT]# cat index.jsp
<
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<
html>
<
head>
<
title>
JSP TEST2 page<
/title>
<
/head>
<
body>
<
% out.println("Welcome to TEST2 web,http:/www.test2.com");
%>
<
/body>
<
/html>
配置 Nginx 服务端安装 Nginx 反向代理服务./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
[root@bobi src]# cd nginx-1.6.2/
[root@bobi nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
checking for OpenSSL library ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<
path>
option.
缺少开发组件,安装即可
[root@bobi nginx-1.6.2]# yum install openssl openssl-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
dvdrom| 3.6 kB00:00:00
(1/2): dvdrom/group_gz| 153 kB00:00:00
(2/2): dvdrom/primary_db| 3.3 MB00:00:00
Package 1:openssl-1.0.2k-19.el7.x86_64 already installed and latest version
Resolving Dependencies
...
----------------------------------------------------------------------------------
Total9.1 MB/s | 2.1 MB00:00
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7krb5-devel.x86_64 0:1.15.1-50.el7
libcom_err-devel.x86_64 0:1.42.9-19.el7libselinux-devel.x86_64 0:2.5-15.el7
libsepol-devel.x86_64 0:2.5-10.el7libverto-devel.x86_64 0:0.2.5-4.el7
Complete!
注:
刚开始 yum install openssl openssl-devel 的时候出现下面的错误
Error downloading packages:
libsepol-devel-2.5-10.el7.x86_64: [Errno 256] No more mirrors to try.
libverto-devel-0.2.5-4.el7.x86_64: [Errno 256] No more mirrors to try.
libselinux-devel-2.5-15.el7.x86_64: [Errno 256] No more mirrors to try.
keyutils-libs-devel-1.5.8-3.el7.x86_64: [Errno 256] No more mirrors to try.
krb5-devel-1.15.1-50.el7.x86_64: [Errno 256] No more mirrors to try.
1:openssl-devel-1.0.2k-19.el7.x86_64: [Errno 256] No more mirrors to try.
libcom_err-devel-1.42.9-19.el7.x86_64: [Errno 256] No more mirrors to try.
百度一下,需要进行下面的操作
yum clean all
yum makecache
yum update
但是又出现错误
failure: repodata/repomd.xml from dvdrom: [Errno 256] No more mirrors to try.
file:/media/repodata/repomd.xml: [Errno 14] curl#37 - "Couldnt open file /media/repodata/repomd.xml"
最后发现是本地的镜像没有挂载
(虽然在前面的实验中挂载了,但是是临时挂载,重启后就失效了,开机的时候反而忘记了这回事,重新挂载一下就好了)
[root@bobi yum.repos.d]# cd /media/
[root@bobi media]# ls
[root@bobi media]#
[root@bobi media]# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only
开发组件安装完成后,继续检查编译环境
[root@bobi nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
make -j 4
make install
验证[root@bobi local]# cd /usr/local/nginx/
[root@bobi nginx]# ls
client_body_tempfastcgi_templogssbinuwsgi_temp
confhtmlproxy_tempscgi_temp
[root@bobi nginx]# cd sbin/
[root@bobi sbin]# ls
nginxnginx.old
注:
nginx.old 是之前安装的nginx服务
nginx 是现在安装的nginx反向代理服务
配置 Nginx 反向代理服务修改配置文件
vim /usr/local/nginx/conf/nginx.conf
upstream tomcatserver
server 172.16.1.10:8080 weight=1;
server 172.16.1.20:8080 weight=1;
【nginx+Tomcat 负载均衡】
推荐阅读
- 基于java eclipse+jsp+mysql+servlet+Spring的学生信息管理系统基础版
- 机器学习算法之KMeans聚类
- Linux 下通过 pip3 安装 Ansible
- #yyds干货盘点# 解决剑指offer(重建二叉树)
- 目标检测算法之YOLOv3及YOLOV3-Tiny
- mysql处理数据库事务
- R_01 centos8.0 redis4.0 伪集群搭建
- OpenHarmony轻量系统移植示例
- 今天爬,明天没,天津市XX网 详情页加密逻辑拆解,文中关键字已经加密