Spring Boot的应用启动与关闭的方法
SpringBoot,作为Spring框架对“约定优先于配置(ConventionOverConfiguration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分SpringBoot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。
1.SpringBoot应用打包
SpringBoot应用可以打成jar包,其中内嵌tomcat,因此可以直接启动使用。但是在SpringBoot应用启动之前,首先需要进行打包,本文讲述的是Maven工程的打包,打包需要的前提条件(pom.xml文件中的内容)是:
...jar ...... org.springframework.boot spring-boot-starter-web ... org.springframework.boot spring-boot-maven-plugin com.***.Application repackage
打包命令为:
mvncleanpackage-Dmaven.test.skip=true #Demo $mvncleanpackage-Dmaven.test.skip=true [INFO]Scanningforprojects... [WARNING] [WARNING]Someproblemswereencounteredwhilebuildingtheeffectivemodelforcom.example:myproject:jar:0.0.1-SNAPSHOT [WARNING]'build.plugins.plugin.version'fororg.springframework.boot:spring-boot-maven-pluginismissing.@line38,column17 [WARNING] [WARNING]Itishighlyrecommendedtofixtheseproblemsbecausetheythreatenthestabilityofyourbuild. [WARNING] [WARNING]Forthisreason,futureMavenversionsmightnolongersupportbuildingsuchmalformedprojects. [WARNING] [INFO] [INFO]------------------------------------------------------------------------ [INFO]Buildingmyproject0.0.1-SNAPSHOT [INFO]------------------------------------------------------------------------ [INFO] [INFO]---maven-clean-plugin:2.5:clean(default-clean)@myproject--- [INFO]Deleting/Users/ltc/SpringBootDemo/target [INFO] [INFO]---maven-resources-plugin:2.6:resources(default-resources)@myproject--- [WARNING]Usingplatformencoding(UTF-8actually)tocopyfilteredresources,i.e.buildisplatformdependent! [INFO]Copying1resource [INFO] [INFO]---maven-compiler-plugin:3.1:compile(default-compile)@myproject--- [INFO]Changesdetected-recompilingthemodule! [WARNING]Fileencodinghasnotbeenset,usingplatformencodingUTF-8,i.e.buildisplatformdependent! [INFO]Compiling1sourcefileto/Users/ltc/SpringBootDemo/target/classes [INFO] [INFO]---maven-resources-plugin:2.6:testResources(default-testResources)@myproject--- [INFO]Notcopyingtestresources [INFO] [INFO]---maven-compiler-plugin:3.1:testCompile(default-testCompile)@myproject--- [INFO]Notcompilingtestsources [INFO] [INFO]---maven-surefire-plugin:2.12.4:test(default-test)@myproject--- [INFO]Testsareskipped. [INFO] [INFO]---maven-jar-plugin:2.4:jar(default-jar)@myproject--- [INFO]Buildingjar:/Users/ltc/SpringBootDemo/target/myproject-0.0.1-SNAPSHOT.jar [INFO] [INFO]---spring-boot-maven-plugin:1.5.0.RC1:repackage(default)@myproject--- [INFO]------------------------------------------------------------------------ [INFO]BUILDSUCCESS [INFO]------------------------------------------------------------------------ [INFO]Totaltime:1.861s [INFO]Finishedat:2017-01-13T15:31:32+08:00 [INFO]FinalMemory:26M/308M [INFO]------------------------------------------------------------------------
或在eclipse中运行run->Mavenbuild...,在Goals中填写cleanpackage-Dmaven.test.skip=true,运行,打包完成。
2.SpringBoot应用启动
SpringBoot的启动命令为:
java-jarapplication.jar #Demo $java-jartarget/myproject-0.0.1-SNAPSHOT.jar ._________ /\\/___'_____(_)______\\\\ (()\___|'_|'_||'_\/_`|\\\\ \\/___)||_)|||||||(_||)))) '|____|.__|_||_|_||_\__,|//// =========|_|==============|___/=/_/_/_/ ::SpringBoot::(v1.4.3.RELEASE) 2017-01-1315:31:36.911INFO62119---[main]com.test.Example:StartingExampleonlocalwithPID62119(/Users/ltc/SpringBootDemo/target/myproject-0.0.1-SNAPSHOT.jarstartedbyliutianchiin/Users/ltc/SpringBootDemo) 2017-01-1315:31:36.916INFO62119---[main]com.test.Example:Noactiveprofileset,fallingbacktodefaultprofiles:default 2017-01-1315:31:36.981INFO62119---[main]ationConfigEmbeddedWebApplicationContext:Refreshingorg.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3:startupdate[FriJan1315:31:36CST2017];rootofcontexthierarchy 2017-01-1315:31:38.602INFO62119---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcatinitializedwithport(s):8080(http) 2017-01-1315:31:38.615INFO62119---[main]o.apache.catalina.core.StandardService:StartingserviceTomcat 2017-01-1315:31:38.616INFO62119---[main]org.apache.catalina.core.StandardEngine:StartingServletEngine:ApacheTomcat/8.5.6 2017-01-1315:31:38.718INFO62119---[ost-startStop-1]o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringembeddedWebApplicationContext 2017-01-1315:31:38.718INFO62119---[ost-startStop-1]o.s.web.context.ContextLoader:RootWebApplicationContext:initializationcompletedin1740ms 2017-01-1315:31:38.927INFO62119---[ost-startStop-1]o.s.b.w.servlet.ServletRegistrationBean:Mappingservlet:'dispatcherServlet'to[/] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'metricsFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'characterEncodingFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'hiddenHttpMethodFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'httpPutFormContentFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'requestContextFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'webRequestLoggingFilter'to:[/*] 2017-01-1315:31:38.932INFO62119---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'applicationContextIdFilter'to:[/*] 2017-01-1315:31:39.217INFO62119---[main]s.w.s.m.m.a.RequestMappingHandlerAdapter:Lookingfor@ControllerAdvice:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3:startupdate[FriJan1315:31:36CST2017];rootofcontexthierarchy 2017-01-1315:31:39.310INFO62119---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/]}"ontojava.lang.Stringcom.test.Example.home() 2017-01-1315:31:39.313INFO62119---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error]}"ontopublicorg.springframework.http.ResponseEntity>org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-01-1315:31:39.313INFO62119---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error],produces=[text/html]}"ontopublicorg.springframework.web.servlet.ModelAndVieworg.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-01-1315:31:39.338INFO62119---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/webjars/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-01-1315:31:39.338INFO62119---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-01-1315:31:39.378INFO62119---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**/favicon.ico]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-01-1315:31:39.665INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/metrics/{name:.*}],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String) 2017-01-1315:31:39.665INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/metrics||/manage/metrics.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.666INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/mappings||/manage/mappings.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.667INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/trace||/manage/trace.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.667INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/info||/manage/info.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.668INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/configprops||/manage/configprops.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.669INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/heapdump||/manage/heapdump.json],methods=[GET],produces=[application/octet-stream]}"ontopublicvoidorg.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)throwsjava.io.IOException,javax.servlet.ServletException 2017-01-1315:31:39.669INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/autoconfig||/manage/autoconfig.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.673INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/env/{name:.*}],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String) 2017-01-1315:31:39.673INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/env||/manage/env.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.674INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/health||/manage/health.json],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal) 2017-01-1315:31:39.675INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/dump||/manage/dump.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.677INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/shutdown||/manage/shutdown.json],methods=[POST]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint.invoke() 2017-01-1315:31:39.678INFO62119---[main]o.s.b.a.e.mvc.EndpointHandlerMapping:Mapped"{[/manage/beans||/manage/beans.json],methods=[GET],produces=[application/json]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-01-1315:31:39.799INFO62119---[main]o.s.j.e.a.AnnotationMBeanExporter:RegisteringbeansforJMXexposureonstartup 2017-01-1315:31:39.809INFO62119---[main]o.s.c.support.DefaultLifecycleProcessor:Startingbeansinphase0 2017-01-1315:31:39.944INFO62119---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcatstartedonport(s):8080(http) 2017-01-1315:31:39.949INFO62119---[main]com.test.Example:StartedExamplein4.292seconds(JVMrunningfor4.726)
3.SpringBoot应用关闭
下面主要有两种方式进行SpringBoot的关闭:通过HTTP发送shutdown信号,或者通过servicestop的方式。
SpringBoot应用关闭的前提条件是POM.xml添加以下内容:
org.springframework.boot spring-boot-starter-actuator
application.properties中添加:
#启用shutdown endpoints.shutdown.enabled=true #禁用密码验证 endpoints.shutdown.sensitive=false
关闭命令为:
curl-XPOSThost:port/shutdown #Demo $curl-XPOSThttp://localhost:8080/shutdown {"message":"Shuttingdown,bye..."} $curl-XPOSThttp://localhost:8080/manage/shutdown {"message":"Shuttingdown,bye..."}
如果要配置路径,需要在application.properties中添加management.context-path=/manage,则关闭命令变为curl-XPOSThost:port/manage/shutdown。
4.安全验证
如果在关闭时需要安全验证,则在pom.xml文件中添加:
org.springframework.boot spring-boot-starter-security
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-uadmin:admin-XPOSThttp://127.0.0.1:8081/manage/shutdown #Demo $curl-uadmin:admin-XPOSThttp://127.0.0.1:8081/manage/shutdown {"message":"Shuttingdown,bye..."}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。