简介及相关概念

Elastic APM 组成: APM Agent, APM Server, Elasticsearch, Kibana APM UI
Elastic APM - 图1

APM Agent种类

agent-java的核心源码

APM Server

  • 接收来自APM Agent的数据。
  • 将数据转换成Elasticsearch Document并存储在Elasticsearch indices中

安装APM Server 官方文档

使用

  1. 创建elasticsearch, kibana, APM server

以docker-compose为例启动一个单节点的elasticsearch + kibana + apm server集群
docker-compose.yaml

  1. version: '3'
  2. services:
  3. elasticsearch:
  4. image: elasticsearch:7.4.2
  5. ports:
  6. - 9200:9200
  7. - 9300:9300
  8. environment:
  9. - discovery.type=single-node
  10. - cluster.name=es-docker-cluster
  11. volumes:
  12. - es_data01:/usr/share/elasticsearch/data
  13. kibana:
  14. image: kibana:7.4.2
  15. environment:
  16. ELASTICSEARCH_HOSTS: http://elasticsearch:9200
  17. ports:
  18. - 5601:5601
  19. elasticapm:
  20. image: docker.elastic.co/apm/apm-server:7.4.2
  21. ports:
  22. - 8200:8200
  23. environment:
  24. - output.elasticsearch.hosts=["elasticsearch:9200"]
  25. user: apm-server
  26. volumes:
  27. es_data01:
  28. driver: local

启动
docker-compose up -d

  1. 在项目中引入apm agent依赖
<!-- maven 项目的 pom.xml -->
<dependency>
    <groupId>co.elastic.apm</groupId>
    <artifactId>apm-agent-attach</artifactId>
    <version>1.11.0</version>
</dependency>
  1. 配置apm agent的相关参数

以spring boot项目为例,在 src/main/resources/ 路径下创建 elasticapm.properties 文件
application_packages 为需要监听的包路径,如果要监听多个包,可以用逗号隔开。官方文档

service_name=eureka-service   // 服务名称
application_packages=ca.com.elastic  // 
server_url=http://10.100.172.59:8200 // APM server 的地址
  1. 启动应用

以spring boot项目为例,在boot的main函数中调用以下函数

package xyz.homesangsang;

import co.elastic.apm.attach.ElasticApmAttacher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
    public static void main(String[] args) {
        // 重点,调用ElasticApmAttacher.attach();函数
        ElasticApmAttacher.attach();
        SpringApplication.run(RegistryApplication.class, args);
    }
}

配置好后即可启动项目

  1. 查看监控

image.pngimage.png