java企业应用之Tomcat
第1章 Tomcat简介
tomcat属于apache软件基金会中的一个项目
tomcat服务器是一个开源的web应用服务器,属于轻量级应用服务器,在中小型系统和并发系统用户不是很多的场合下被普遍使用,是开发和调试jsp的首选
1.1 jvm简介:
jvm即java虚拟机的缩写
java的特性:一次编译,到处运行
类比vmware的linux系统
1.2 jdk简介:
jdk即java语言的软件开发工具,包含jvm
常见的jdk软件: oracle jdk 属于商业软件
open jdk 开源软件
第2章 部署tomcat
2.1 环境准备:
[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
2.2 安装tomcat
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
2.3 安装jdk: 部署java环境
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
source /etc/profile
java -version
2.4 安装数据库,创建数据库并创建用户:
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';
2.5 启动tomcat:
[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启动慢的原因:
3.1 查看系统中的熵:
[root@web03 logs]# cat /proc/sys/kernel/random/entropy_avail
178
3.2 熵越大,启动速度会越快
yum -y install rng-tools # 安装rngd服务(熵服务,增大熵池)
systemctl start rngd # 启动服务
第4章 tomcat基于web页面管理:
[root@web03 conf]# vim tomcat-users.xml
在倒数第二行下面添加
第5章 tomcat主配置文件:
5.1 配置文件目录
[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
5.2 主配置文件:
[root@web03 tomcat]# grep Server conf/server.xml
tomcat和apache用来建立连接的端口
connectionTimeout="20000" redirectPort="8443" /> jpress就是类似wordpress的博客站点 yum -y install mariadb-server systemctl start mariadb.service mysql cd /application/tools/ /application/tomcat8_1/bin/startup.sh echo 8081 >>/application/tomcat8_1/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 { root html; } } } [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 : out.println("Max memory:"+mm+"MB"+" out.println("Total memory:"+tm+"MB"+" out.println("Free memory:"+fm+"MB"+" out.println("Available memory can be used is :"+(mm+fm-tm)+"MB"+" %> 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] java.lang.Thread.State: RUNNABLE [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] java.lang.Thread.State: RUNNABLE [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] java.lang.Thread.State: RUNNABLE [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 [root@web03 bin]# vim catalina.sh CATALINA_OPTS="$CATALINA_OPTS echo "10.0.0.17 web03" >>/etc/hosts /application/tomcat8_1/bin/shutdown.sh sed -i -e '220a JavaGateway=127.0.0.1' -e '236a StartJavaPollers=5' /etc/zabbix/zabbix_server.conf 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)) sed 's#SHUTDOWN#jiangboyang#g' /application/tomcat/conf/server.xml -i # 站点目录下仅留存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用户执行命令后在退出 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 /application/tomcat8_1/bin/startup.sh第6章 搭建jpress:
6.1 把jpress代码上传到站点目录
6.2 安装数据库:
create database jpress DEFAULT CHARACTER SET utf8;
grant all on jpress.* to jpress@'localhost' identified by '123456'; 6.3 在web页面配置好以后要重启tomcat
第7章 tomcat多实例:
7.1 以添加两个实例为例:
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_2/bin/startup.sh
echo 8082 >>/application/tomcat8_2/webapps/ROOT/index.jsp 7.2 利用nginx实现反向代理:
第8章 tomcat监控:
8.1 如何查看java使用情况?
8.1.1 利用tomcat自带函数检测:
");
");
");
");
");8.1.2 利用jps命令查看:
8.1.3 执行脚本查看java内存使用情况:
8.1.4 windows安装jdk利用jconsole和jvjsualvm图形监控工具
第9章 开启tomcat远程监控功能:
9.1 在97行下面添加如下内容
-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"9.2 hosts文件进行解析:
9.3 重启tomcat服务
/application/tomcat8_1/bin/startup.sh 9.4 检查12345端口是否存在
第10章 zabbix监控tomcat:
10.1 开启zabbix-server监控java功能
systemctl restart zabbix-server.service10.2 安装zabbix-java-gatewey(java程序)也需要jdk(openjdk)
10.3 tomcat开启 远程监控功能:每个实例都要开启
第11章 tomcat安全优化:
11.1 关闭端口保护:
11.2 关闭apache和tomcat连接的端口
11.3 禁用管理端:
11.4 使用普通用户运行tomcat
第12章 tomcat性能优化:
12.1 tomcat性能取决于你内存的大小
12.1.1 方法一:优化代码
12.1.2 方法二:
12.1.3 方法三:加大物理机的内存
12.1.4 方法四:每天0点重启tomcat服务