Spring Boot的应用启动与关闭的方法
2018-02-24 00:00
645 查看
摘要: spring boot
Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。
打包命令为:
或在eclipse中运行run -> Maven build...,在Goals中填写clean package -Dmaven.test.skip=true,运行,打包完成。
application.properties中添加:
关闭命令为:
如果要配置路径,需要在application.properties中添加management.context-path=/manage,则关闭命令变为curl -X POST host:port/manage/shutdown。
application.properties中添加:
关闭命令为:
Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。
1.应用打包
Spring Boot应用可以打成jar包,其中内嵌tomcat,因此可以直接启动使用。但是在Spring Boot应用启动之前,首先需要进行打包,本文讲述的是Maven工程的打包,打包需要的前提条件(pom.xml文件中的内容)是:... <packaging>jar</packaging> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ... <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.***.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ...
打包命令为:
mvn clean package -Dmaven.test.skip=true # Demo $ mvn clean package -Dmaven.test.skip=true [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.example:myproject:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 38, column 17 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject --- [INFO] Deleting /Users/ltc/Spring Boot Demo/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myproject --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /Users/ltc/Spring Boot Demo/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myproject --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myproject --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myproject --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject --- [INFO] Building jar: /Users/ltc/Spring Boot Demo/target/myproject-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.0.RC1:repackage (default) @ myproject --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.861 s [INFO] Finished at: 2017-01-13T15:31:32+08:00 [INFO] Final Memory: 26M/308M [INFO] ------------------------------------------------------------------------
或在eclipse中运行run -> Maven build...,在Goals中填写clean package -Dmaven.test.skip=true,运行,打包完成。
2.应用启动
Spring Boot的启动命令为:java -jar application.jar # Demo $ java -jar target/myproject-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.3.RELEASE) ............ 2017-01-13 15:31:39.944 INFO 62119 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-01-13 15:31:39.949 INFO 62119 --- [ main] com.test.Example : Started Example in 4.292 seconds (JVM running for 4.726)
3.应用关闭
下面主要有两种方式进行Spring Boot的关闭:通过HTTP发送shutdown信号,或者通过service stop的方式。ps -ef(-aux) | grep project_name --> kill -9 XXX方式一:HTTP发送shutdown信号
Spring Boot应用关闭的前提条件是POM.xml添加以下内容:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.properties中添加:
#启用shutdown endpoints.shutdown.enabled=true #禁用密码验证 endpoints.shutdown.sensitive=false
关闭命令为:
curl -X POST host:port/shutdown # Demo $ curl -X POST http://localhost:8080/shutdown {"message":"Shutting down, bye..."} $ curl -X POST http://localhost:8080/manage/shutdown {"message":"Shutting down, bye..."}
如果要配置路径,需要在application.properties中添加management.context-path=/manage,则关闭命令变为curl -X POST host:port/manage/shutdown。
方式二:服务方式
简单关闭:ps -ef(-aux) | grep project_name --> kill -9 XXXstartTest.sh内容如下: #!/bin/sh java -jar Test.jar & #注意:必须有&让其后台执行,否则没有pid生成 echo $! > java.pid #将jar包启动对应的pid写入文件中,为停止时提供pid stopTest.sh内容如下: #!/bin/sh PID=$(cat java.pid ) kill -9 $PID
4.安全验证
如果在关闭时需要安全验证,则在pom.xml文件中添加:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
application.properties中添加:
#开启shutdown的安全验证 endpoints.shutdown.sensitive=true #验证用户名 security.user.name=admin #验证密码 security.user.password=admin #角色 management.security.role=SUPERUSER # 指定端口 management.port=8081 # 指定地址 management.address=127.0.0.1
关闭命令为:
curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown # Demo $ curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown {"message":"Shutting down, bye..."}
相关文章推荐
- Spring Boot的应用启动与关闭的方法
- Spring Boot的应用正常启动与关闭
- spring boot应用启动及关闭方式
- Spring Boot的应用启动与关闭
- Spring Boot使用方法小札(3):应用启动后做一些事
- 8、Spring boot中容器启动前后回调的方法
- SpringBoot应用启动流程
- 【深入SpringBoot 1.3.5 第一章】Boot应用的启动流程
- spring应用启动加载自定义类方法的几种方式
- 13.5 SpringBoot应用启动错误org.apache.catalina.LifecycleException
- Spring Boot应用启动原理分析(转)
- 在SpringBoot应用中获取应用上下文方法
- spring boot应用启动原理分析
- spring boot应用启动原理分析
- spring boot启动时加载外部配置文件的方法
- Intellij IDEA实现SpringBoot项目多端口启动的两种方法
- spring boot应用启动原理分析
- springboot配置sharding-jdbc,启动时出现resultset以关闭
- SpringBoot应用部署到Tomcat中无法启动问题
- spring boot应用启动原理分析