SpringBoot如何测试打包部署

SpringBoot如何测试打包部署

Spring Boot 项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下 Spring Boot 如

何开发、调试、打包到最后的投产上线。

1、开发阶段

1.1 单元测试

在开发阶段的时候最重要的是单元测试了, Spring Boot 对单元测试的支持已经很完善了。

1、在 pom 包中添加 spring-boot-starter-test 包引用

xml 复制代码
<?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

java 复制代码
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

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

properties 复制代码
info.app.name=spring-boot-test
info.app.version= 1.0.0

application-pro.properties

properties 复制代码
info.app.name=spring-boot-pro
info.app.version= 1.0.0

application-test.properties

properties 复制代码
info.app.name=spring-boot-uat
info.app.version= 1.0.0

1.2 开发测试类

以最简单的 helloworld 为例,在测试类的类头部需要添加:@RunWith(SpringRunner.class)

@SpringBootTest注解,在测试方法的顶端添加@Test即可,最后在方法上点击右键run就可以运行。

java 复制代码
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")));
    }

}

实际使用中,可以按照项目的正常使用去注入数据层代码或者是 Service 层代码进行测试验证,spring-boot-

starter-test 提供很多基础用法,更难得的是增加了对 Controller 层测试的支持。

java 复制代码
//简单验证结果集是否正确
Assert.assertEquals(3, userMapper.getAll().size());
//验证结果集,提示
Assert.assertTrue("错误,正确的返回值为200", status == 200); 
Assert.assertFalse("错误,正确的返回值为200", status != 200);  

引入了MockMvc支持了对 Controller 层的测试,简单示例如下:

java 复制代码
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();
    }

}

单元测试是验证你代码第一道屏障,要养成每写一部分代码就进行单元测试的习惯,不要等到全部集成后再进行测

试,集成后因为更关注整体运行效果,很容易遗漏掉代码底层的bug。

1.3 集成测试

整体开发完成之后进入集成测试, Spring Boot 项目的启动入口在 Application 类中,直接运行 run 方法就可以启

动项目,但是在调试的过程中我们肯定需要不断的去调试代码,如果每修改一次代码就需要手动重启一次服务就很

麻烦, Spring Boot 非常贴心的给出了热部署的支持,很方便在 Web 项目中调试使用。

1、pom 添加以下的配置

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

2、在在IDE中设置 Settings→Build→Compiler中将Build project automatically勾选上

3、允许在程序运行时进行自动构建,按 ctrl+shift+alt+/ 选择registy,将

compiler.automake.allow.when.app.running 或者 compiler.automake.allow.parallel 勾选。

如果点击进去后,发现没有 compiler.automake.allow.when.app.running,那么就需要我们去

Settings → Advanced Settings 进行勾选:

Allow auto-make to start even if developed application is currently running

4、application.yml 文件添加配置

yaml 复制代码
spring:
  devtools:
    restart:
      enabled: true
      # 设置不参与热部署的文件或文件夹
      exclude: static/**,public/**,config/application.yml

添加以上配置后,项目就支持了热部署,非常方便集成测试。

2、投产上线

其实我觉得这个阶段,应该还是比较简单一般分为两种;一种是打包成 jar 包直接执行,另一种是打包成 war 包放

到 tomcat 服务器下。

2.1 打成jar包

如果你使用的是 maven 来管理项目,执行以下命令就可以:

shell 复制代码
$ cd 项目根目录(和pom.xml同级)
$ mvn clean package
# 或者执行下面的命令排除测试代码后进行打包
$ mvn clean package -Dmaven.test.skip=true

打包完成后 jar 包会生成到 target 目录下,命名一般是 项目名+版本号.jar

启动 jar 包命令

shell 复制代码
$ java -jar  target/spring-boot-scheduler-1.0.0.jar

这种方式,只要控制台关闭,服务就不能访问了。下面我们使用在后台运行的方式来启动:

shell 复制代码
$ nohup java -jar target/spring-boot-scheduler-1.0.0.jar &

也可以在启动的时候选择读取不同的配置文件

shell 复制代码
$ java -jar app.jar --spring.profiles.active=dev

也可以在启动的时候设置jvm参数

shell 复制代码
$ java -Xms10m -Xmx80m -jar app.jar &

如果使用的是 gradle,使用下面命令打包:

shell 复制代码
$ gradle build
$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar

2.2 打成war包

打成 war 包一般可以分两种方式来实现,第一种可以通过 eclipse 这种开发工具来导出 war 包,另外一种是使用

命令来完成,这里主要介绍后一种。

总的依赖:

xml 复制代码
<?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

xml 复制代码
<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 属性设置为 provided,这样在最终形成的 WAR 中不会包含这个 JAR 包,因为 Tomcat 或 Jetty 等

服务器在运行时将会提供相关的 API 类。

3、注册启动类

创建 ServletInitializer.java,继承 SpringBootServletInitializer ,覆盖 configure(),把启动类 Application 注册进

去。外部 Web 应用服务器构建 Web Application Context 的时候,会把启动类添加进去。

java 复制代码
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);
    }
}
java 复制代码
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);
	}

}

控制器

java 复制代码
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";
    }
}

最后执行

shell 复制代码
$ mvn clean package  -Dmaven.test.skip=true

会在 target 目录下生成:项目名+版本号.war文件,拷贝到 tomcat 服务器中启动即可。

3、生产运维

3.1 查看 JVM 参数的值

可以根据 Java 自带的 jinfo 命令:

shell 复制代码
$ jinfo -flags pid

来查看 jar 启动后使用的是什么 gc、新生代、老年代分批的内存都是多少,示例如下:

shell 复制代码
-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 :最大的并行编译数

  • -XX:InitialHeapSize-XX:MaxHeapSize :指定 JVM 的初始和最大堆内存大小

  • -XX:MaxNewSize : JVM 堆区域新生代内存的最大可分配大小

  • -XX:+UseParallelGC :垃圾回收使用 Parallel 收集器

4、如何重启

简单粗暴

直接 kill 掉进程再次启动 jar 包

shell 复制代码
$ ps -ef | grep java 
# 拿到对于Java程序的pid
$ kill -9 pid
# 再次重启
$ java -jar xxxx.jar

当然这种方式比较传统和暴力,所以建议大家使用下面的方式来管理。

脚本执行

如果使用的是maven,需要包含以下的配置

xml 复制代码
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

启动方式:

1、 可以直接 ./yourapp.jar 来启动

2、注册为服务,做一个软链接指向你的jar包并加入到init.d中,然后用命令来启动。

init.d 例子:

shell 复制代码
$ ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
$ chmod +x /etc/init.d/yourapp

这样就可以使用stop或者是restart命令去管理你的应用。

shell 复制代码
$ /etc/init.d/yourapp start|stop|restart

或者

shell 复制代码
$ service yourapp start|stop|restart

到此 Spring Boot 项目如何测试、联调和打包投产均已经介绍完,以后可以找时间研究一下 Spring Boot 的自动化

运维,以及 Spring Boot 和 Docker 相结合的使用。

相关推荐
柊二三1 小时前
spring boot开发中的资源处理等问题
java·spring boot·后端
一枚小小程序员哈1 小时前
基于springboot的宠物商城设计与实现
java·spring boot·spring·eclipse·tomcat·maven·宠物
风与尘3 小时前
RabbitMQ延时队列的两种实现方式
spring boot·分布式·中间件·rabbitmq
_码农121384 小时前
简单spring boot项目,之前练习的,现在好像没有达到效果
java·spring boot·后端
巴拉巴巴巴拉4 小时前
Spring Boot 整合 Web 开发全攻略
spring boot
写代码的比利7 小时前
Spring 调试终于不再痛苦了
spring boot·spring·intellij idea
一只爱撸猫的程序猿8 小时前
构建一个简单的亿级数据迁移方案案例
spring boot·数据分析·ai编程
风象南9 小时前
告别YAML,在SpringBoot中用数据库配置替代配置文件
spring boot·后端
枣伊吕波9 小时前
十一、请求响应-请求:简单参数和实体参数(简单实体参数与复杂实体参数)
java·spring boot·后端
白白白鲤鱼9 小时前
Vue2项目—基于路由守卫实现钉钉小程序动态更新标题
服务器·前端·spring boot·后端·职场和发展·小程序·钉钉