tomcat 集群实现

 

 

1、tomcat服务器安装java环境(主:192.168.10.28 从:192.168.10.26)
# tar zxvf jdk-8u192-linux-x64.tar.gz
# mkdir /usr/local/java
# mv jdk1.8.0_192 /usr/local/java
 
1.1、tomcat配置环境变量:

# vim /etc/profile
export JAVA_HOME=/usr/local/java
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/rt.jar
export JAVA_HOME JRE_HOME CLASSPATH PATH
# source /etc/profile
 
2、部署tomcat
# tar xvf apache-tomcat-9.0.11.tar.gz
# mv apache-tomcat-9.0.11 /usr/local/tomcat
注:
bin:              存放启动和关闭Tomcat的脚本文件;      
conf:           存放Tomcat服务器的各种全局配置⽂文件,其中最重要的是server.xml和web.xml
lib:              存放Tomcat服务器所需的各种JAR文件;       
logs:           存放Tomcat执行时的日志文件;    
temp:         此目录是tomcat存放的临时文件;
webapps:   tomcat默认存放应用程序的目录,好比apache的默认网页存放路径是/var/www/html一样;
work:         用于存放JSP应用程序在部署时编译后产生的class文件
 
2.1 启动tomcat
# ./startup.sh
# ps aux | grep java
# ps aux |grep tomcat
# netstat -lntp |grep 8080
 
3、配置启动服务
# vim /usr/local/tomcat/bin/catalina.sh   # 添加一条pid信息
 
137 CATALINA_PID=/var/run/tomcat.pid                 

 

3.1 centos6启动文件
# vim /etc/init.d/tomcat
#!/bin/bash
#chkconfig: 35 86 89
TOMCATBIN=/usr/local/tomcat/bin/catalina.sh
export JAVA_HOME=/usr/local/java
start(){
  $TOMCATBIN start
}
stop(){
  $TOMCATBIN stop
}
restart(){
 stop
 start
}
status(){
  if [ -f '/var/run/tomcat.pid' ];then
     echo 'tomcat is running'
  else
     echo 'tomcat is not run'
  fi
}
configtest(){
  $TOMCATBIN configtest
}
case "$1" in
   start)
      start;;
   stop)
      stop;;
   restart)
      restart;;
   status)
      status;;
   configtest)
      configtest;;
   *)
      echo '{start|stop|restart|configtest|status}'
esac 
# service tomcat start
 
3.2、Centos7系统tomcat启动文件
# vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/var/run/tomcat.pid
Environment=JAVA_HOME=/usr/local/java
Environment=CATALINA_HOME=/usr/local/tomcat
 
ExecStart=/usr/local/tomcat/bin/daemon.sh start
ExecStop=/usr/local/tomcat/bin/daemon.sh stop
User=tomcat
Group=tomcat
PrivateTmp=true
[Install]
WantedBy=multi-user.target 
 
# systemctl daemon-reload
 
4、tomcat服务器优化设置
# vim /usr/local/tomcat/webapps/host-manager/META-INF/context.xml             # 注释掉所有ip限制
 19   <!--
 20     <Valve className="org.apache.catalina.valves.RemoteAddrValve"
 21          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
 22   -->

 

# vim /usr/local/tomcat/conf/tomcat-users.xml          # 添加用户名和密码
 46  <role rolename="manager-gui"/>
 47  <role rolename="admin-gui"/>
 48  <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

 

 
5、nginx和tomcat实现反向代理(在LB服务器上部署192.168.10.65 192.168.10.66)
# vim nginx.conf
upstream tomcat {
        server 192.168.10.28:8080;
        server 192.168.10.26:8080;
        }
   server {
        listen       80;
        server_name  blog.devops.com;
        root /usr/local/tomcat/webapps/devops/ROOT;
        #location ~* \.(jpg|jpeg|gif|js|html|css)$ {
        #}
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat;
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
   }

 

6、建立项目
6.1、将war包传送和设置
# mkdir -p /usr/local/tomcat/webapps/devops
# cd /usr/local/tomcat/webapps/devops
# wget https://dl.zrlog.com/release/zrlog.war
# mv zrlog.war ROOT.war
# cd /usr/local/tomcat/conf/
# vi server.xml
    164       </Host>
    165       <Host name="blog.devops.com"  appBase="webapps/devops">
    166       </Host>

 

# service tomcat configtest

# service tomcat restart
# chown -R www:www /usr/local/tomcat/webapps
 
6.2、数据库创建:
# mysql -uroot -p123456 
mysql> GRANT ALL ON zrblog.* to 'zrblog'@'192.168.10.%'IDENTIFIED BY '123456';
mysql> flush privileges;
mysql> create database zrblog;
 
6.3、项目同步
web02:192.168.10.26
# ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.10.28
# cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
#设置允许登录的ip,这里没有限制IP
hosts allow=*
#设置最大连接数
max connections = 3
#设置启动rsync服务器的文件
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
#同步模块
[devops]
#需要的文件及其文件夹路径
path = /usr/local/tomcat/webapps/devops
#备注
comment = devops
#只读关闭
read only = false
 
web01:192.168.10.28
# ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.10.26
 
# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
# mv GNU-Linux-x86 /usr/local/sersync
# cat /etc/init.d/sersync
#!/bin/bash
# chkconfig: 2345 10 90
# description: sersync2
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

 

# chmod +x /etc/init.d/sersync

# chkconfig --add sersync
# chkconfig  sersync on
# service sersync start
# ps aux |grep sersync
 
6.4 LB服务器设置
(主服务器:192.168.10.65 从服务器:192.168.10.66 vip:192.168.10.41)
# cat  /usr/local/nginx/conf/nginx.conf
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    sendfile on;
    keepalive_timeout  65;
    upstream shop {
       #ip_hash;
       server 192.168.10.61 weight=5;
       server 192.168.10.62 weight=3;
       }
  server {
    listen       80;
    server_name  www.shop.com;
    location / {
       proxy_pass http://shop;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       }
    }
    upstream tomcat {
        # ip_hash;
        server 192.168.10.28:8080 weight=5;
        server 192.168.10.26:8080 weight=3;
        }
   server {
        listen       80;
        server_name  blog.devops.com;
        root /usr/local/tomcat/webapps/devops/ROOT;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat;
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

# systemctl restart nginx

 
posted @ 2021-11-18 07:43  雨夜清风  Views(839)  Comments(0Edit  收藏  举报