Spring Boot的应用启动与关闭的方法
spring boot,作为spring框架对“约定优先于配置(convention over configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于spring框架的应用,大部分spring boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(microservices)相当契合的微框架。
1. spring boot应用打包
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应用启动
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:36.911 info 62119 --- [ main] com.test.example : starting example on local with pid 62119 (/users/ltc/spring boot demo/target/myproject-0.0.1-snapshot.jar started by liutianchi in /users/ltc/spring boot demo) 2017-01-13 15:31:36.916 info 62119 --- [ main] com.test.example : no active profile set, falling back to default profiles: default 2017-01-13 15:31:36.981 info 62119 --- [ main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@b1a58a3: startup date [fri jan 13 15:31:36 cst 2017]; root of context hierarchy 2017-01-13 15:31:38.602 info 62119 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized with port(s): 8080 (http) 2017-01-13 15:31:38.615 info 62119 --- [ main] o.apache.catalina.core.standardservice : starting service tomcat 2017-01-13 15:31:38.616 info 62119 --- [ main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/8.5.6 2017-01-13 15:31:38.718 info 62119 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2017-01-13 15:31:38.718 info 62119 --- [ost-startstop-1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 1740 ms 2017-01-13 15:31:38.927 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'metricsfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'webrequestloggingfilter' to: [/*] 2017-01-13 15:31:38.932 info 62119 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'applicationcontextidfilter' to: [/*] 2017-01-13 15:31:39.217 info 62119 --- [ main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@b1a58a3: startup date [fri jan 13 15:31:36 cst 2017]; root of context hierarchy 2017-01-13 15:31:39.310 info 62119 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/]}" onto java.lang.string com.test.example.home() 2017-01-13 15:31:39.313 info 62119 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest) 2017-01-13 15:31:39.313 info 62119 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2017-01-13 15:31:39.338 info 62119 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-01-13 15:31:39.338 info 62119 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-01-13 15:31:39.378 info 62119 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-01-13 15:31:39.665 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/metrics/{name:.*}],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.metricsmvcendpoint.value(java.lang.string) 2017-01-13 15:31:39.665 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/metrics || /manage/metrics.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.666 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/mappings || /manage/mappings.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.667 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/trace || /manage/trace.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.667 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/info || /manage/info.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.668 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/configprops || /manage/configprops.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.669 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/heapdump || /manage/heapdump.json],methods=[get],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.heapdumpmvcendpoint.invoke(boolean,javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) throws java.io.ioexception,javax.servlet.servletexception 2017-01-13 15:31:39.669 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/autoconfig || /manage/autoconfig.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.673 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/env/{name:.*}],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.environmentmvcendpoint.value(java.lang.string) 2017-01-13 15:31:39.673 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/env || /manage/env.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.674 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/health || /manage/health.json],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.healthmvcendpoint.invoke(java.security.principal) 2017-01-13 15:31:39.675 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/dump || /manage/dump.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.677 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/shutdown || /manage/shutdown.json],methods=[post]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.shutdownmvcendpoint.invoke() 2017-01-13 15:31:39.678 info 62119 --- [ main] o.s.b.a.e.mvc.endpointhandlermapping : mapped "{[/manage/beans || /manage/beans.json],methods=[get],produces=[application/json]}" onto public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke() 2017-01-13 15:31:39.799 info 62119 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2017-01-13 15:31:39.809 info 62119 --- [ main] o.s.c.support.defaultlifecycleprocessor : starting beans in phase 0 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应用关闭
下面主要有两种方式进行spring boot的关闭:通过http发送shutdown信号,或者通过service stop的方式。
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。
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..."}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java编程实现验证哥德巴赫猜想
推荐阅读
-
Spring Boot的应用启动与关闭的方法
-
spring boot整合Cucumber(BDD)的方法
-
Spring boot redis cache的key的使用方法
-
Spring Boot利用@Async异步调用:ThreadPoolTaskScheduler线程池的优雅关闭详解
-
Spring Boot+maven打war包的方法
-
Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法
-
Spring Boot 与 Kotlin 上传文件的示例代码
-
Spring Boot与Kotlin处理Web表单提交的方法
-
Spring Boot 中application.yml与bootstrap.yml的区别
-
Spring Boot修改启动端口的方法