利用systemd 實現 tomcat 多實例

環境: CentOS 7 x86_64css


一、安裝jdkhtml

rpm -ivh jdk-8u60-linux-x64.rpm


二、建立普通用戶linux

getent group tomcat || groupadd -r tomcat
getent passwd tomcat || useradd -r -d /opt -s /bin/nologin tomcat


三、下載tomcat 7nginx

cd /opt
wget http://mirrors.ustc.edu.cn/apache/tomcat/tomcat-7/v7.0.64/bin/apache-tomcat-7.0.64.tar.gz
tar zxvf apache-tomcat-7.0.64.tar.gz
mv apache-tomcat-7.0.64 tomcat01
chown -R tomcat:tomcat tomcat01

tar zxvf apache-tomcat-7.0.64.tar.gz
mv apache-tomcat-7.0.64 tomcat02
chown -R tomcat:tomcat tomcat02


兩個實例同時監聽8080端口必然會產生衝突,因此必須修改.apache

sed -i 's/8080/8081/g' /opt/tomcat01/conf/server.xml
sed -i 's/8005/8001/g' /opt/tomcat01/conf/server.xml
sed -i 's/8080/8082/g' /opt/tomcat02/conf/server.xml
sed -i 's/8005/8002/g' /opt/tomcat02/conf/server.xml

AJP一般用不上,這裏統一關閉tomcat

sed -i '/8009/d' /opt/tomcat01/conf/server.xml
sed -i '/8009/d' /opt/tomcat01/conf/server.xml

tomcat在Linux下默認工做模式是bio,性能很是低,建議使用apr或者nio,bash

這裏改成nio,即非阻塞IO,性能比較好。jsp

sed -i.bak 's#HTTP/1.1#org.apache.coyote.http11.Http11NioProtocol#' /opt/tomcat01/conf/server.xml
sed -i.bak 's#HTTP/1.1#org.apache.coyote.http11.Http11NioProtocol#' /opt/tomcat02/conf/server.xml

(若是想起用apr模式,須要安裝apr-devel、tomcat-native兩個軟件包,無需改配置)ide


三、建立啓動文件性能

cd /usr/lib/systemd/system
cat >tomcat01.service <<EOF
[Unit]
Description=Apache Tomcat 7
After=network.target
[Service]
Type=oneshot
ExecStart=/opt/tomcat01/bin/startup.sh
ExecStop=/opt/tomcat01/bin/shutdown.sh
RemainAfterExit=yes
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
EOF

tomcat02同理

sed 's/tomcat01/tomcat02/g' tomcat01.service > tomcat02.service


四、啓動服務

systemctl enable tomcat01
systemctl enable tomcat02
systemctl start tomcat01
systemctl start tomcat02


五、日誌輪轉

因爲tomcat是做爲後臺服務持續運行,天長日久,日誌一定會愈來愈大,在當初部署tomcat之時很容易忽略這個問題,不然硬盤早晚會被撐爆。

建立文件/etc/logrotate.d/tomcat,內容以下:

/opt/tomcat01/logs/catalina.out
/opt/tomcat02/logs/catalina.out
{
    copytruncate
    daily
    rotate 7
    missingok
    notifempty
    compress
    create 0644 tomcat tomcat
}


六、查看進程

ps aux |grep tomcat


附上一份nginx反向代理tomcat的配置文件

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=static:10m inactive=30d max_size=1g;

upstream tomcat {
        ip_hash       ;
        #hash           $remote_addr consistent;
        server          127.0.0.1:8081 max_fails=1 fail_timeout=2s ;
        server          127.0.0.1:8082 max_fails=1 fail_timeout=2s ; 
        keepalive       16;
}

server {
        listen          80;
        server_name     tomcat.example.com;

        charset         utf-8;
        access_log      /var/log/nginx/tomcat.access.log  main;
        root            /usr/share/nginx/html;
        index           index.html index.htm index.jsp;

        location / {
                proxy_pass              http://tomcat;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
        }

        location ~* ^.+\.(js|css|ico|gif|jpg|jpeg|png)$ {
                proxy_pass              http://tomcat ;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                proxy_cache             static;
                proxy_cache_key         $host$uri$is_args$args;
                proxy_cache_valid       200 302 7d;
                proxy_cache_valid       404 1m;
                proxy_cache_valid       any 1h;
                add_header              X-Cache $upstream_cache_status;

                #log_not_found off;
                #access_log off;
                expires max;
        }

        location ~ /\.ht {
                deny  all;
        }

}


本文理論上也適用於tomcat8,未經測試。

相關文章
相關標籤/搜索