SpringBoot如何測試打包部署
Spring Boot 項目如何測試,如何部署,在生產(chǎn)中有什么好的部署方案嗎?這篇文章就來介紹一下 Spring Boot 如
何開發(fā)、調(diào)試、打包到最后的投產(chǎn)上線。
1、開發(fā)階段
1.1 單元測試
在開發(fā)階段的時候最重要的是單元測試了, Spring Boot 對單元測試的支持已經(jīng)很完善了。
1、在 pom 包中添加 spring-boot-starter-test
包引用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-package</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-package</name>
<description>spring-boot-package</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、編寫 controller
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
3、編寫配置文件
application.properties
server.error.path=D:/workspaces/tmp/error
server.port=8080
server.session-timeout=60
server.tomcat.max-threads=600
server.tomcat.uri-encoding=UTF-8
server.tomcat.basedir=D:/workspaces/tmp/log
logging.path=D:/workspaces/tmp/log
logging.file=myapp.log
application-dev.properties
info.app.name=spring-boot-test
info.app.version= 1.0.0
application-pro.properties
info.app.name=spring-boot-pro
info.app.version= 1.0.0
application-test.properties
info.app.name=spring-boot-uat
info.app.version= 1.0.0
1.2 開發(fā)測試類
以最簡單的 helloworld 為例,在測試類的類頭部需要添加:@RunWith(SpringRunner.class)
和
@SpringBootTest
注解,在測試方法的頂端添加@Test
即可,最后在方法上點(diǎn)擊右鍵run就可以運(yùn)行。
package com.example.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
實(shí)際使用中,可以按照項目的正常使用去注入數(shù)據(jù)層代碼或者是 Service 層代碼進(jìn)行測試驗(yàn)證,spring-boot-
starter-test
提供很多基礎(chǔ)用法,更難得的是增加了對 Controller 層測試的支持。
//簡單驗(yàn)證結(jié)果集是否正確
Assert.assertEquals(3, userMapper.getAll().size());
//驗(yàn)證結(jié)果集,提示
Assert.assertTrue("錯誤,正確的返回值為200", status == 200);
Assert.assertFalse("錯誤,正確的返回值為200", status != 200);
引入了MockMvc
支持了對 Controller 層的測試,簡單示例如下:
package com.example.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloWorldControlerTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
單元測試是驗(yàn)證你代碼第一道屏障,要養(yǎng)成每寫一部分代碼就進(jìn)行單元測試的習(xí)慣,不要等到全部集成后再進(jìn)行測
試,集成后因?yàn)楦P(guān)注整體運(yùn)行效果,很容易遺漏掉代碼底層的bug。
1.3 集成測試
整體開發(fā)完成之后進(jìn)入集成測試, Spring Boot 項目的啟動入口在 Application 類中,直接運(yùn)行 run 方法就可以啟
動項目,但是在調(diào)試的過程中我們肯定需要不斷的去調(diào)試代碼,如果每修改一次代碼就需要手動重啟一次服務(wù)就很
麻煩, Spring Boot 非常貼心的給出了熱部署的支持,很方便在 Web 項目中調(diào)試使用。
1、pom 添加以下的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2、在在IDE中設(shè)置 Settings→Build→Compiler中將Build project automatically勾選上
3、允許在程序運(yùn)行時進(jìn)行自動構(gòu)建,按 ctrl+shift+alt+/ 選擇registy,將
compiler.automake.allow.when.app.running 或者 compiler.automake.allow.parallel 勾選。
如果點(diǎn)擊進(jìn)去后,發(fā)現(xiàn)沒有 compiler.automake.allow.when.app.running,那么就需要我們?nèi)?/p>
Settings → Advanced Settings 進(jìn)行勾選:
Allow auto-make to start even if developed application is currently running
4、application.yml 文件添加配置
spring:
devtools:
restart:
enabled: true
# 設(shè)置不參與熱部署的文件或文件夾
exclude: static/**,public/**,config/application.yml
添加以上配置后,項目就支持了熱部署,非常方便集成測試。
2、投產(chǎn)上線
其實(shí)我覺得這個階段,應(yīng)該還是比較簡單一般分為兩種;一種是打包成 jar 包直接執(zhí)行,另一種是打包成 war 包放
到 tomcat 服務(wù)器下。
2.1 打成jar包
如果你使用的是 maven 來管理項目,執(zhí)行以下命令就可以:
$ cd 項目根目錄(和pom.xml同級)
$ mvn clean package
# 或者執(zhí)行下面的命令排除測試代碼后進(jìn)行打包
$ mvn clean package -Dmaven.test.skip=true
打包完成后 jar 包會生成到 target 目錄下,命名一般是 項目名+版本號.jar
啟動 jar 包命令
$ java -jar target/spring-boot-scheduler-1.0.0.jar
這種方式,只要控制臺關(guān)閉,服務(wù)就不能訪問了。下面我們使用在后臺運(yùn)行的方式來啟動:
$ nohup java -jar target/spring-boot-scheduler-1.0.0.jar &
也可以在啟動的時候選擇讀取不同的配置文件
$ java -jar app.jar --spring.profiles.active=dev
也可以在啟動的時候設(shè)置jvm參數(shù)
$ java -Xms10m -Xmx80m -jar app.jar &
如果使用的是 gradle,使用下面命令打包:
$ gradle build
$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar
2.2 打成war包
打成 war 包一般可以分兩種方式來實(shí)現(xiàn),第一種可以通過 eclipse 這種開發(fā)工具來導(dǎo)出 war 包,另外一種是使用
命令來完成,這里主要介紹后一種。
總的依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-package-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-package-war</name>
<description>spring-boot-package-war</description>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
1、maven 項目,修改 pom 包
將<packaging>jar</packaging>
改為 <packaging>war</packaging>
2、打包時排除tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
在這里將 scope 屬性設(shè)置為 provided,這樣在最終形成的 WAR 中不會包含這個 JAR 包,因?yàn)?Tomcat 或 Jetty 等
服務(wù)器在運(yùn)行時將會提供相關(guān)的 API 類。
3、注冊啟動類
創(chuàng)建 ServletInitializer.java,繼承 SpringBootServletInitializer ,覆蓋 configure(),把啟動類 Application 注冊進(jìn)
去。外部 Web 應(yīng)用服務(wù)器構(gòu)建 Web Application Context 的時候,會把啟動類添加進(jìn)去。
package com.example;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
控制器
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String index() {
return "Hello World xx";
}
}
最后執(zhí)行
$ mvn clean package -Dmaven.test.skip=true
會在 target 目錄下生成:項目名+版本號.war文件
,拷貝到 tomcat 服務(wù)器中啟動即可。
3、生產(chǎn)運(yùn)維
3.1 查看 JVM 參數(shù)的值
可以根據(jù) Java 自帶的 jinfo 命令:
$ jinfo -flags pid
來查看 jar 啟動后使用的是什么 gc、新生代、老年代分批的內(nèi)存都是多少,示例如下:
-XX:CICompilerCount=3 -XX:InitialHeapSize=234881024 -XX:MaxHeapSize=3743416320 -XX:MaxNewSize=1247805440 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=78118912 -XX:OldSize=156762112 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
-
-XX:CICompilerCount
:最大的并行編譯數(shù) -
-XX:InitialHeapSize
和-XX:MaxHeapSize
:指定 JVM 的初始和最大堆內(nèi)存大小 -
-XX:MaxNewSize
: JVM 堆區(qū)域新生代內(nèi)存的最大可分配大小 -
-XX:+UseParallelGC
:垃圾回收使用 Parallel 收集器
4、如何重啟
簡單粗暴
直接 kill 掉進(jìn)程再次啟動 jar 包
$ ps -ef | grep java
# 拿到對于Java程序的pid
$ kill -9 pid
# 再次重啟
$ java -jar xxxx.jar
當(dāng)然這種方式比較傳統(tǒng)和暴力,所以建議大家使用下面的方式來管理。
腳本執(zhí)行
如果使用的是maven,需要包含以下的配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
啟動方式:
1、 可以直接 ./yourapp.jar
來啟動
2、注冊為服務(wù),做一個軟鏈接指向你的jar包并加入到init.d
中,然后用命令來啟動。
init.d
例子:
$ ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
$ chmod +x /etc/init.d/yourapp
這樣就可以使用stop
或者是restart
命令去管理你的應(yīng)用。
$ /etc/init.d/yourapp start|stop|restart
或者
$ service yourapp start|stop|restart
到此 Spring Boot 項目如何測試、聯(lián)調(diào)和打包投產(chǎn)均已經(jīng)介紹完,以后可以找時間研究一下 Spring Boot 的自動化文章來源:http://www.zghlxwxcb.cn/news/detail-821017.html
運(yùn)維,以及 Spring Boot 和 Docker 相結(jié)合的使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-821017.html
到了這里,關(guān)于SpringBoot如何測試打包部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!