天天看点

Tomcat

java企业应用之Tomcat

第1章 Tomcat简介

tomcat属于apache软件基金会中的一个项目

tomcat服务器是一个开源的web应用服务器,属于轻量级应用服务器,在中小型系统和并发系统用户不是很多的场合下被普遍使用,是开发和调试jsp的首选

Tomcat

jvm即java虚拟机的缩写

java的特性:一次编译,到处运行

类比vmware的linux系统

jdk即java语言的软件开发工具,包含jvm

常见的jdk软件:  oracle jdk   属于商业软件

open  jdk   开源软件

第2章 部署tomcat

[root@web03 ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[root@web03 ~]# uname -r

3.10.0-327.el7.x86_64

[root@web03 ~]# getenforce

Disabled

[root@web03 ~]# systemctl status firewalld.service

● firewalld.service - firewalld - dynamic firewall daemon

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

   Active: inactive (dead)

[root@web03 ~]# hostname -I

10.0.0.17 172.16.1.17

tar xf apache-tomcat-8.0.27.tar.gz -C /application/

ln -s /application/apache-tomcat-8.0.27 /application/tomcat

echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile

source /etc/profile

chown -R root.root /application/jdk/ /application/tomcat/

/application/tomcat/bin/version.sh

tar xf jdk-8u60-linux-x64.tar.gz -C /application/

ln -s /application/jdk1.8.0_60 /application/jdk

sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile

java -version

yum -y install mariadb-server

systemctl start mariadb.service

mysql

create database jpress DEFAULT CHARACTER SET utf8;

grant all on jpress.* to jpress@'localhost' identified by '123456';

[root@web03 webapps]# /application/tomcat/bin/startup.sh

Using CATALINA_BASE:   /application/tomcat

Using CATALINA_HOME:   /application/tomcat

Using CATALINA_TMPDIR: /application/tomcat/temp

Using JRE_HOME:        /application/jdk

Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar

Tomcat started.

[root@web03 webapps]# ss -tunlp| grep java

tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=15816,fd=51))

tcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=15816,fd=46))

tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=15816,fd=67))

第3章 CentOS7版本tomcat启动慢的原因:

[root@web03 logs]# cat /proc/sys/kernel/random/entropy_avail

178

yum -y install rng-tools # 安装rngd服务(熵服务,增大熵池)

systemctl start rngd  # 启动服务

第4章 tomcat基于web页面管理:

[root@web03 conf]# vim tomcat-users.xml

在倒数第二行下面添加

<role rolename="manager-gui"/>

<role rolename="admin-gui"/>

<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

第5章 tomcat主配置文件:

[root@web03 tomcat]# tree -L 1 .

.

├── bin        命令目录start.sh启动文件,shutdown.sh关闭文件,catalina.sh所有执行脚本的核心

├── conf  server.xml   主配置文件

├── lib

├── LICENSE

├── logs       catalina.out实时记录tomcat运行信息,文件会越来越大,定时清空

                catalina.时间.logcatalina.out问及那每天的日志切割文件

                剩下的每个站点访问的日志

├── NOTICE

├── RELEASE-NOTES

├── RUNNING.txt

├── temp               临时文件

├── webapps            tomcat站点目录

└── work

[root@web03 tomcat]# grep Server conf/server.xml

<Server port="8005" shutdown="caonige">  在8005端口输入shutdown可关闭java服务

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

tomcat和apache用来建立连接的端口

    <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

第6章 搭建jpress:

jpress就是类似wordpress的博客站点

create database jpress DEFAULT CHARACTER SET utf8;

grant all on jpress.* to jpress@'localhost' identified by '123456';

第7章 tomcat多实例:

cd /application/tools/

tar xf apache-tomcat-8.0.27.tar.gz

cp -a apache-tomcat-8.0.27 tomcat8_1

cp -a apache-tomcat-8.0.27 tomcat8_2

sed -i 's#8005#8011#;s#8080#8081#' tomcat8_1/conf/server.xml

sed -i 's#8005#8012#;s#8080#8082#' tomcat8_2/conf/server.xml

diff tomcat8_1/conf/server.xml tomcat8_2/conf/server.xml

# 生产经验:将已经配置的实例打包,以后方便批量配置多实例

tar zcf tomcat_muti.tar.gz ./tomcat8_1/ ./tomcat8_2/

cp -a tomcat8_1 tomcat8_2 /application/

/application/tomcat8_1/bin/startup.sh

/application/tomcat8_2/bin/startup.sh

echo 8081 >>/application/tomcat8_1/webapps/ROOT/index.jsp 

echo 8082 >>/application/tomcat8_2/webapps/ROOT/index.jsp 

[root@lb01 conf]# cat nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 upstream web_pools {

        server 10.0.0.17:8081;

        server 10.0.0.17:8082;

    }

    server {

        listen       80;

        server_name  localhost;

    location / {

             root   html;

             index  index.jsp index.html index.htm;

         proxy_pass http://web_pools;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

第8章 tomcat监控:

[root@web03 memtest]# cat meminfo.jsp

<%

Runtime rtm = Runtime.getRuntime();

long mm = rtm.maxMemory()/1024/1024;

long tm = rtm.totalMemory()/1024/1024;

long fm = rtm.freeMemory()/1024/1024;

out.println("JVM memory detail info :<br>");

out.println("Max memory:"+mm+"MB"+"<br>");

out.println("Total memory:"+tm+"MB"+"<br>");

out.println("Free memory:"+fm+"MB"+"<br>");

out.println("Available memory can be used is :"+(mm+fm-tm)+"MB"+"<br>");

%>

jps主要用来输出jvm中运行的进程状态信息

jps –lvm可以快速获取java进程的pid,不用ps和grep

[root@web03 memtest]# jps -lvm

16965 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp

17116 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat8_1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat8_1/endorsed -Dcatalina.base=/application/tomcat8_1 -Dcatalina.home=/application/tomcat8_1 -Djava.io.tmpdir=/application/tomcat8_1/temp

17326 sun.tools.jps.Jps -lvm -Denv.class.path=..:/application/jdk/lib:/application/jdk/jre/lib:/application/jdk/lib/tools.jar:/application/jdk/lib:/application/jdk/jre/lib:/application/jdk/lib/tools.jar -Dapplication.home=/application/jdk1.8.0_60 -Xms8m

17135 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat8_2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat8_2/endorsed -Dcatalina.base=/application/tomcat8_2 -Dcatalina.home=/application/tomcat8_2 -Djava.io.tmpdir=/application/tomcat8_2/temp

16623 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp

[root@web03 tools]# sh show-busy-java-threads.sh

[1] Busy(0.5%) thread(16971/0x424b) stack of java process(16965) under user(root):

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f2fe40ac000 nid=0x424b waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

[2] Busy(0.2%) thread(17141/0x42f5) stack of java process(17135) under user(root):

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fe9cc0ac000 nid=0x42f5 waiting on condition [0x0000000000000000]

[3] Busy(0.2%) thread(17122/0x42e2) stack of java process(17116) under user(root):

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fc98c0ac000 nid=0x42e2 waiting on condition [0x0000000000000000]

[4] Busy(0.2%) thread(16629/0x40f5) stack of java process(16623) under user(root):

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fba2c0ae000 nid=0x40f5 waiting on condition [0x0000000000000000]

[5] Busy(0.1%) thread(17144/0x42f8) stack of java process(17135) under user(root):

"VM Periodic Task Thread" os_prio=0 tid=0x00007fe9cc0b6800 nid=0x42f8 waiting on condition

第9章 开启tomcat远程监控功能:

[root@web03 bin]# vim catalina.sh        

CATALINA_OPTS="$CATALINA_OPTS

-Dcom.sun.management.jmxremote 

-Dcom.sun.management.jmxremote.port=12345  

-Dcom.sun.management.jmxremote.authenticate=false 

-Dcom.sun.management.jmxremote.ssl=false 

-Djava.rmi.server.hostname=10.0.0.17"

echo "10.0.0.17 web03" >>/etc/hosts

/application/tomcat8_1/bin/shutdown.sh 

/application/tomcat8_1/bin/startup.sh 

第10章 zabbix监控tomcat:

sed -i -e '220a JavaGateway=127.0.0.1' -e '236a StartJavaPollers=5' /etc/zabbix/zabbix_server.conf

systemctl restart zabbix-server.service

rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

yum  -y install zabbix-java-gateway

systemctl start zabbix-java-gateway.service

[root@m02 ~]# ss -tunlp |grep java

tcp    LISTEN     0      50       :::10052                :::*                   users:(("java",pid=22241,fd=11))

第11章 tomcat安全优化:

sed 's#SHUTDOWN#jiangboyang#g' /application/tomcat/conf/server.xml  -i

# <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

站点目录下仅留存ROOT目录,其他目录全部删除

pkill java

useradd tomcat

cp -a /application/tools/tomcat8_1 /home/tomcat/

chown -R tomcat.tomcat /home/tomcat/tomcat8_1/

su -c '/home/tomcat/tomcat8_1/bin/startup.sh' tomcat      -c表示用tmocat用户执行命令后在退出

第12章 tomcat性能优化:

jvm优化机制---垃圾回收机制,把不需要的内存回收,优化垃圾回收策略

优化catalina.sh配置文件:给tomcat分配指定大小的内存

vim /application/tomcat8_1/bin/catalina.sh    在97行下面添加如下内容

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m"

/application/tomcat8_1/bin/shutdown.sh

上一篇: Tomcat
下一篇: Tomcat