spring boot tests the method of packaging deployment
- 2020-12-19 21:04:43
- OfStack
Many netizens ask me from time to time, how to test spring boot project, how to deploy it, is there any good deployment plan in production? This article will introduce 1 spring boot how to develop, debug, package and finally put into production and online.
The development phase
Unit testing
Unit testing is most important during the development phase, and springboot's support for unit testing is well established.
1. Add spring-ES16en-ES17en-ES18en package reference to pom package
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. Develop test classes
For the simplest example of helloworld, add the @RunWith (SpringRunner.class) and @SpringBootTest annotations to the header of the test class, add @Test to the top of the test method, and right-click on the method to run run.
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
In practice, dao layer code or service layer code can be injected for test verification according to the normal use of the project. spring-boot-starter-test provides a lot of basic usage, and more importantly, it adds support for Controller layer testing.
// Simply verify that the result set is correct
Assert.assertEquals(3, userMapper.getAll().size());
// Verify the result set, prompt
Assert.assertTrue(" Error, the correct return value is 200", status == 200);
Assert.assertFalse(" Error, the correct return value is 200", status != 200);
MockMvc has been introduced to support testing of the Controller layer. Simple examples are as follows:
public class HelloControlerTests {
private MockMvc mvc;
// Initialization execution
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
//验证controller Whether the response is normal and prints the return result
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
//验证controller Whether the response is normal and determine whether the returned result is correct
@Test
public void testHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
Unit testing is the first barrier to validating your code. Get in the habit of doing unit testing every time you write part of your code. Don't wait for full integration, which can make it easy to miss out on the underlying bug because you're more focused on the overall performance.
Integration testing
Overall development after entering the integration test, spring boot project start entrance in Application class, run directly run method can start the project, but we certainly need to constantly in the process of debugging to debug the code, if each modified one code will need to manually restart once service is very troublesome, spring boot nice hot deployment support are given, is very convenient in web program debugging use.
pom needs to add the following configuration:
<dependencies>
<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>
With the above configuration added, the project supports hot deployment, making integration testing easy.
Production of online
In fact, I think this stage, should be relatively simple 1 generally divided into two; One is packaged as an jar package and executed directly; the other is packaged as an war package and placed under the tomcat server.
Mingle jar package
If you are using maven to manage your project, execute the following command
cd Project with directory (and pom.xml At the same level)
mvn clean package
## Or execute the following command
## Package after excluding the test code
mvn clean package -Dmaven.test.skip=true
The jar package will be generated in the target directory after the package is finished. The name 1 is usually the project name + version number.jar
Start the jar package command
java -jar target/spring-boot-scheduler-1.0.0.jar
In this way, as soon as the console is closed, the service cannot be accessed. Let's start it by running in the background:
nohup java -jar target/spring-boot-scheduler-1.0.0.jar &
You can also choose to read different configuration files at startup
java -jar app.jar --spring.profiles.active=dev
You can also set the jvm parameter on startup
java -Xms10m -Xmx80m -jar app.jar &
gradle
If you are using gradle, use the following command to package
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
0
Mingle war package
There are two ways to do this: the first way is to export the war package through eclipse, and the second way is to use the command
1. maven project, modify pom package
will
<packaging>jar</packaging>
Instead of
<packaging>war</packaging>
2. tomcat is excluded when packing.
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
3
Set the scope attribute to provided here so that the JAR package will not be included in the resulting WAR, because servers such as Tomcat or Jetty will provide the associated API class at runtime.
3. Register the startup class
Create ES139en.java, inherit from SpringBootServletInitializer, override configure(), and register the startup class Application. When the external web application server builds Web Application Context, it adds the boot class.
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
4
The last execution
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
5
war file will be generated in target directory: project name + version number.war file, copy to tomcat server to start.
gradle
If gradle is used, the basic step is 1, build.gradle adds the support of war, excluding ES166en-ES167en-ES168en-ES169en:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
6
Use the build command again
gradle build
war will be generated under the build\libs directory.
Production operations
View the value of the JVM parameter
According to the jinfo command which comes with java:
jinfo -flags pid
To see what jar is using after startup, how much memory is gc, New generation, old generation batch, as shown below:
-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 : Maximum number of parallel compilations
-XX:InitialHeapSize and -XX:MaxHeapSize : specify JVM Of the initial and maximum heap memory sizes
-XX:MaxNewSize : JVM The maximum allocable size of the new generation of memory in the heap area
...
-XX:+UseParallelGC : Garbage collection for use Parallel The collector
How to restart
Simple and crude
Direct kill drop process starts jar package again
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void hello() {
System.out.println("hello world");
}
}
9
Of course, this method is more traditional and violent, so I suggest you use the following method to manage
Script execution
If you are using maven, you need to include the following configuration
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
If using gradle, you need to include the following configuration
springBoot {
executable = true
}
Startup mode:
1, can be directly./ yourapp. jar to start
2. Register as a service
You can also make a soft link to your jar package, add it to ES228en.d, and launch it with a command.
init. d example:
ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
chmod +x /etc/init.d/yourapp
This allows you to use the stop or restart command to manage your application.
/etc/init.d/yourapp start|stop|restart
or
service yourapp start|stop|restart
How to test, combine, package and put into production of springboot project has been introduced. In the future, we can find time to study the automatic operation and maintenance of springboot under 1, as well as the combined use of spring boot and docker.
Sample code -github
Example code - Code cloud
conclusion