TOMCAT-NTM架构示例

NTM架构
mysql先启,tomcat后起,顺序颠倒tomcat起不来,开发二次重连机制

tomcat介绍

官方网站:
http://tomcat.apache.org/

特点
支持servlet + JSP
server+applet服务器端的应用程序 解析JSP动态请求

Tomcat 技术先进、性能稳定,免费

要求:
需要有java环境
yum install -y java

功能:
解析JSP的动态web-server。

tomcat的端口:
8080:应用程序使用,类似于nginx或apache的80
8009:容器(卡车 桶 水)使用
8005:关闭tomcat时使用


1. 安装tomcat

安装顺序:先安装jdk,在安装tomcat
(jdk包的环境可以自己编写,安装前跟开发勤沟通,否则最后有可能用不了)

[root@tomcat1 ~]# lftp 172.16.0.99
lftp 172.16.0.99:~> cd scripts/     
lftp 172.16.0.99:/scripts> get jdk-1.8.sh tomcat-8.0.50_jdk1.8_apr-xxs.sh 

[root@tomcat1 ~]# chmod +x jdk-1.8.sh tomcat-8.0.50_jdk1.8_apr-xxs.sh 
[root@tomcat1 ~]# ./jdk-1.8.sh 
	部署java环境
[root@tomcat1 ~]# ./tomcat-8.0.50_jdk1.8_apr-xxs.sh
	安装tomcat应用程序

设置环境变量和启动关闭介绍
jdk和tomcat 绿色版的安装
需要在/etc/profile设置环境变量(具体设置参照脚本)

[root@tomcat1 ~]# . /etc/profile
启动:
[root@tomcat1 /opt/apache-tomcat-8.0.50/bin]# ./startup.sh 

[root@tomcat1 ~]# startup.sh

关闭:
[root@tomcat1 /opt/apache-tomcat-8.0.50/bin]# ./shutdown.sh

[root@tomcat1 ~]# shutdown.sh

查看启动状态

在错误日志,查看启动效果
[root@tomcat1 /opt/apache-tomcat-8.0.50/logs]# less catalina.out
26-Nov-2019 09:58:02.302 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-apr-8080"]
26-Nov-2019 09:58:02.303 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-apr-8009"]
	错误日志,查看启动效果

启动后的3个端口
[root@tomcat1 ~]# netstat -antp | grep java
tcp6   0      0 :::8080                 :::*     LISTEN      15062/java
tcp6   0      0 127.0.0.1:8005          :::*     LISTEN      15062/java
tcp6   0      0 :::8009                 :::*     LISTEN      15062/java


部署应用程序:
是不是好用的?
不能看端口
得访问
启动时间有可能很长


tomcat的工作模式:

(1) BIO
blocking IO 阻塞式的IO操作,默认的,效率最低的

(2) NIO
new IO 有缓冲区、并且支持非阻塞式IO操作,
non-blocking IO 需要修改tomcat的配置文件

[root@tomcat1 /opt/apache-tomcat-8.0.50/conf]# vim server.xml 
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /><Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />

(3) APR
apache portable running apache可以直接的运行模式
最高效的

tomcat的端口:

8080:应用程序使用,类似于nginx或apache的80
8009:容器(卡车 桶 水)使用
8005:关闭tomcat时使用

讲解配置文件:

注释符号:
	<!--
	内容
	-->

(1)context.xml

tomcat的环境配置文件

tomcat的环境配置文件
定义了2个资源文件
${catalina.base}/conf/web.xml
	||
/opt/apache-tomcat-8.0.50/conf/web.xml

WEB-INF/web.xml
	||
/opt/apache-tomcat-8.0.50/webapps/ROOT/WEB-INF/web.xml
	开发的同事写的(webapps/是存放开发同事写的war文件,tomcat会自动解包)		

(2)web.xml

web应用程序的说明文件
默认

(3)tomcat-users.xml

用户配置文件
默认

(4)server.xml

主配置文件

	主配置文件
<Server port="8005" shutdown="SHUTDOWN">
	整个配置文件的根元素

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
	指定tomcat的端口,协议,连接的超时时间(ms),加密端口(?)
	connector是连接client与tomcat之间的连接器

    <Engine name="Catalina" defaultHost="localhost">
	为tomcat处理所有的请求,并且指定默认的虚拟主机是哪个

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
		name:访问的名字
		appBase:默认的访问路径
		unpackWARs:当tomcat处于运行状态,如果检测到有新的应用程序(war包)被加载,会自动解压
		autoDeploy:当tomcat处于运行状态,会自动发布新的web应用

        <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" />
			访问日志		
[root@tomcat1 /opt/apache-tomcat-8.0.50/logs]# less localhost_access_log.2019-11-26.txt
      </Host>
	  虚拟主机
    </Engine>
  </Service>
</Server>

操作1:修改默认端口 8080

[root@tomcat1 /opt/apache-tomcat-8.0.50/conf]# vim server.xml 
    <Connector port="1234" protocol="HTTP/1.1"

[root@tomcat1 ~]# shutdown.sh
[root@tomcat1 ~]# startup.sh

[root@tomcat1 ~]# netstat -antp | grep java

操作2:单实例,双war包
网站的前台 war包
网站的后台 war包

[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# ls
Manager  Manager.war  ROOT  ROOT.war

[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# echo "www.xxx.com" > ROOT/index.html
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# echo manager.xxx.com > Manager/index.html

[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# ls ROOT
index.html
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# ls Manager
index.html

[root@tomcat1 ~]# shutdown.sh
[root@tomcat1 ~]# startup.sh

访问前台:
http://172.16.0.61:1234/
访问后台:
http://172.16.0.61:1234/Manager/

操作3:双实例,双war包
每个实例有一个war包,单机双实例

[root@tomcat1 ~]# shutdown.sh 

[root@tomcat1 /opt]# cp -a apache-tomcat-8.0.50/ apache-tomcat-8.0.50_2

[root@tomcat1 /opt/apache-tomcat-8.0.50_2/conf]# vim server.xml 
	修改了 3个 端口
		1234(原8008) --> 2234
		8005 --> 9005
		8009 --> 9009

[root@tomcat1 /opt/apache-tomcat-8.0.50_2/webapps]# ls
Manager  Manager.war

[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# ls
ROOT  ROOT.war

[root@tomcat1 /opt/apache-tomcat-8.0.50/bin]# ./startup.sh
[root@tomcat1 /opt/apache-tomcat-8.0.50_2/bin]# ./startup.sh

[root@tomcat1 ~]# netstat -antp | grep java
tcp6    0      0 127.0.0.1:9005     :::*     LISTEN      18592/java
tcp6    0      0 :::9009            :::*     LISTEN      18592/java
tcp6    0      0 :::1234            :::*     LISTEN      18542/java
tcp6    0      0 :::2234            :::*     LISTEN      18592/java
tcp6    0      0 127.0.0.1:8005     :::*     LISTEN      18542/java
tcp6    0      0 :::8009            :::*     LISTEN      18542/java

访问前台:
http://172.16.0.61:1234/

访问后台:
http://172.16.0.61:2234/Manager/


部署应用(NTM架构):

论坛,网上下载的。

172.16.0.51 nginx1
172.16.0.61 tomcat1
172.16.0.62 tomcat2
172.16.0.71 mysqlA
172.16.0.81 NFS
在这里插入图片描述

1. tomcat1

(利用上边实验的机器)

2. 部署mysql

先获取做论坛的tar包
[root@tomcat1 ~]# lftp 172.16.0.99
lftp 172.16.0.99:~> cd tar          
cd ok, cwd=/tar
lftp 172.16.0.99:/tar> get ejforum-2.3.zip 

[root@tomcat1 ~]# yum install -y unzip
[root@tomcat1 ~]# unzip ejforum-2.3.zip

[root@tomcat1 ~/ejforum-2.3/ejforum/]# cp  *  /opt/apache-tomcat-8.0.50/webapps/ROOT/   ---> 拷贝文件还是拷贝文件夹,自己看好了.
[root@tomcat1 ~/ejforum-2.3]# cd /opt/apache-tomcat-8.0.50/webapps/
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# ls
ROOT
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps]# cd ROOT/
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps/ROOT]# ls

[root@tomcat1 ~/ejforum-2.3/install/script]# scp easyjforum_mysql.sql 172.16.0.71:/tmp/

进入mysql设置相关参数
[root@mysqlA ~]# mysql -u root -p
Enter password:
mysql> create database ejf;		# 建库
mysql> grant all on ejf.* to ejf@'172.16.%' identified by 'ABC123qwe#';		# 设置对应库的权限(mysql里ejf@"172.16.%"   不包含"localhost"---> grant all on ejf.* to ejf@"localhost" identified by "12345"; 这样设置的权限才可以实现本地登录)
mysql> flush privileges;

[root@mysqlA ~]# mysql -u root -p ejf < /tmp/easyjforum_mysql.sql 
Enter password:

mysql> use ejf
mysql> show tables;
	看到表,OK

tomcat连接mysql:

[root@tomcat02 /opt/apache-tomcat-8.0.50/webapps/ROOT/ejforum/WEB-INF/conf]# ls
config.xml  setting_default.xml
[root@tomcat02 /opt/apache-tomcat-8.0.50/webapps/ROOT/ejforum/WEB-INF/conf]# cp config.xml /opt/apache-tomcat-8.0.50/webapps/ROOT/WEB-INF/conf/
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps/ROOT/WEB-INF/conf]# vim config.xml 
    <database maxActive="10" maxIdle="10" minIdle="2" maxWait="10000" 
              username="ejf" password="ABC123qwe#" 
              driverClassName="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://172.16.0.71:3306/ejf?characterEncoding=gbk&amp;autoReconnect=true&amp;autoReconnectForPools=true&amp;zeroDateTimeBehavior=convertToNull"
              sqlAdapter="sql.MysqlAdapter"/>
# username--> 用户名
# password--> 密码
# localhost--> 设置mysql服务器的ip
# databases--> 库名 "ejf"

[root@tomcat1 ~]# startup.sh 

浏览器访问:http://172.16.0.61:1234/     -----> (TOMCAT1)


3. 使用nginx代理tomcat

[root@nginx1 ~]# cd /usr/local/nginx/
[root@nginx1 /usr/local/nginx]# vim conf/nginx.conf
user  www;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
    use epoll;			# 添加
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;

    upstream tomcat_server {
        ip_hash;
        server 172.16.0.61:1234 weight=10 max_fails=2 fail_timeout=3s;
    }
    server {
        listen       80;						  # 端口
        server_name  www.ejf.com 172.16.0.51;     # 对应nginx的ip,或者解析的域名
        location / {
            proxy_pass http://tomcat_server/;
            include /usr/local/nginx/conf/proxy.conf;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

[root@nginx1 /usr/local/nginx]# vim conf/proxy.conf
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;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

浏览器访问:http://172.16.0.51/    --->对应nginxip



4. 使用nginx做软件七层负载均衡集群

tomcat1 虚拟机克隆tomcat2
让nginx做代理
    upstream tomcat_server {
        ip_hash;
        server 172.16.0.61:1234 weight=10 max_fails=2 fail_timeout=3s;
        server 172.16.0.62:1234 weight=10 max_fails=2 fail_timeout=3s;
    }

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

5. 加入集中存储–NFS,保证数据统一

为了实现上传数据(照片,图片)的统一性,防止某个服务器因数据不统一导致访问后数据不同步的问题,搭建NFS共享上传目录可以很好的解决这一问题。

[root@NFS ~]# yum install -y nfs-utils		# nfs 服务器安装服务 

配置nfs服务
[root@NFS ~]# mkdir /nfs		# 创建共享目录
[root@NFS ~]# chmod 757 /nfs	# 给共享目录权限
[root@NFS ~]# vim /etc/exports	# 编辑配置文件,设置访问权限等参数
/nfs    172.16.0.61(rw,sync,no_root_squash) 172.16.0.62(rw,sync,no_root_squash)
# 将tomcat的地址设置在这里,配置权限

启动服务
[root@NFS ~]# systemctl start nfs
[root@NFS ~]# systemctl enable nfs

tomcat服务器配置共享目录

开始准备挂载


在tomcat服务器上安装nfs
[root@tomcat1 ~]# yum install -y nfs-utils		# tomcat服务器安装时为了挂载nfs目录
[root@tomcat2 ~]# yum install -y nfs-utils		# 安装完才可以使用showmoun -e 命令

查看nfs服务器端的共享目录
[root@tomcat1 ~]# showmount -e 172.16.0.81
[root@tomcat2 ~]# showmount -e 172.16.0.81
Export list for 172.16.0.81:
/nfs 172.16.0.62,172.16.0.61

挂载nfs服务器提供的nfs目录,到挂载点mnt (这步是因为tomcat1 服务器这段已经有数据了,如果直接将upload挂载nfs的共享目录,那么upload目录就会变为挂载点,数据就会"清空",所以先临时将nfs和mnt关联挂载关系)
[root@tomcat1 ~]# mount 172.16.0.81:/nfs /mnt/	# 临时挂载到mnt
[root@tomcat1 ~]# cd -
/opt/apache-tomcat-8.0.50/webapps/ROOT/upload	#进入上传目录
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps/ROOT/upload]# ls
201911  avatar  index.jsp  temp
[root@tomcat1 /opt/apache-tomcat-8.0.50/webapps/ROOT/upload]# cp -a * /mnt/		#将上传目录的数据,全部拷贝到mnt(已经和nfs建立挂载关系.)

[root@tomcat1 ~]# umount /mnt/   # 拷贝完成后卸载挂载点

[root@tomcat1 ~]# shutdown.sh # 关闭tomcat服务
[root@tomcat1 ~]# mount 172.16.0.81:/nfs /opt/apache-tomcat-8.0.50/webapps/ROOT/upload/		# 将nfs共享目录和上传目录进行挂载
[root@tomcat1 ~]# startup.sh 	# 开启服务

[root@tomcat2 ~]# shutdown.sh
[root@tomcat2 ~]# mount 172.16.0.81:/nfs /opt/apache-tomcat-8.0.50/webapps/ROOT/upload/
[root@tomcat2 ~]# startup.sh 

将最后的挂在项添加到rc.local的自启动设置,这样设备重启后就不需要手动挂载了.
[root@tomcat1 ~]# tail -2 /etc/rc.local 
[root@tomcat2 ~]# tail -2 /etc/rc.local 
mount 172.16.0.81:/nfs /opt/apache-tomcat-8.0.50/webapps/ROOT/upload/
/opt/apache-tomcat-8.0.50/bin/startup.sh

以上为 NTM 架构!!!

查看状态

tomcat端口

[root@tomcat2 ~]# netstat -antp | grep java
tcp6   0  0 :::1234              :::*                 LISTEN      1242/java             # 外网端口
tcp6   0  0 127.0.0.1:8005       :::*                 LISTEN      1242/java       #  关闭端口
tcp6   0  0 :::8009              :::*                 LISTEN      1242/java           # 容器
tcp6   0  0 172.16.14.22:1234    172.16.14.253:59029  ESTABLISHED 1242/java           # 当有客户端访问时会产生
tcp6   0  0 172.16.14.22:40302   172.16.14.24:3306    ESTABLISHED 1242/java  # 连接mysql的端口

参考

tomcat 三个工作模式,几个端口 (面试)

端口提示close_wait
172.16.14.21 tomcat
172.16.14.24 mysql

[root@tomcat1 conf]# netstat -antp | grep java
tcp6       0      0 :::1234                 :::*                    LISTEN      1178/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      1178/java           
tcp6       0      0 172.16.14.21:60848      172.16.14.24:3306       CLOSE_WAIT  1178/java

解决方法:检查tomcat与mysql连接的配置文件,字母是否错误

无法运行 /opt/apache-tomcat-8.0.50/bin/startup.sh
解决方法: . /etc/profile # 重新加载变量 (可以添加变量到开机启动)

无法重新加载
[root@nginx1 nginx]# ./sbin/nginx -s reload
nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”
解决:先杀掉所有nginx进程,方法:killall -9 nginx
再重新加载 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
再次执行[root@nginx1 nginx]# ./sbin/nginx -s reload就没问题了

8005端口无法启动:

vim $JAVA_HOME/jre/lib/security/java.security

第117行
将原本的: securerandom.source=file:/dev/random 
修改为:  securerandom.source=file:/dev/urandom

tomcat访问tomcat8080,出现白板
删除ROOT目录
重新创建ROOT
重新拷贝ejf目录内的文件,
重新配置config.xml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值