tomcat群集
实验对象:三台centos7服务器
其中nginx负载
nginx 192.168.247.206
tomcat1 192.168.247.160
tomcat2 192.168.247.161
[root@lamp ~]# hostnamectl set-hostname nginx
[root@lamp ~]# su
[root@nginx ~]#
[root@nginx ~]# hostnamectl set-hostname tomcat1
[root@nginx ~]# su
[root@tomcat1 ~]#
[root@localhost ~]# hostnamectl set-hostname tomcat2
[root@localhost ~]# su
[root@tomcat2 ~]#
实验步骤:
1.先处理tomcat
[root@tomcat1 ~]# systemctl stop firewalld
[root@tomcat1 ~]# setenforce 0
[root@tomcat1 ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@tomcat1 ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@tomcat1 ~]# cd /abc
[root@tomcat1 abc]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat1 abc]# 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:$PATH
[root@tomcat1 abc]# source /etc/profile
[root@tomcat1 abc]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
[root@tomcat1 abc]# cd /usr/local
[root@tomcat1 local]# ls
apache-tomcat-8.5.16 etc include lib libexec sbin src
bin games jdk1.8.0_91 lib64 nginx share
[root@tomcat1 local]# mv apache-tomcat-8.5.16/ tomcat
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
tomcat安装完毕,接下来创建站点
[root@tomcat1 local]# mkdir -pv /web/webapp1
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/webapp1’
[root@tomcat1 local]# cd /web/webapp1/
[root@tomcat1 webapp1]# ls
[root@tomcat1 webapp1]# vim index.jsp
<%@ page language="java" import="java.uitl.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Welcome KGC Web");%>
</body>
</html>
备注:tomcat2里面KGC改为ACCP
添加指定站点
[root@tomcat1 webapp1]# vim /usr/local/tomcat/conf/server.xml
148 <Host name="localhost" appBase="webapps"
149 unpackWARs="true" autoDeploy="true">
150 //加入下面两行
151 <Context docBase="/web/webapp1" path="" reloadable="false">
152 </Context>
[root@tomcat1 webapp1]# startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
此时再同样配置tomcat2
验证
接下来轮到nginx服务器
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum install gcc-c++ gcc pcre-devel zlib-devel make -y
[root@nginx ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@nginx ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@nginx ~]# cd /abc
[root@nginx abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx abc]# useradd -M -s /sbin/nologin nginx
[root@nginx abc]# cd /opt/nginx-1.12.0/
[root@nginx nginx-1.12.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
http-flv-module 视频模块
[root@nginx nginx-1.12.0]# make && make install
[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
#下面tomcat的服务器池定义在server{}外面
upstream tomcat_server {
server 192.168.247.160:8080 weight=1;
server 192.168.247.161:8080 weight=1;
}
server {
。。。
location / {
root html;
index index.html index.htm;
#下面一行是添加内容
proxy_pass http://tomcat_server;
}
[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx nginx-1.12.0]# nginx
[root@nginx nginx-1.12.0]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 66405/nginx: master
访问验证
刷新一下
使用nginx实现反向代理去负载均衡轮询访问tomcat服务器池