SpringBoot如何测试打包部署

SpringBoot如何测试打包部署

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

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

1、开发阶段

1.1 单元测试

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

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


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.0.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-boot-packageartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-packagename>
    <description>spring-boot-packagedescription>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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 开发测试类

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

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

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 层测试的支持。

//简单验证结果集是否正确
Assert.assertEquals(3, userMapper.getAll().size());
//验证结果集,提示
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();
    }

}

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

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

1.3 集成测试

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

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

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

1、pom 添加以下的配置

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-devtoolsartifactId>
    <optional>trueoptional>
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 文件添加配置

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

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

2、投产上线

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

到 tomcat 服务器下。

2.1 打成jar包

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

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

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

启动 jar 包命令

$ java -jar  target/spring-boot-scheduler-1.0.0.jar

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

$ nohup java -jar target/spring-boot-scheduler-1.0.0.jar &

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

$ java -jar app.jar --spring.profiles.active=dev

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

$ java -Xms10m -Xmx80m -jar app.jar &

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

$ gradle build
$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar

2.2 打成war包

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

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

总的依赖:


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.6.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-boot-package-warartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-package-warname>
    <description>spring-boot-package-wardescription>
    <packaging>warpackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>
                configuration>
            plugin>
        plugins>
    build>

project>

1、maven 项目,修改 pom 包

jar改为 war

2、打包时排除tomcat

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
	
	<exclusions>
		<exclusion>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-tomcatartifactId>
		exclusion>
	exclusions>
dependency>

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-tomcatartifactId>
	<scope>providedscope>
dependency>

在这里将 scope 属性设置为 provided,这样在最终形成的 WAR 中不会包含这个 JAR 包,因为 Tomcat 或 Jetty 等

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

3、注册启动类

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

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

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";
    }
}

最后执行

$ mvn clean package  -Dmaven.test.skip=true

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

3、生产运维

3.1 查看 JVM 参数的值

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

$ jinfo -flags pid

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

-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 包

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

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

脚本执行

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

<plugin>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-maven-pluginartifactId>
    <configuration>
        <executable>trueexecutable>
    configuration>
plugin>

启动方式:

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

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

init.d 例子:

$ ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
$ chmod +x /etc/init.d/yourapp

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

$ /etc/init.d/yourapp start|stop|restart

或者

$ service yourapp start|stop|restart

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

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

你可能感兴趣的:(spring,boot,spring,boot)