Spring Boot,做爲Spring框架對「約定優先於配置(Convention Over Configuration)」理念的最佳實踐的產物,它能幫助咱們很快捷的建立出獨立運行、產品級別的基於Spring框架的應用,大部分Spring Boot應用只須要很是少的配置就能夠快速運行起來,是一個與微服務(MicroServices)至關契合的微框架。java
Spring Boot應用能夠打成jar包,其中內嵌tomcat,所以能夠直接啓動使用。可是在Spring Boot應用啓動以前,首先須要進行打包,本文講述的是Maven工程的打包,打包須要的前提條件(pom.xml文件中的內容)是:web
... <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> ...
打包命令爲:spring
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,運行,打包完成。tomcat
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)
下面主要有兩種方式進行Spring Boot的關閉:經過HTTP發送shutdown信號,或者經過service stop的方式。ps -ef(-aux) | grep project_name --> kill -9 XXX bash
Spring Boot應用關閉的前提條件是POM.xml添加如下內容:app
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.properties中添加:框架
#啓用shutdown endpoints.shutdown.enabled=true #禁用密碼驗證 endpoints.shutdown.sensitive=false
關閉命令爲:eclipse
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。curl
簡單關閉:ps -ef(-aux) | grep project_name --> kill -9 XXX
startTest.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
若是在關閉時須要安全驗證,則在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..."}