Spring Boot项目的部署运行

Spring Boot项目的部署和运行

Spring Boot项目在开发完成之后的部署和命令行运行过程如下。其中部署过程又可以使用内嵌Tomcat或外部Tomcat等其他服务器进行。

IDEA中编译并打包Spring Boot项目

在使用IDEA创建Spring Boot项目时,选择的是war的打包方式,也就是在pom.xml文件中的如下内容:

<packaging>war</packaging>

如果需要jar包的打包方式,则将这里改为jar即可,只不过jar包的方式无法运行WEB应用。

然后使用maven install来编译并生成war包,可以右击pom.xml按照如下的方式进行运行:

也可以通过命令行进行打包,进入项目根目录,运行如下命令:

mvn package

这样就可以得到target目录下生成的war:

使用Spring Boot内嵌的Tomcat服务器运行项目

在war包所在的目录,运行如下java命令,既可以使用spring boot中内嵌的tomcat服务器来启动并运行该项目:

Yitian-MacBook-Pro:springboot yitian$ java -jar springboot-learning-0.0.1-SNAPSHOT.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-08 10:11:19.119  INFO 38505 --- [           main] c.z.s.SpringbootLearningApplication      : Starting SpringbootLearningApplication v0.0.1-SNAPSHOT on Yitian-MacBook-Pro.local with PID 38505 (/Users/yitian/Documents/JavaTechStack/springboot/springboot-learning-0.0.1-SNAPSHOT.war started by yitian in /Users/yitian/Documents/JavaTechStack/springboot)
2020-02-08 10:11:19.121  INFO 38505 --- [           main] c.z.s.SpringbootLearningApplication      : No active profile set, falling back to default profiles: default
2020-02-08 10:11:19.894  INFO 38505 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'asyncConfiguration' of type [cn.zyt.springbootlearning.config.AsyncConfiguration$$EnhancerBySpringCGLIB$$ef7c2113] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-02-08 10:11:20.223  INFO 38505 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-02-08 10:11:20.236  INFO 38505 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-02-08 10:11:20.236  INFO 38505 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-08 10:11:21.223  INFO 38505 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-02-08 10:11:21.486  INFO 38505 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-02-08 10:11:21.486  INFO 38505 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2319 ms
--------------------------------
com.zaxxer.hikari.HikariDataSource
--------------------------------
2020-02-08 10:11:21.940  INFO 38505 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-08 10:11:22.010  INFO 38505 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2020-02-08 10:11:22.069  INFO 38505 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-02-08 10:11:22.128  INFO 38505 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-08 10:11:22.130  INFO 38505 --- [           main] c.z.s.SpringbootLearningApplication      : Started SpringbootLearningApplication in 3.549 seconds (JVM running for 3.985)
-----------------------------
Application start done!
-----------------------------

如果需要停止运行使用,使用control+c即可停止服务。

使用外部的服务器运行Spring Boot项目

在创建Spring Boot项目是如果选择使用war打包方式,那么项目中会默认创建一个SerlvetInitializer.java,它的作用为初始化Servlet,将Spring Boot的相应启动类XXXApplication.java进行载入,依靠这个启动类既可以来构建Web容器,加载相应的配置,然后启动项目

所以如果使用外部的服务器运行Spring Boot项目,只需要将上述获得的war复制到外部服务器Tomcat的webapps目录下既可完成部署,然后使用上述的启动命令既可以启动该项目。

命令行启动服务时指定相关参数

在部署完以后使用命令行启动服务时,可以指定相关的运行参数,例如如下指定了项目运行的端口:

java -jar springboot-learning-0.0.1-SNAPSHOT.war --server.port=9080

这样就可以使用9080端口运行项目。如果配置文件中有相同配置项的设置,命令行中的参数会覆盖配置文件中的设置。

发布了296 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/yitian_z/article/details/104219416