欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署

程序员文章站 2022-05-24 10:22:24
...
目录
大型网站分布式架构(一)—— SpringBoot构建项目
大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署
大型网站分布式架构(三)—— 使用Apache Bench进行压力测试
大型网站分布式架构(四)—— JVisualVM远程监控Tomcat
大型网站分布式架构(五)—— Apache Bench与JVisualVM联调
大型网站分布式架构(六)—— Nginx的安装
大型网站分布式架构(七)—— Nginx整合Tomcat实现动静分离
大型网站分布式架构(八)—— Tomcat集群横向拓展 + Nginx负载均衡
大型网站分布式架构(九)—— 从Cookie机制到会话Session再到集群中Session的场景
大型网站分布式架构(十)—— Springboot + SpringSession + Redis实现Tomcat集群Session共享
大型网站分布式架构(十一)—— Keepalived+Nginx+Tomcat集群实现服务的高可用HA

获取安装包

[aaa@qq.com ~]# cd /export/servers/
[aaa@qq.com servers]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz

解压安装包

[aaa@qq.com servers]# tar -zvxf apache-tomcat-8.0.53.tar.gz

启动Tomcat

前提配置好Java环境

[aaa@qq.com servers]# cd apache-tomcat-8.0.53/bin/
[aaa@qq.com bin]# ./startup.sh
Using CATALINA_BASE:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_HOME:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_TMPDIR: /export/servers/apache-tomcat-8.0.53/temp
Using JRE_HOME:        /export/servers/jdk1.8.0_65/jre
Using CLASSPATH:       /export/servers/apache-tomcat-8.0.53/bin/bootstrap.jar:/export/servers/apache-tomcat-8.0.53/bin/tomcat-juli.jar
Tomcat started.

查看Tomcat进程

[aaa@qq.com bin]# jps
88057 Jps
88012 Bootstrap
[aaa@qq.com bin]# ps -ef|grep java
root      88012      1  2 08:23 pts/0    00:00:04 /export/servers/jdk1.8.0_65/jre/bin/java -Djava.util.logging.config.file=/export/servers/apache-tomcat-8.0.53/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /export/servers/apache-tomcat-8.0.53/bin/bootstrap.jar:/export/servers/apache-tomcat-8.0.53/bin/tomcat-juli.jar -Dcatalina.base=/export/servers/apache-tomcat-8.0.53 -Dcatalina.home=/export/servers/apache-tomcat-8.0.53 -Djava.io.tmpdir=/export/servers/apache-tomcat-8.0.53/temp org.apache.catalina.startup.Bootstrap start
root      88074  87966  0 08:26 pts/0    00:00:00 grep java
[aaa@qq.com bin]# 

浏览器访问Tomcat

  • 192.168.214.150为博主虚拟机ip
  • 8080为Tomcat默认端口,可配置
  • 访问的是Tomcat内置的ROOT项目
    大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署

可以看到tomcat正常运行

部署项目到Tomcat

使用SpringBoot构建的appdemo项目部署,构建过程详见:大型网站分布式架构(一)—— SpringBoot构建项目

1.修改pom文件
  • 改为war包
  • tomcat依赖
  • 注销热部署依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zaomianbao</groupId>
	<artifactId>appdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<!--<packaging>jar</packaging>-->

	<name>appdemo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2.打包项目

窗口右侧点击"Maven Projects"–>点击appdemo下lifecycle下的package即可
大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署
显示BUILD SUCCESS代表打包成功,打包成功后会在项目target目录下生成一个项目war包:appdemo-0.0.1-SNAPSHOT.war

3.将war包部署到Tomcat
首先关闭Tomcat
[aaa@qq.com ~]# cd /export/servers/apache-tomcat-8.0.53/bin/
[aaa@qq.com bin]# jps
88113 Jps
88012 Bootstrap
[aaa@qq.com bin]# ./shutdown.sh 
Using CATALINA_BASE:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_HOME:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_TMPDIR: /export/servers/apache-tomcat-8.0.53/temp
Using JRE_HOME:        /export/servers/jdk1.8.0_65/jre
Using CLASSPATH:       /export/servers/apache-tomcat-8.0.53/bin/bootstrap.jar:/export/servers/apache-tomcat-8.0.53/bin/tomcat-juli.jar
[aaa@qq.com bin]# jps
88150 Jps
[aaa@qq.com bin]# 
将项目war包上传至Tomcat的webapp目录下
[aaa@qq.com ~]# cd /export/servers/apache-tomcat-8.0.53/webapps/
[aaa@qq.com webapps]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring appdemo-0.0.1-SNAPSHOT.war...
  100%   17269 KB    17269 KB/sec    00:00:01       0 Errors  

[aaa@qq.com webapps]# ll
total 17292
-rw-r--r--.  1 root root 17684349 Aug 22  2018 appdemo-0.0.1-SNAPSHOT.war
drwxr-xr-x. 14 root root     4096 Aug 20 08:19 docs
drwxr-xr-x.  6 root root     4096 Aug 20 08:19 examples
drwxr-xr-x.  5 root root     4096 Aug 20 08:19 host-manager
drwxr-xr-x.  5 root root     4096 Aug 20 08:19 manager
drwxr-xr-x.  3 root root     4096 Aug 20 08:19 ROOT
[aaa@qq.com webapps]# 

大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署
这里使用的是lrzsz工具上传,详见:三种方式实现Linux的文件上传下载

启动Tomcat
[roaaa@qq.com webapps]# cd /export/servers/apache-tomcat-8.0.53/bin/
[aaa@qq.com bin]# ./startup.sh 
Using CATALINA_BASE:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_HOME:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_TMPDIR: /export/servers/apache-tomcat-8.0.53/temp
Using JRE_HOME:        /export/servers/jdk1.8.0_65/jre
Using CLASSPATH:       /export/servers/apache-tomcat-8.0.53/bin/bootstrap.jar:/export/servers/apache-tomcat-8.0.53/bin/tomcat-juli.jar
Tomcat started.
[aaa@qq.com bin]# cd ..
[aaa@qq.com apache-tomcat-8.0.53]# cd webapps/
[aaa@qq.com webapps]# ll
total 17296
drwxr-xr-x.  5 root root     4096 Aug 20 08:57 appdemo-0.0.1-SNAPSHOT
-rw-r--r--.  1 root root 17684349 Aug 22  2018 appdemo-0.0.1-SNAPSHOT.war
drwxr-xr-x. 14 root root     4096 Aug 20 08:19 docs
drwxr-xr-x.  6 root root     4096 Aug 20 08:19 examples
drwxr-xr-x.  5 root root     4096 Aug 20 08:19 host-manager
drwxr-xr-x.  5 root root     4096 Aug 20 08:19 manager
drwxr-xr-x.  3 root root     4096 Aug 20 08:19 ROOT
[aaa@qq.com webapps]# 
访问appdemo

大型网站分布式架构(二)—— Linux下Tomcat的安装和项目部署

  • appdemo-0.0.1-SNAPSHOT为appdemo的项目名,可以修改

至此,Tomcat作为Web服务器,appdemo为我们的web项目,一个单机版的web服务已经能够提供给用户访问,那么一个这样的Tomcat服务器能够承担多大的用户并发访问呢?下一篇将介绍一款web服务的压力测试软件,通过测试得到单机版Tomcat的访问极限。