Deployment test monitoring of SpringBoot

The deployment of

Based on the maven

packaging

JAR

Jar packages are generally used, and the default springBoot package is used.

Using the maven command:

mvn clean package -Dmaven.test.skip=trueCopy the code

After the command is executed successfully, you can find the corresponding package in the target directory, for example, eg-zuul-0.0.1- snapshot.jar

WAR

run

Built-in container operation

Springboot has a built-in Web Container tomcat, which can be run using the Java-jar command.

Such as:

Java jar XXX/target/eg - zuul - 0.0.1 - the SNAPSHOT. The jarCopy the code

External container runs

You can also use war, using external Tomcat to run, but the code needs to change: 1 change the packaging mode to war package; In pom.xml, add the configuration under the version tag:

<package>war</package>Copy the code

2 Add the transformation startup code

package com.springbootpractice.egzuul; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; /** * @author <a href="mailto:[email protected]"> Carterbrother </a> * @description Runs springboot via an external container * @date 21 June 2019  15:32 * @Copyright (c) carterbrother */ public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(EgZuulApplication.class); }}Copy the code

Principle is: from the beginning of the servlet3.1, running without web. XML web application, only need to implement ServletContainerInitializer interface, and SpringBootServletInitializer extends this class, so can be achieved without XML start;

3 Configure external Tomcat

4 Ignore the packing check

< plugin > < groupId > org. Apache. Maven. Plugins < / groupId > < artifactId > maven - war - the plugin < / artifactId > < version > 2.3 < / version > <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>Copy the code

Hot deployment

Spring-boot-devtools for development; Introducing dependencies:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
</dependency>Copy the code

Exposed configuration information can be configured for more detailed aspects of processing:

test

Based on junit and Mockito (eliminate the difficulties of various environments with HTTP)

Test business layer

Test the REST

package com.springbootpractice.eguser; import com.springbootpractice.api.user.dto.UserPro; import com.springbootpractice.eguser.service.UserService; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class EgUserApplicationTests { @Autowired private UserService userService; @Autowired private TestRestTemplate testRestTemplate; @Test public void testUserService() { UserPro userPro = new UserPro(); userPro.setId(1L); userPro.setUserName("xxxaaa"); final Map<String, Object> map = userService.insertUser(userPro); Assert.assertequals (" insert failed ",true,map.get("success")); assert.assertequals (" insert failed ",true,map.get("success")); final UserPro userProReturn = userService.getUserPro(1L); Assert.assertEquals(userPro,userProReturn); } @Test public void testUserRest() { UserPro userPro = new UserPro(); userPro.setId(2L); userPro.setUserName("BBBB"); Map map = testRestTemplate.postForObject("/insert", userPro, Map.class); Assert.assertequals (" insert failed ",true,map.get("success")); assert.assertequals (" insert failed ",true,map.get("success")); UserPro userProReturn = testRestTemplate.getForObject("/user/{id}", UserPro.class, 2L); Assert.assertEquals(userPro,userProReturn); }}Copy the code

The Mock test

Use mocks when the dependent services are not fully developed and the functionality you want to test is heavily dependent.

package com.springbootpractice.egproduct; import com.springbootpractice.api.user.UserApi; import com.springbootpractice.api.user.dto.UserPro; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.BDDMockito; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class EgProductApplicationTests { @MockBean private UserApi userApi; @Test public void mockUserApiTest() { UserPro mockUserPro = new UserPro(); mockUserPro.setId(1L); mockUserPro.setUserName("xxxx"); BDDMockito.given(userApi.getUserPro(1L)).willReturn(mockUserPro); UserPro userProReturn = userApi.getUserPro(1L); Assert.assertEquals(userProReturn,mockUserPro); }}Copy the code

monitoring

Based on the actuator, it monitors operating status and performs some simple management

WEB monitoring

Introducing dependencies:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.hateoas</groupId> <artifactId>spring-hateoas</artifactId> < version > 0.24.0. RELEASE < / version > < / dependency >Copy the code

By default, only health, info is enabled.

If you want to open all: point to point management. The endpoint. Web. Exposure. Include = *

Common monitoring points are:

url Monitoring shows that
health Monitoring information
info
beans Beans in containers
mappings url mapping
env Configuration parameters
shutdown Close the service
conditions Automatic assembly related information

Sensitive configuration information can be controlled and protected using Spring-Security.

Shutdown the endpoint is off by default, open configuration properties are: management endpoint. Shutdown. Enabled = true

Access /actuator/shutdown requires POST requests.

The general configuration of the endpoint switch is as follows:

/ / all the endpoint is off by default, then select endpoints that need to be exposed to open management endpoints. The enabled – by – the default = false

 

Annotation @endpoint annotation class, @readOperation annotation method identification GET method, @writeOperation identification POST method, @deleteOperation identification Delete method

package com.springbootpractice.egproduct.endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; /** * @author <a href="mailto:[email protected]"> Carterbrother </a> * @description Adds one endpoint * @date 2019 06月21日 18:42 * @Copyright (c) carterbrother */ @Endpoint(id = "dbCheck",enableByDefault = true) @Component public class DBCheckEndpoint { @ReadOperation public String test(){ return "db check ok"; }}Copy the code

 

The actuator has many built-in health indicators that need to be configured to display. Configuration methods:

management.endpoint.health.show-details=always

package com.springbootpractice.egproduct.health; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.stereotype.Component; import java.io.IOException; import java.net.InetAddress; /** * @author <a href="mailto:[email protected]"> Carterbrother </a> * @description check network * @date 2019 06月21日 18:30 * @Copyright (c) carterbrother */ @Component public class WWWHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { if (ping()){ Builder.withdetail ("message"," normal connection to the Internet ").up(); return; } builder.withdetail ("message"," unable to connect to Internet ").unknown(); } private boolean ping() { try { return InetAddress.getByName("www.baidu.com").isReachable(3000); } catch (IOException e) { return false; }}}Copy the code

JMX monitoring

Compared with HTTP monitoring, JMX monitoring is also provided.

The typical usage is jConsole of JDK. JMX protocol is used to connect the local JVM for monitoring. The returned information can be viewed under Health under MBean. To monitor it.

Original is not easy, reprint please indicate the source.