SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

As the microservice system becomes larger and larger, the invocation relationship between various services becomes more and more complex, and a tool is needed to help clarify the service link of request invocation. Previously, Spring Cloud Sleuth: Sleuth+Zipkin’s solution was used in distributed Request Link Tracing, and Application Performance Monitoring (APM) was recently found to be a good solution to this problem. Comparing SkyWalking with Elastic APM and finding that Elastic APM is better than SkyWalking, today let’s take a look at how Elastic APM works!

Introduction of Elastic APM

Elastic APM is an application performance monitoring (APM) system based on Elastic Stack. It mainly has the following uses:

  • It is used to monitor application performance in real time, including HTTP request duration, database query information, cache call information, and external HTTP request call information. This helps us quickly identify and resolve performance issues.
  • Automatically collects unprocessed errors and exceptions in applications and displays the stack information of exceptions, helping you quickly locate exceptions and learn their occurrence frequency.
  • Metrics are another important source of information when debugging a production system. The Elastic APM Agent automatically collects host-level metrics (such as Java JVM and Go Runtime metrics).
  • Support for distributed request link tracing enables you to analyze performance issues of the entire service architecture in one view.

Related components

Elastic APM includes four components: APM Agent, APM Server, Elasticsearch, and Kibana.

  • APM Agent: an application library that collects performance monitoring data and error data during application running and sends it to the APM Server after being cached for a short time.
  • APM Server: an independent component that receives performance monitoring data sent by the APM Agent. After verifying and processing the data, it is stored in Elasticsearch and you can then view the performance monitoring data in the Kibana APM application.
  • Elasticsearch: stores application performance monitoring data and provides the aggregation function.
  • Kibana APM App: Visually view APM performance monitoring data to help find performance bottlenecks.

The data model

The Elastic APM Agent captures different types of information from the applications it detects. These operations are called events and can be Span, Transaction, Error, or Metric.

  • Span: Span contains information about the path of code execution during an operation. It measures from the beginning to the end of an operation and can have parent/child relationships with other spans.
  • Transaction: Transaction is a special Span with other attributes associated with it. It describes the highest level of events that the Elastic APM Agent can capture, such as a request, a batch task, and so on.
  • Error: The Error event contains at least information about the original exception in which the Error occurred or the log that was created.
  • Metric: The APM Agent automatically obtains basic host-level metrics, including CPU and memory metrics at the system and process levels. You can also get agent-specific metrics, such as JVM metrics in Java Agent and Go runtime metrics in Go Agent.

Using the practice

Now that you’ve learned the basic concepts above, it’s time to practice using Elastic APM to monitor performance information for SpringBoot applications.

Install Elasticsearch and Kibana

Before installing Elastic APM, we need to install Elasticsearch and Kibana. See how you can get logs from the server! Note Use version 7.6.2.

Install the APM Server

  • Download the APM Server installation package, download address: www.elastic.co/cn/download…

  • After downloading, decompress to the specified directory.

  • Modifying a Configuration Fileapm-server.ymlTo change the connection address of Elasticsearch.
output.elasticsearch:
  hosts: ["localhost:9200"]
Copy the code
  • Run the following command to start the APM Server8200Port operation;
apm-sever -e
Copy the code
  • APM detection in Kibana Server is started successfully, access to the address: http://localhost:5601/app/kibana#/home/tutorial/apm

SpringBoot integrates with APM Agent

There are three ways for Java applications to integrate APM Agents, but we used the simplest way, integrating directly within the application.

  • inpom.xmlTo add dependencies;
<! --Elastic Agent dependencies -->
<dependency>
    <groupId>co.elastic.apm</groupId>
    <artifactId>apm-agent-attach</artifactId>
    <version>1.17.0</version>
</dependency>
Copy the code
  • In the application startup classmainMethod add Elastic APM Attach API;
@SpringBootApplication
public class MallTinyApplication {

    public static void main(String[] args) { ElasticApmAttacher.attach(); SpringApplication.run(MallTinyApplication.class, args); }}Copy the code
  • inresourceAdd the Elastic APM configuration file to the directoryelasticapm.properties;
Configure the service name
service_name=mall-tiny-apm
Configure the base package where the application resides
application_packages=com.macro.mall.tiny
Configure the APM Server access address
server_urls=http://localhost:8200
Copy the code
  • APM detection in Kibana Agent is started successfully, access to the address: http://localhost:5601/app/kibana#/home/tutorial/apm

View performance monitoring information

  • When you open up the surveillance panel, you’ll find oursmall-tiny-apmServices already exist;

  • You can view the application performance information by invoking the application interface several times.

  • Open aTransactionIf you look at the details, you can see that even the SQL execution time information is given to us.

  • Not only that, open to perform the querySpanView the details, even SQL statements have been collected for us;

  • Add a remote call interface to your project to see if you can collect the request call link.
/** * Controller * Created by macro on 2019/4/19. */
@API (tags = "PmsBrandController", description = "Product Brand Management ")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
    
    @apiOperation (" Remote call to get all brand information ")
    @RequestMapping(value = "/remoteListAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> remoteListAll() {
        // Simulate time-consuming operations
        ThreadUtil.sleep(1, TimeUnit.SECONDS);
        // Remote call to get data
        String response = HttpUtil.get("http://localhost:8088/brand/listAll");
        JSONObject jsonObject = new JSONObject(response);
        JSONArray data = jsonObject.getJSONArray("data");
        List<PmsBrand> brandList = data.toList(PmsBrand.class);
        returnCommonResult.success(brandList); }}Copy the code
  • The Elastic APM can replace Sleuth+Zipkin for microservice request link tracking.

  • Before using usspringcloud-learningIn microservice invocation case, request link tracing can also be carried out;

  • Next we create an artificial exception and add it to the methodint i=1/0;Check the collected exception information.

  • Take a look at the application host measurement information, very comprehensive, CPU, memory, JVM information is available, later when tuning performance to see!

conclusion

Elastic APM can replace Sleuth+Zipkin for distributed request link tracing and provide database and cache call duration statistics. Not only that, it can also be used to monitor application performance information and metrics in real time, and even collect error logs. It is a great tool for monitoring application performance!

Project source code address

Github.com/macrozheng/…

The resources

The official document: www.elastic.co/guide/en/ap…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!