【问题标题】:How to Attach Elastic APM in Spring Application如何在 Spring 应用程序中附加 Elastic APM
【发布时间】:2019-11-09 14:29:21
【问题描述】:

我目前正在做一个 PoC 以将 Elastic APM 集成到我的 Spring 应用程序中。我正在关注此页面:- https://www.elastic.co/guide/en/apm/agent/java/1.x/setup-attach-api.html 以编程方式附加 elastic-apm jar。

我已将所需的 jar 添加到 pom.xml 中,但我不知道如何将 Elastic Apm (ElasticApmAttacher.attach()) 附加到我的正常 spring 代码中。给出的示例适用于 SpringBoot。但我的应用程序位于 Spring 核心(spring-core、spring-web ..)上,其余服务使用 Jax-Rs 公开。

【问题讨论】:

    标签: spring elasticsearch apm elastic-apm


    【解决方案1】:

    您可以在 Spring Application 主类中附加 ElasticApmAttacher.attach()

    对于打包成war文件并部署到Tomcat服务器的SpringBootApplication,可以在configure方法中加入这个

    以下代码可能会有所帮助:

    package com.test.main
    
    import co.elastic.apm.attach.ElasticApmAttacher
    import org.springframework.boot.Banner
    import org.springframework.boot.SpringApplication
    import org.springframework.boot.autoconfigure.SpringBootApplication
    import org.springframework.boot.builder.SpringApplicationBuilder
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
    
    
    @SpringBootApplication(scanBasePackages = [ "com.test" ])
    class Application extends SpringBootServletInitializer{
    
        static void main(String[] args) {
            SpringApplication app = new SpringApplication(Application.class)
            app.setBannerMode(Banner.Mode.OFF)
            ElasticApmAttacher.attach();
            app.run(args)
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            ElasticApmAttacher.attach();
            return application.sources(Application.class)
        }
    }
    

    【讨论】:

      【解决方案2】:
          Map<String, String> apmConfiguration = new HashMap<>();
          apmConfiguration.put("server_urls", "http://localhost:8200");
          apmConfiguration.put("service_name", "SpringBootApp");
          ElasticApmAttacher.attach(apmConfiguration);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-07
        相关资源
        最近更新 更多