步骤四:修改多实例tomcat的开启与关闭配置文件,添加相关的tomcat全局变量
Tomcat的多实例部署简单的讲就是基于端口的虚拟主机设置
vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64 export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar export PATH=$JAVA_HOME/bin:$PATH source /etc/profile.d/java.sh
tar -zxf apache-tomcat-9.0.16.tar.gz mkdir /usr/local/tomcat mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1 cp -a /usr/local/tomcat/tomcat1/ /usr/local/tomcat/tomcat2
vim /etc/profile.d/tomcat.sh #tomcat1的全局变量设置 export CATALINA_HOME1=/usr/local/tomcat/tomcat1 export CATALINA_BASE1=/usr/local/tomcat/tomcat1 export TOMCAT_HOME1=/usr/local/tomcat/tomcat1 #tomcat2的全局变量设置 export CATALINA_HOME2=/usr/local/tomcat/tomcat2 export CATALINA_BASE2=/usr/local/tomcat/tomcat2 export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
步骤四:修改多实例tomcat的开启与关闭配置文件,添加相关的tomcat全局变量
vim /usr/local/tomcat/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN"> #22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1" #69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
修改各Tomcat1和2实例中的startup.sh和shutdown.sh文件
修改配置文件
vim /usr/local/tomcat/tomcat1/bin/startup.sh # ----------------------------------------------------------------------------- # Start Script for the CATALINA Server # ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE1 export CATALINA_HOME=$CATALINA_HOME1 export TOMCAT_HOME=$TOMCAT_HOME1 vim /usr/local/tomcat/tomcat1/bin/shutdown.sh # ----------------------------------------------------------------------------- # Stop script for the CATALINA Server # ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE1 export CATALINA_HOME=$CATALINA_HOME1 export TOMCAT_HOME=$TOMCAT_HOME1 vim /usr/local/tomcat/tomcat2/bin/startup.sh # ----------------------------------------------------------------------------- # Start Script for the CATALINA Server # ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE2 export CATALINA_HOME=$CATALINA_HOME2 export TOMCAT_HOME=$TOMCAT_HOME2 vim /usr/local/tomcat/tomcat2/bin/shutdown.sh # ----------------------------------------------------------------------------- # Stop script for the CATALINA Server # ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE2 export CATALINA_HOME=$CATALINA_HOME2 export TOMCAT_HOME=$TOMCAT_HOME2
/usr/local/tomcat/tomcat1/bin/startup.sh /usr/local/tomcat/tomcat2/bin/startup.sh
2.1.1 Nginx实现负载均衡的原理
Nginx实现负载均衡是通过反向代理实现Nginx服务器作为前端,Tomcat服务器作为后端,web页面请求由Nginx服务来进行转发。 但是不是把所有的web请求转发,而是将静态页面请求Ncinx服务器自己来处理,动态页面请求,转发给后端的Tomcat服务器来处理。据了解,Tomcat是属于轻量级的应用服务器,可接受访问量可能会不足,所以我们需要多台Tomcat服务器。并且Tomcat并发量处理能力弱(约Nginx的六分之一),所以需要Nginx方向代理时,进行合理的调用分配。
2.1.2 Nginx实现负载均衡的主要配置项
upstream 服务池名 { }
作用:配置后端服务器池,以提供响应数据
proxy_pass http:// 服务池名
作用:配置将访问请求转发给后端服务器池的服务器处理
2.1.3 Nginx+Tomcat负载均衡的组合的优点
Nginx 静态处理优势:Nginx 处理静态页面的效率远高于 Tomcat 的处理能力若 Tomcat 的请求量为1000次,则 Nginx 的请求量为6000次Tomcat 每秒的吞吐量为0.6M,Nginx 的每秒吞吐量为3.6MNginx 处理静态资源的能力是 Tomcat 处理的6倍
动静分离原理:服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由 Nginx 提供服务,动态资源由 Nginx 转发至后端。
2.2 Nginx+Tomcat负载均衡的实验设计
实验需求:某公司中有一台nginx服务器,两台tomcat服务器。要求部署出用户访问服务,静态资源由Nginx自行处理,jsp动态资源移交给tomcat服务器处理,且达到负载均衡的效果
systemctl stop firewalld setenforce 0 yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make useradd -M -s /sbin/nologin nginx cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/ cd nginx-1.12.0/ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-file-aio \ #启用文件修改支持 --with-http_stub_status_module \ #启用状态统计 --with-http_gzip_static_module \ #启用 gzip静态压缩 --with-http_flv_module \ #启用 flv模块,提供对 flv 视频的伪流支持 --with-http_ssl_module #启用 SSL模块,提供SSL加密功能 --with-stream #启用 stream模块,提供4层调 make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecrReload=/bin/kill -s HUP $MAINPID ExecrStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target chmod 754 /lib/systemd/system/nginx.service systemctl start nginx.service systemctl enable nginx.service
tomcat1 和tomcat2 (上面多实例中以及布置完毕,可以直接采用)
搭建第三台Tomcat
systemctl stop firewalld setenforce 0 #软件包的方式安装jdk环境 tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_91 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH source /etc/profile tar zxvf apache-tomcat-8.5.16.tar.gz mv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh netstat -ntap | grep 8080
mkdir /usr/local/tomcat/webapps/test vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test3 page</title> #指定为 test3 页面 </head> <body> <% out.println("动态页面 3,http://www.test3.com");%> </body> </html> vim /usr/local/tomcat/conf/server.xml #由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true"> </Context> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh
tomcat1的设置:
mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> #指定为 test2 页面 </head> <body> <% out.println("动态页面 2,http://www.test2.com");%> </body> </html> vim /usr/local/tomcat/tomcat1/conf/server.xml #删除前面的 HOST 配置 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/tomcat1/bin/shutdown.sh /usr/local/tomcat/tomcat1/bin/startup.sh
tomcat2的设置:
vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("动态页面 1,http://www.test1.com");%> </body> </html> vim /usr/local/tomcat/tomcat2/conf/server.xml #删除前面的 HOST 配置 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/tomcat2/bin/shutdown.sh /usr/local/tomcat/tomcat2/bin/startup.sh
编写Nginx静态资源测试网页 :
vim /usr/local/nginx/html/index.html <html> <body> <h1> this is Nginx static test !</h2> <img src="test.png"/> </body> </html>
进行负载均衡和动静分离配置:
vim /usr/local/nginx/conf/nginx.conf ...... http { ...... #gzip on; #配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大 upstream tomcat_server { server 192.168.73.105:8080 weight=1; server 192.168.73.105:8081 weight=1; server 192.168.73.106:8080 weight=1; } server { listen 80; server_name www.yang.com; charset utf-8; #access_log logs/host.access.log main; #配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理 location ~ .*\.jsp$ { proxy_pass http://tomcat_server; #设置后端的Web服务器可以获取远程客户端的真实IP ##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来自反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。 proxy_set_header HOST $host; ##把$remote_addr赋值给X-Real-IP,来获取源IP proxy_set_header X-Real-IP $remote_addr; ##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #配置Nginx处理静态图片请求 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { root /usr/local/nginx/html; expires 10d; } location / { root html; index index.html index.htm; } ...... } ...... }
标签:负载,CATALINA,Tomcat,tomcat,Nginx,export,usr,local From: https://www.cnblogs.com/123456789SI/p/16893477.html