Linux運(yùn)維-day58-tomcat與阿里云

一土砂、tomcat

1.1 tomcat LNMT ※※※

網(wǎng)站代碼

PHP---LNMP
Java----LNMP

1.2 JVM

  • Windows版本的QQ放到Linux是否可以運(yùn)行储笑?不能
  • 代碼的可移植性:1份代碼可以運(yùn)行在不同的系統(tǒng)中
  • JAVA程序 會(huì)被放在1個(gè)叫虛擬機(jī)容器
  • JVM(Java Virtual Machine):Java虛擬機(jī)扰路,運(yùn)行Java程序褐着,1份代碼處處使用
1.2.1 JVM的核心環(huán)境---->jdk+tomcat
  • JDK(Java Development Kit):Java程序運(yùn)行環(huán)境
  • tomcat(nginx)存放Java程序代碼

1.3 準(zhǔn)備jdk和tomcat環(huán)境

1.3.1 環(huán)境準(zhǔn)備

在阿里云購買一臺(tái) ecs云服務(wù)器小作,做下簡單的優(yōu)化恒序,安裝基礎(chǔ)必備軟件

yum install -y tree vim wget bash-completion bash-completion-extras lrzsz net-tools sysstat iotop iftop htop unzip nc nmap telnet bc psmisc ntpdate cowsay sl sshpass pssh bind-utils

1.3.2 準(zhǔn)備jdk環(huán)境(ecs云服務(wù)器上)

先上傳tomcat大禮包到/app/tools下三圆,然后進(jìn)行如下操作

[root@tomcat /app/tools]# unzip tomcat.zip 
[root@tomcat /app/tools]# cd tomcat/
[root@tomcat /app/tools/tomcat]# tar xf jdk-8u60-linux-x64.tar.gz
[root@tomcat /app/tools/tomcat]# mkdir -p /application
[root@tomcat /app/tools/tomcat]# mv jdk1.8.0_60/ /application/
[root@tomcat /app/tools/tomcat]# ll /application/
total 4
drwxr-xr-x 8 10 143 4096 Aug  5  2015 jdk1.8.0_60
[root@tomcat /app/tools/tomcat]# ln -s /application/jdk1.8.0_60/ /application/jdk
[root@tomcat /app/tools/tomcat]# ll /application/
total 4
lrwxrwxrwx 1 root root   25 Jul  5 09:35 jdk -> /application/jdk1.8.0_60/
drwxr-xr-x 8   10  143 4096 Aug  5  2015 jdk1.8.0_60
[root@tomcat /app/tools/tomcat]# 
1.3.3 配置JDK環(huán)境變量(為了方便)
[root@tomcat /app/tools/tomcat]# cat >>/etc/profile<<'EOF'   
> export JAVA_HOME=/application/jdk
> export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
> export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/
> EOF
[root@tomcat /app/tools/tomcat]# source /etc/profile
[root@tomcat /app/tools/tomcat]# java -version  \\驗(yàn)證
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

注:當(dāng)前使用的JDK有兩種:

Oracle JDK(目前)
OpenJDK

1.3.4 部署Tomcat

Tomcat 是Apache軟件基金會(huì)中的一個(gè)開源軟件(tomcat.apache.org)
Apache對(duì)應(yīng)的web服務(wù)是httpd

[root@tomcat /app/tools/tomcat]# tar xf apache-tomcat-8.0.27.tar.gz 
[root@tomcat /app/tools/tomcat]# mv apache-tomcat-8.0.27 /application/
[root@tomcat /app/tools/tomcat]# ln -s /application/apache-tomcat-8.0.27/ /application/tomcat
[root@tomcat /app/tools/tomcat]# ll /application/
total 8
drwxr-xr-x 9 root root 4096 Jul  5 09:42 apache-tomcat-8.0.27
lrwxrwxrwx 1 root root   25 Jul  5 09:35 jdk -> /application/jdk1.8.0_60/
drwxr-xr-x 8   10  143 4096 Aug  5  2015 jdk1.8.0_60
lrwxrwxrwx 1 root root   34 Jul  5 10:22 tomcat -> /application/apache-tomcat-8.0.27/
[root@tomcat /app/tools/tomcat]# echo "export TOMCAT_HOME=/application
[root@tomcat /app/tools/tomcat]# tail -1 /etc/profile
export TOMCAT_HOME=/application/tomcat
[root@tomcat /app/tools/tomcat]# source /etc/profile
1.3.5 檢測 tomcat jdk環(huán)境變量是否ok
[root@tomcat /app/tools/tomcat]# /application/tomcat/bin/version.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
Server version: Apache Tomcat/8.0.27
Server built:   Sep 28 2015 08:17:25 UTC
Server number:  8.0.27.0
OS Name:        Linux
OS Version:     3.10.0-957.21.3.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_60-b27
JVM Vendor:     Oracle Corporation
[root@tomcat /app/tools/tomcat]# 
1.3.6 啟動(dòng)tomcat
[root@tomcat /app/tools/tomcat]# /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.
1.3.7 查看進(jìn)程、檢查端口
[root@tomcat /app/tools/tomcat]# ss -lntup|grep java
tcp    LISTEN     0      1      127.0.0.1:8005                  *:*                   users:(("java",pid=13622,fd=67))
tcp    LISTEN     0      100       *:8009                  *:*                   users:(("java",pid=13622,fd=51))
tcp    LISTEN     0      100       *:8080                  *:*                   users:(("java",pid=13622,fd=46))
[root@tomcat /app/tools/tomcat]# ps -ef|grep java
root     13622     1  6 10:29 pts/1    00:00:02 /application/jdk/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root     13661 13307  0 10:29 pts/1    00:00:00 grep --color=auto java
[root@tomcat /app/tools/tomcat]# 
1.3.8 瀏覽器檢查

注:tomcat默認(rèn)的端口是8080污呼,如果你的環(huán)境是云服務(wù)器裕坊,必須配置8080的安全組,瀏覽器才能正常顯示



1.4 tomcat目錄結(jié)構(gòu)

[root@tomcat /application/tomcat]# ll
total 112
drwxr-xr-x 2 root root  4096 Jul  5 09:42 bin  ##主要存放命令燕酷,核心的命令有startup.sh(開)籍凝、shutdown.sh(關(guān))、catalina.sh
drwxr-xr-x 3 root root  4096 Jul  5 10:29 conf  ##存放配置文件苗缩,核心配置:server.xml
drwxr-xr-x 2 root root  4096 Jul  5 09:42 lib  #tomcat以后安裝的插件(庫文件)
-rw-r--r-- 1 root root 57011 Sep 28  2015 LICENSE
drwxr-xr-x 2 root root  4096 Jul  5 10:29 logs  #日志
-rw-r--r-- 1 root root  1444 Sep 28  2015 NOTICE
-rw-r--r-- 1 root root  6741 Sep 28  2015 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 Sep 28  2015 RUNNING.txt
drwxr-xr-x 2 root root  4096 Jul  5 09:42 temp
drwxr-xr-x 7 root root  4096 Sep 28  2015 webapps  #類似于nginx的站點(diǎn)目錄
drwxr-xr-x 3 root root  4096 Jul  5 10:29 work
[root@tomcat /application/tomcat]# 
1.4.1 bin(主要存放命令)

1>catalina.sh

  • 是shutdow.sh和startup.sh調(diào)用的時(shí)候饵蒂,背后都在調(diào)用catalina.sh
  • 修改tomcat參數(shù)、修改酱讶,修改這個(gè)文件
    2>startup.sh
    3>shutdown.sh #關(guān)閉tomcat之后退盯,一定要檢查端口和進(jìn)程,是否關(guān)閉
1.4.2 conf(存放配置文件)

properties 屬性 [?prɑp?rtiz]

1>server.xml :主配置文件,相當(dāng)于nginx.conf ※ ※ ※
2>web.xml:配置擴(kuò)展功能得问、額外功能
3>logging.properties:日志的一些屬性
4>tomcat-users.xml:tomcat管理端用戶和密碼的配置文件囤攀,用于測試環(huán)境软免;生產(chǎn)環(huán)境宫纬,禁止使用。

1.4.3 logs

Destroying 毀滅
startup 啟動(dòng)
error 錯(cuò)誤
1>catalina.out ※ ※ ※
tomcat最核心的日志膏萧,這個(gè)日志默認(rèn)會(huì)被輪詢(定時(shí)切割)漓骚,切割之后catalina.out日志內(nèi)容不會(huì)被切割
2>catalina.2019-7-5.log #中間日期,是catalina.out的切割日志
3>localhost_access_log.2019-07-05.txt #訪問日志

1.5 配置tomcat管理端

  • 開頭字符集
  • 注釋:在xml格式中
  • rolename 角色(admin-gui榛泛、manager-gui蝌蹂、host-gui)

1.5.1 配置tomcat-users.xml

[root@oldboy /application/tomcat]# cat conf/tomcat-users.xml 
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
  <role rolename="admin-gui"/>
  <role rolename="manager-gui"/>
  <role rolename="host-gui"/>
  <user username="oldboy" password="oldboy" roles="admin-gui,manager-gui,host-gui"/>
</tomcat-users>

1.5.2 重啟tomcat使配置生效

[root@tomcat /application/tomcat]# /application/tomcat/bin/shutdown.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
[root@tomcat ~]# ps -ef|grep java
root      6305  6261  0 10:13 pts/0    00:00:00 grep --color=auto java
[root@tomcat ~]# ss -lntup|grep java
[root@tomcat /application/tomcat]# /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@tomcat /application/tomcat]# 

1.6 tomcat的主配置文件server.xml

  • 8080 http協(xié)議的端口
  • 8005 shutdown端口,telnet連接上這個(gè)端口曹锨,輸入SHUTDOWN暗號(hào)孤个,關(guān)閉tomcat,比較危險(xiǎn)沛简,估默認(rèn)只能本地連接8005端口
    <Server port="8005" shutdown="1 SHUTDOWN">
    
  • 管理端存放用戶名密碼的文件及存放方式
    <Resource name="UserDatabase" auth="Container"
         type="org.apache.catalina.UserDatabase"
         description="User database that can be updated and saved"
         factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
         pathname="conf/tomcat-users.xml" />
    
  • 指定http協(xié)議的端口齐鲤,https用8443端口
    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"   #超時(shí)時(shí)間,默認(rèn)毫秒
    redirectPort="8443" />
    
  • 8008 AJP與Apache進(jìn)行通訊用的
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    
  • 從<Host開始到</Host>結(jié)束椒楣,相當(dāng)于nginx中server虛擬機(jī)主機(jī)
Tomcat Nginx
Host name="localhost" server_name 域名
appBase="webapps" root 站點(diǎn)目錄
  • unpackWARs="true" #自動(dòng)解壓 app.war
  • autoDeploy="true" #Deploy 部署 自動(dòng)加載到tomcat
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
    
            <!-- SingleSignOn valve, share authentication between web applications
                 Documentation at: /docs/config/valve.html -->
            <!--
            <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
            -->
    
            <!-- Access log processes all example.
                 Documentation at: /docs/config/valve.html
                 Note: The pattern used is equivalent to using pattern="common" -->
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" /> #指定日志的文件和日志內(nèi)容的格式
    
          </Host>
    

1.7 Tomcat部署app(應(yīng)用)

1.7.1 準(zhǔn)備數(shù)據(jù)庫

1>安裝mariadb-server

[root@tomcat ~]# yum install -y  mariadb-server

2>啟動(dòng)mariadb给郊,并設(shè)置開機(jī)自啟

[root@tomcat ~]# systemctl start mariadb.service 
[root@tomcat ~]# systemctl enable mariadb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@tomcat ~]# 

3>進(jìn)入數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫和用戶

[root@tomcat ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4>創(chuàng)建jpress數(shù)據(jù)庫

MariaDB [(none)]> create database jpress DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

5>查看jpress數(shù)據(jù)庫是否創(chuàng)建成功

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jpress             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

6>創(chuàng)建用戶捧灰,并查看

MariaDB [(none)]> grant all on jpress.* to jpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on jpress.* to jpress@'172.16.%.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+--------+------------+
| user   | host       |
+--------+------------+
| root   | 127.0.0.1  |
| jpress | 172.16.%.% |
| root   | ::1        |
|        | localhost  |
| jpress | localhost  |
| root   | localhost  |
|        | tomcat     |
| root   | tomcat     |
+--------+------------+
8 rows in set (0.00 sec)

7>刪除為空的用戶

MariaDB [(none)]> drop user ''@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> drop user ''@tomcat;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+--------+------------+
| user   | host       |
+--------+------------+
| root   | 127.0.0.1  |
| jpress | 172.16.%.% |
| root   | ::1        |
| jpress | localhost  |
| root   | localhost  |
| root   | tomcat     |
+--------+------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> Bye
[root@tomcat ~]# 

8>測試數(shù)據(jù)庫

[root@tomcat ~]# mysql -ujpress -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Bye
[root@tomcat ~]# 

擴(kuò)展 mysql -e 參數(shù) 非交互式執(zhí)行SQL語句

[root@tomcat ~]# mysql -ujpress -p -e 'select user,host from mysql.user;'
Enter password: 
ERROR 1142 (42000) at line 1: SELECT command denied to user 'jpress'@'localhost' for table 'user'
[root@tomcat ~]# mysql -ujpress -p -e 'select user();'
Enter password: 
+------------------+
| user()           |
+------------------+
| jpress@localhost |
+------------------+
[root@tomcat ~]# 
1.7.2 準(zhǔn)備app(war包)

1>首先移動(dòng)war包到站點(diǎn)目錄淆九,并重命名

##首先移動(dòng)war包到站點(diǎn)目錄,并重命名
[root@tomcat /app/tools/tomcat]# mv jpress-web-newest.war /application/tomcat/webapps/jpress.war
[root@tomcat /app/tools/tomcat]# ll /application/tomcat/webapps/
total 20336
drwxr-xr-x 14 root root     4096 Jul  5 09:42 docs
drwxr-xr-x  6 root root     4096 Jul  5 09:42 examples
drwxr-xr-x  5 root root     4096 Jul  5 09:42 host-manager
drwxr-xr-x  7 root root     4096 Jul  5 12:43 jpress
-rw-r--r--  1 root root 20797013 Mar  3  2017 jpress.war
drwxr-xr-x  5 root root     4096 Jul  5 09:42 manager
drwxr-xr-x  3 root root     4096 Jul  5 09:42 ROOT
[root@tomcat /app/tools/tomcat]# 

2>在瀏覽器輸入http://47.92.92.94:8080/jpress/install安裝網(wǎng)站(注:這里的ip是我購買ecs自己分配的毛俏,每個(gè)每個(gè)人的不一樣





安裝完成之后需要重啟tomcat

[root@tomcat /app/tools/tomcat]# pkill java  #注:pkill java會(huì)將虛擬機(jī)中的所有Java進(jìn)程都?xì)⒌籼棵恚绻愕奶摂M機(jī)中有多個(gè)tomcat的話,請(qǐng)慎用煌寇;可以用/application/tomcat/bin/shutdown.sh
#停止tomcat進(jìn)程之后一定要檢查進(jìn)程和端口是否都已經(jīng)沒有了焕蹄,一定要保證都沒有了
[root@tomcat /app/tools/tomcat]# ps -ef|grep java  
root     14452 13307  0 13:02 pts/1    00:00:00 grep --color=auto java
[root@tomcat /app/tools/tomcat]# ss -lntup|grep java
[root@tomcat /app/tools/tomcat]# /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@tomcat /app/tools/tomcat]# 

重啟tomcat之后就可以正常訪問網(wǎng)站了


網(wǎng)站地址:http://47.92.92.94:8080/jpress/
網(wǎng)站后臺(tái)地址:http://47.92.92.94:8080/jpress/admin/login
注:以上的數(shù)據(jù)庫在本地,要改為遠(yuǎn)程服務(wù)器的修改db.properties文件就行

[root@tomcat /application/tomcat/webapps/jpress/WEB-INF/classes]# cat db.properties 
#Auto create by JPress
#Fri Jul 05 13:01:50 CST 2019
db_name=jpress
db_host_port=3306
db_tablePrefix=jpress_
db_host=localhost
db_password=*****
db_user=*****

溫馨提示:

  • 關(guān)閉Tomcat之后一定要檢查端口和進(jìn)程是否完全關(guān)閉
  • 如果沒有完全關(guān)閉唧席,會(huì)導(dǎo)致再啟動(dòng)tomcat時(shí)擦盾,會(huì)有大量的日志在catalina.out中,而且增加的非程视矗快迹卢,最終會(huì)導(dǎo)致磁盤滿了

1.8 Tomcat與負(fù)載均衡

1個(gè)tomcat存放1個(gè)app
1個(gè)tomcat存放1個(gè)app
比如有2臺(tái)負(fù)載均衡,前面就必須要有個(gè)負(fù)載均衡

upstream
proxy

upstream app_pools {
server 10.0.0.7:8080;
server 10.0.0.7:8081;
server 10.0.0.7:8082;
server 10.0.0.8:8080;
}

1.9 Tomcat多實(shí)例

比如:有一臺(tái)機(jī)器徒仓,配置(1臺(tái)服務(wù)器 4路 志強(qiáng)金牌 2TB 16*600G sas 15k)腐碱,在這臺(tái)機(jī)器上多運(yùn)行幾個(gè)tomcat,并且端口不一樣

1.10 tomcat監(jiān)控

1.10.1 命令行檢查

1>查看進(jìn)程信息

[root@tomcat /application/tomcat/webapps/jpress/WEB-INF/classes]# ps -ef|grep java
root     14463     1  0 13:02 pts/1    00:00:19 /application/jdk/bin/java #Java命令本身
    -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties #指定日志配置文件
    -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
    -Djava.endorsed.dirs=/application/tomcat/endorsed 
    -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar 
    -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat  #tomcat軟件的目錄
    -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap #臨時(shí)目錄
    start

如果要在上面添加一些參數(shù),修改的文件是catalina.sh
2>jps -lvm #專門檢查Java進(jìn)程

[root@tomcat /application/tomcat/bin]# jps -lvm
16744 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
16813 sun.tools.jps.Jps -lvm -
Denv.class.path=.:/application/jdk/lib:/application/jdk/jre/lib:/application/
jdk/lib/tools.jar -Dapplication.home=/application/jdk1.8.0_60 -Xms8m

3>show-busy-java-threads.sh #顯示當(dāng)前系統(tǒng)中 java進(jìn)程症见、負(fù)載喂走、cpu 使用率較高

ps -ef 進(jìn)程信息
進(jìn)程 process pid,實(shí)際相當(dāng)于房子或空間
線程 thread谋作,實(shí)際是處理用戶請(qǐng)求
查看進(jìn)程和線程的關(guān)系芋肠,pstree -pa

1.10.2 開啟tomcat監(jiān)控

應(yīng)用場景:是用于zabbix監(jiān)控tomcat zabbix監(jiān)控tomcat的方法之一
1>開啟監(jiān)控功能,在catalina.sh中的第二行加入以下內(nèi)容

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=IP地址"

分別代表的意思:

CATALINA_OPTS="$CATALINA_OPTS .*"    #表示Java程序啟動(dòng)的時(shí)候遵蚜,增加的參數(shù)
-Dcom.sun.management.jmxremote       #開啟tomcat遠(yuǎn)程監(jiān)控功能
-Dcom.sun.management.jmxremote.port=12345    #指定端口帖池,什么都行,別跟現(xiàn)有的沖突就行
-Dcom.sun.management.jmxremote.authenticate=false  #是否開啟認(rèn)證模式(用戶名吭净、密碼)
-Dcom.sun.management.jmxremote.ssl=false   #是否開啟https
-Djava.rmi.server.hostname=IP地址"     #監(jiān)聽的地址(本地)睡汹。實(shí)際工作寫上服務(wù)器的內(nèi)網(wǎng)ip地址;我們測試環(huán)境中寂殉,可以寫上服務(wù)器的公網(wǎng)ip

將上面的內(nèi)容添加到catalina.sh中囚巴,然后重啟tomcat

[root@tomcat ~]# ss -lntup |grep java
[root@tomcat ~]# ss -lntup |grep java
tcp LISTEN 0 100 *:8009 : users:(("java",pid=17028,fd=54))
tcp LISTEN 0 50 *:34345 : users:(("java",pid=17028,fd=19))
tcp LISTEN 0 100 *:8080 : users:(("java",pid=17028,fd=50))
tcp LISTEN 0 50 *:33142 : users:(("java",pid=17028,fd=21))
tcp LISTEN 0 50 *:12345 : users:(("java",pid=17028,fd=20))

2>windows下面 通過java 命令 連接tomcat (模擬zabbix監(jiān)控tomcat)

開發(fā)環(huán)境:jdk
命令1:jconsole
命令2:jvisualvm

二、云服務(wù)器

國內(nèi):阿里云友扰、騰旭彤叉、華為
國外:aws(外企)

2.1 阿里云服務(wù)器的架構(gòu)

2.2 專有名詞

專有名詞 含義
可用區(qū) zone 同一地區(qū)的不同機(jī)房
地區(qū) 地域 region 不同的地方 上海 北京 杭州
實(shí)例 虛擬機(jī)
RDS 云數(shù)據(jù)庫
NAS和OSS的相同點(diǎn)和不同點(diǎn):
  • 都是存放用戶上傳的附件
  • NAS是文件存儲(chǔ),實(shí)現(xiàn)nfs共享存儲(chǔ)焕檬,價(jià)格比較貴
  • OSS是對(duì)象存儲(chǔ)姆坚。大容量、訪問頻率高/低
  • OSS的價(jià)格分為幾種:1>訪問頻率低实愚、性能不高的就便宜點(diǎn)兼呵;2>要求實(shí)時(shí)性好、速度又快的就會(huì)貴點(diǎn)
  • OSS的使用腊敲,有兩種方法:
    • 1>把它真的的當(dāng)對(duì)象存儲(chǔ)用击喂,就是硬盤里的,當(dāng)網(wǎng)盤用
    • 2>通過程序調(diào)用碰辅,就是在程序代碼里寫懂昂,只要用戶傳數(shù)據(jù),就存到OSS

2.3 購買ECS云服務(wù)器没宾,同1.3.1 ※※※

image.png

2.4 購買RDS云數(shù)據(jù)庫 (數(shù)據(jù)庫遷移)※※※

2.4.1 購買RDS云數(shù)據(jù)庫的過程:

1>在阿里云中搜索RDS進(jìn)行創(chuàng)建實(shí)例凌彬,以下只是我的一個(gè)簡單的配置例子,每個(gè)人可以根據(jù)自己的情況進(jìn)行選擇


2>購買好RDS云數(shù)據(jù)庫之后循衰,因?yàn)闀簳r(shí)沒有ip地址铲敛,故無法連接,可以點(diǎn)擊圖中的任意一個(gè)進(jìn)入下一個(gè)頁面会钝,先設(shè)置白名單伐蒋,就可以獲得RDS的內(nèi)網(wǎng)地址

3>進(jìn)入白名單列表頁面,添加或修改已有的都可以

注:設(shè)置的白名單可以是多個(gè)ip或多個(gè)網(wǎng)段,中間用英文的逗號(hào)隔開先鱼,也可以直接寫0.0.0.0/0俭正,所有的ip都可以,但這個(gè)不安全焙畔,不建議設(shè)置掸读。
4>白名單設(shè)置成功之后,就可以看到內(nèi)網(wǎng)地址了(每個(gè)人的都不一樣)

5>上面的內(nèi)網(wǎng)地址是域名闹蒜,要想知道具體的內(nèi)網(wǎng)ip地址寺枉,去xshell中ping下就知道了

這樣RDS云數(shù)據(jù)庫就買好了

2.4.2 數(shù)據(jù)庫遷移:

1>從ECS云服務(wù)器里導(dǎo)出備份jpress數(shù)據(jù)庫到/root目錄下

[root@tomcat ~]# mysqldump -ujpress -p -B jpress >/root/jpress.sql
Enter password: 
[root@tomcat ~]# ll
total 20
-rw-r--r-- 1 root root 17409 Jul  5 17:18 jpress.sql
[root@tomcat ~]# 

2>遠(yuǎn)程進(jìn)入購買的RDS云數(shù)據(jù)庫中創(chuàng)建空的jpress數(shù)據(jù)庫

[root@tomcat ~]# mysql -uwwj_mysql -p -h rm-8vbzbcm62xy92g82q.mysql.zhangbei.rds.aliyuncs.com
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1997
Server version: 5.7.25-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> create database jpress  DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jpress             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> show tables from jpress;
Empty set (0.00 sec)

MySQL [(none)]> 

3>把備份的jpress數(shù)據(jù)庫還原到RDS云數(shù)據(jù)庫上抑淫,并查看

[root@tomcat ~]# mysql -uwwj_mysql -p -h rm-8vbzbcm62xy92g82q.mysql.zhangbei.rds.aliyuncs.com </root/jpress.sql 
Enter password: 
[root@tomcat ~]# mysql -uwwj_mysql -p -h rm-8vbzbcm62xy92g82q.mysql.zhangbei.rds.aliyuncs.com
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2096
Server version: 5.7.25-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show tables from jpress;
+-------------------+
| Tables_in_jpress  |
+-------------------+
| jpress_attachment |
| jpress_comment    |
| jpress_content    |
| jpress_mapping    |
| jpress_metadata   |
| jpress_option     |
| jpress_taxonomy   |
| jpress_user       |
+-------------------+
8 rows in set (0.00 sec)

MySQL [(none)]>

4>停止ECS云服務(wù)器上的mysql服務(wù)

[root@tomcat ~]# systemctl stop mariadb.service 
[root@tomcat ~]# systemctl disable mariadb.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/mariadb.service.
[root@tomcat ~]# 

5>修改數(shù)據(jù)庫配置

vim /application/tomcat/webapps/jpress/WEB-INF/classes/db.properties



6>重啟tomcat服務(wù)

[root@tomcat ~]# pkill java #或/application/tomcat/bin/shutdown.sh 
[root@tomcat ~]# ss -lntup|grep java
[root@tomcat ~]# ps -ef|grep java
root     15205 14908  0 17:32 pts/0    00:00:00 grep --color=auto java
[root@tomcat ~]# /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@tomcat ~]# 

7>測試數(shù)據(jù)庫是否遷移成功
打開部署好的jpress網(wǎng)站绷落,刷新查看網(wǎng)站是否正常,再在網(wǎng)站隨便寫個(gè)文章始苇,遠(yuǎn)程進(jìn)入RDS云數(shù)據(jù)庫中砌烁,查看數(shù)據(jù)是否存在

2.5 購買文件存儲(chǔ)NAS (部署nfs存儲(chǔ))※※※

2.5.1 購買文件存儲(chǔ)NAS的過程

1>在阿里云中搜索NAS,如圖:


2>創(chuàng)建文件系統(tǒng)

3>文件系統(tǒng)創(chuàng)建好之后催式,添加掛載點(diǎn)

4>點(diǎn)擊文件系統(tǒng)列表中管理進(jìn)入詳細(xì)頁面函喉,可以看到掛載地址或掛載命令

到這里NAS文件存儲(chǔ)系統(tǒng)就購買好了厅瞎,下面就到ECS云服務(wù)器中進(jìn)行掛載

2.5.1 ECS云服務(wù)器中安裝nfs-utils準(zhǔn)備進(jìn)行掛載

1>安裝nfs-utils溯壶,啟動(dòng)nfs-utils服務(wù)尽超,并設(shè)置開機(jī)自啟

[root@tomcat ~]# yum install -y nfs-utils
[root@tomcat ~]# systemctl start nfs-utils
[root@tomcat ~]# systemctl enable nfs-utils
[root@tomcat ~]# 

2>進(jìn)行掛載測試

[root@tomcat ~]# mount -t nfs 9db184ad00-hrk84.cn-zhangjiakou.nas.aliyuncs.com:/ /mnt
[root@tomcat ~]# df -h
Filesystem                                          Size  Used Avail Use% Mounted on
/dev/vda1                                            40G  3.1G   35G   9% /
devtmpfs                                            909M     0  909M   0% /dev
tmpfs                                               920M     0  920M   0% /dev/shm
tmpfs                                               920M  456K  919M   1% /run
tmpfs                                               920M     0  920M   0% /sys/fs/cgroup
tmpfs                                               184M     0  184M   0% /run/user/0
9db184ad00-hrk84.cn-zhangjiakou.nas.aliyuncs.com:/  1.0P     0  1.0P   0% /mnt
[root@tomcat ~]# 

3>掛載點(diǎn)遷移

[root@tomcat ~]# mv /application/tomcat/webapps/jpress/attachment/* /mnt
[root@tomcat ~]# ll /application/tomcat/webapps/jpress/attachment/
total 0
[root@tomcat ~]# umount /mnt
[root@tomcat ~]# mount -t nfs 9db184ad00-hrk84.cn-zhangjiakou.nas.aliyuncs.com:/ /application/tomcat/webapps/jpress/attachment/
[root@tomcat ~]# df -h
Filesystem                                          Size  Used Avail Use% Mounted on
/dev/vda1                                            40G  3.1G   35G   9% /
devtmpfs                                            909M     0  909M   0% /dev
tmpfs                                               920M     0  920M   0% /dev/shm
tmpfs                                               920M  456K  919M   1% /run
tmpfs                                               920M     0  920M   0% /sys/fs/cgroup
tmpfs                                               184M     0  184M   0% /run/user/0
9db184ad00-hrk84.cn-zhangjiakou.nas.aliyuncs.com:/  1.0P     0  1.0P   0% /application/apache-tomcat-8.0.27/webapps/jpress/attachment
[root@tomcat ~]# 

2.6 目前只部署了一臺(tái)web服務(wù)器(ECS云服務(wù)器)蜓席,如果要多臺(tái)吴菠,將上面部署好網(wǎng)站的web服務(wù)器先做快照和鏡像字柠,再根據(jù)鏡像創(chuàng)建多臺(tái)web服務(wù)器就可以

注意:

  • 創(chuàng)建快照之前一定要檢查好這臺(tái)ECS云服務(wù)器里面的服務(wù)是否都設(shè)置開機(jī)自啟動(dòng)弧哎,掛載是否永久掛載朵逝,否則后面根據(jù)鏡像創(chuàng)建的云服務(wù)器里面的服務(wù)都要單獨(dú)啟動(dòng)萌业,很麻煩
  • 永久掛載可以寫入到/etc/fstab中坷襟,也可以寫入/etc/rc.d/rc.local中
  • 對(duì)一些無法通過systemctl設(shè)置開機(jī)自啟動(dòng)的服務(wù),將他們的啟動(dòng)方式寫入到/etc/rc.d/rc.local中
  • 如果寫入到/etc/rc.d/rc.local中生年,就必須給/etc/rc.d/rc.local這個(gè)文件添加可執(zhí)行權(quán)限
[root@tomcat ~]# cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
source /etc/profile   #注意:在啟動(dòng)tomcat時(shí)婴程,需要配置在/etc/profile中配置好環(huán)境變量,估這里需要source /etc/profile生效環(huán)境變量抱婉,否則無法啟動(dòng)档叔,也可以寫為: . /etc/profile
mount -t nfs 9db184ad00-hrk84.cn-zhangjiakou.nas.aliyuncs.com:/ /application/tomcat/webapps/jpress/attachment/
/application/tomcat/bin/startup.sh
[root@tomcat ~]# chmod +x /etc/rc.d/rc.local 
[root@tomcat ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 619 Jul  5 18:21 /etc/rc.d/rc.local
[root@tomcat ~]# 

2.7 購買SLB負(fù)載均衡

2.7.1 購買SLB的過程

1>在阿里云中搜索SLB



2>創(chuàng)建負(fù)載均衡



3>選擇負(fù)載均衡的簡單配置(根據(jù)自己的情況來定)

4>購買好SLB負(fù)載均衡之后進(jìn)行設(shè)置








到這里簡單的SLB云負(fù)載均衡就購買配置完成了
2.7.2 在兩臺(tái)web云服務(wù)器上簡單配置個(gè)頁面,然后在瀏覽器測試負(fù)載均衡
[root@tomcat ~]# echo web01   >/application/tomcat/webapps/ROOT/oldboy.jsp 
[root@tomcat ~]# 
[root@tomcat02 ~]# echo web02   >/application/tomcat/webapps/ROOT/oldboy.jsp
[root@tomcat02 ~]# 

測試方法:可以試著將其中一臺(tái)web服務(wù)器上的tomcat停掉蒸绩,然后瀏覽器頁面查看是否還能正常顯示

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末衙四,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子侵贵,更是在濱河造成了極大的恐慌届搁,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異卡睦,居然都是意外死亡宴胧,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門表锻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恕齐,“玉大人,你說我怎么就攤上這事瞬逊∠云纾” “怎么了?”我有些...
    開封第一講書人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵确镊,是天一觀的道長士骤。 經(jīng)常有香客問我,道長蕾域,這世上最難降的妖魔是什么拷肌? 我笑而不...
    開封第一講書人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮旨巷,結(jié)果婚禮上巨缘,老公的妹妹穿的比我還像新娘。我一直安慰自己采呐,他們只是感情好若锁,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著斧吐,像睡著了一般又固。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上会通,一...
    開封第一講書人閱讀 49,079評(píng)論 1 285
  • 那天口予,我揣著相機(jī)與錄音,去河邊找鬼涕侈。 笑死沪停,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的裳涛。 我是一名探鬼主播木张,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼端三!你這毒婦竟也來了舷礼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤郊闯,失蹤者是張志新(化名)和其女友劉穎妻献,沒想到半個(gè)月后蛛株,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡育拨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年谨履,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片熬丧。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡笋粟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出析蝴,到底是詐尸還是另有隱情害捕,我是刑警寧澤,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布闷畸,位于F島的核電站尝盼,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏腾啥。R本人自食惡果不足惜东涡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望倘待。 院中可真熱鬧,春花似錦组贺、人聲如沸凸舵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽啊奄。三九已至,卻和暖如春掀潮,著一層夾襖步出監(jiān)牢的瞬間菇夸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來泰國打工仪吧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留庄新,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓薯鼠,卻偏偏與公主長得像择诈,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子出皇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容