Spring Boot (XII): How to Spring Boot test, and package deployment

 

There are a lot of friends will ask me from time to time, how Spring Boot project testing, how to deploy, what good deployment in production yet? This article will introduce how Spring Boot developing, debugging, packaging production to the final on the line.

development stage

unit test

The most important time is in the development phase of the unit test, Spring Boot support for unit tests are well established.

1, the package added pom package referencesspring-boot-starter-test

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-test</artifactId>

  4. <scope>test</scope>

  5. </dependency>

2. Test -

In its simplest helloworld example, you need to add the class to test the head of the class: and annotations, at the top of the test method is added to the last click on the right run can run on the method.@RunWith(SpringRunner.class)@SpringBootTest@Test

  1. @RunWith(SpringRunner.class)

  2. @SpringBootTest

  3. public class ApplicationTests {

  4.  

  5. @Test

  6. public void hello() {

  7. System.out.println("hello world");

  8. }

  9.  

  10. }

Actual use, in accordance with the normal use of the project to inject code or data layer is a layer of code Service test validation, providing many basic usage, even more unusual is the addition of support for the Controller layer testing.spring-boot-starter-test

  1. //简单验证结果集是否正确

  2. Assert.assertEquals(3, userMapper.getAll().size());

  3.  

  4. //验证结果集,提示

  5. Assert.assertTrue("错误,正确的返回值为200", status == 200);

  6. Assert.assertFalse("错误,正确的返回值为200", status != 200);

Introducing a MockMvcsupport layer was tested for the Controller, the following simple example:

  1. public class HelloControlerTests {

  2.  

  3. private MockMvc mvc;

  4.  

  5. //初始化执行

  6. @Before

  7. public void setUp() throws Exception {

  8. mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();

  9. }

  10.  

  11. //验证controller是否正常响应并打印返回结果

  12. @Test

  13. public void getHello() throws Exception {

  14. mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))

  15. .andExpect(MockMvcResultMatchers.status().isOk())

  16. .andDo(MockMvcResultHandlers.print())

  17. .andReturn();

  18. }

  19.  

  20. //验证controller是否正常响应并判断返回结果是否正确

  21. @Test

  22. public void testHello() throws Exception {

  23. mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))

  24. .andExpect(status().isOk())

  25. .andExpect(content().string(equalTo("Hello World")));

  26. }

  27.  

  28. }

Unit testing is to verify that your code is the first barrier, to develop every part of writing code unit testing habits, do not wait until after the entire integration testing, after integration because it is more concerned about the overall operating results, it is easy to miss out on the bottom of the code bug.

Integration Testing

Overall developed into the later integration testing, start the entrance Spring Boot project in the Application class, run directly run method can start the project, but in the process of debugging we certainly need to continue to debug the code, if each modified once the code is required manually restart a service is very troublesome, Spring Boot gives a very intimate supports hot deployment, it is easy to debug using the Web project.

pom need to add the following configuration:

  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-devtools</artifactId>

  5. <optional>true</optional>

  6. </dependency>

  7. </dependencies>

  8.  

  9. <build>

  10. <plugins>

  11. <plugin>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-maven-plugin</artifactId>

  14. <configuration>

  15. <fork>true</fork>

  16. </configuration>

  17. </plugin>

  18. </plugins>

  19. </build>

After adding the above configuration, the project will support hot deployment, integration testing is very convenient.

Production on line

Actually, I think at this stage, it should be relatively simple generally divided into two types; one is packaged into a jar package directly executed, the other is packaged into war package into the tomcat server.

Labeled jar package

If you are using maven to manage the project, execute the following command either

  1. cd 项目跟目录(和pom.xml同级)

  2. mvn clean package

  3. ## 或者执行下面的命令

  4. ## 排除测试代码后进行打包

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

After complete package jar package will be generated to the next target directory, usually named after the project name + version .jar

Start a command jar package

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

In this way, as long as the console is closed, you can not access the service. Here we use to start running in the background:

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

You can also choose to read different configuration files at startup

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

Jvm parameters can also be set at boot time

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

gradle
If you are using gradle, use the following command packaging

  1. gradle build

  2. java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar

Labeled as war package

After the war package labeled generally divided into two ways, the first packet may be derived by war eclipse this development tool, another is to use the command to complete, this presents a major

1, maven project, modify pom package

will

  1. <packaging>jar</packaging>

Changed

  1. <packaging>war</packaging>

 

2, tomcat exclude when packaging.

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-web</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>org.springframework.boot</groupId>

  7. <artifactId>spring-boot-starter-tomcat</artifactId>

  8. <scope>provided</scope>

  9. </dependency>

Here Provided the scope attribute set, so WAR ultimately formed will not contain the JAR package because like Jetty or Tomcat server will provide an API classes at runtime.

3, registration startup class

Creating ServletInitializer.java, inheritance SpringBootServletInitializer, covering configure (), the startup class Application Registration go. External Web application server to build Web Application Context of the time, will start classes added to it.

  1. public class ServletInitializer extends SpringBootServletInitializer {

  2. @Override

  3. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

  4. return application.sources(Application.class);

  5. }

  6. }

The last execution

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

It is generated in the target directory: Project name + version .war file, copied to the tomcat server to start.

gradle

If you are using the same Gradle, basic step-outs, build.gradle add support for war, excluding spring-boot-starter-tomcat:

  1. ...

  2.  

  3. apply plugin: 'war'

  4.  

  5. ...

  6.  

  7. dependencies {

  8. compile("org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE"){

  9. exclude mymodule:"spring-boot-starter-tomcat"

  10. }

  11. }

  12. ...

Then use the build command

  1. gradle build

war is generated in the build \ libs directory.

Operation and maintenance of production

Check the value of JVM parameters

Java comes jinfo can command:

  1. jinfo -flags pid

After you start using a jar to see what gc, the new generation, batch-old's memory is how much, for example:

  1. -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 : The maximum number of parallel compilation

  • -XX:InitialHeapSize And   : initial and maximum heap size of the JVM specified-XX:MaxHeapSize

  • -XX:MaxNewSize : Maximum allocation size of the new generation area of ​​heap memory JVM

  • ...

  • -XX:+UseParallelGC : Garbage collector using Parallel

How to Restart

Simple and crude

Directly kill off the process starts again jar package

  1. ps -ef|grep java

  2. ##拿到对于Java程序的pid

  3. kill -9 pid

  4. ## 再次重启

  5. Java -jar xxxx.jar

Of course, this way more traditional and violence, it is recommended that you use the following approach to managing

Script Execution

If you are using maven, you need to include the following configuration

  1. <plugin>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-maven-plugin</artifactId>

  4. <configuration>

  5. <executable>true</executable>

  6. </configuration>

  7. </plugin>

If a gradle, need to include the following configuration

  1. springBoot {

  2. executable = true

  3. }

Start:

1, you can directly start./yourapp.jar

2, registered as a service

You can also do a soft link pointing to your jar and added to the package , and then use the command to start.init.d

init.d example:

  1. ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp

  2. chmod +x /etc/init.d/yourapp

So that you can use stopor restartcommand to manage your application.

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

or

  1. service yourapp start|stop|restart

How to test this Spring Boot project, to reconcile the package has been introduced into production are finished, you can find time to look after the Spring Boot automated operation and maintenance, as well as Spring Boot and Docker combination use.

Article content has been upgraded to Spring Boot 2.x

示例代码-https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package


 

Java technology geeks public number, is founded by a group of people who love Java technology development, focused on sharing of original, high-quality Java articles. If you feel that our article is not bad, please help appreciated, watching, forwarding support, encourage us to share a better article.

No public concern, we can reply, "blog Park" No background in public, freely available knowledge of Java / information interview must-see.

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11211807.html