文档

RestTemplate 埋点接入

更新时间:

本文将参考示例工程演示如何使用 SOFATracer 对 RestTemplate 进行埋点。

您可以单击 此处 来下载本文中的示例工程。

假设您已经基于 SOFABoot 构建了一个简单的 Spring Web 工程,那么可以通过如下步骤进行操作:

  1. 引入依赖

  2. 工程配置

  3. 添加 Controller

  4. 获取 RestTemplate

  5. 启动应用

  6. 查看日志

引入依赖

在 SOFABoot 的 Web 项目中引入如下 Tracer 依赖:

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
</dependency>

添加 Tracer starter 依赖后,可在 SOFABoot 的全局配置文件中添加配置项目以定制 Tracer 的行为。详情见 Tracer 配置项说明

工程配置

在工程的 application.properties 文件下添加 SOFATracer 要使用的参数,包括spring.application.namespring.application.name

  • spring.application.name 用于标示当前应用的名称。

  • logging.path 用于指定日志的输出目录。

# Application Name
spring.application.name=TestTemplateDemo
# logging path
logging.path=./logs

添加一个提供 RESTful 服务的 Controller

在工程代码中,添加一个简单的 Controller,例如:

    @RestController
    public class SampleController{
        private final AtomicLong counter =new AtomicLong(0);
        @RequestMapping("/rest")
        public Map<String,Object> rest(){
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("count", counter.incrementAndGet());
            return map;
        }

        @RequestMapping("/asyncrest")
        public Map<String,Object> asyncrest() throws InterruptedException{
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("count", counter.incrementAndGet());
            Thread.sleep(5000);
            return map;
        }
    }

构造 RestTemplate

以 API 方式构造 RestTemplate 发起一次对上文的 RESTful 服务的调用:

  • 构造 RestTemplate 同步调用实例

    RestTemplate restTemplate =SofaTracerRestTemplateBuilder.buildRestTemplate();
    ResponseEntity<String> responseEntity = restTemplate.getForEntity(
    "http://sac.alipay.net:8080/rest",String.class);
  • 构造 RestTemplate 异步调用实例

    AsyncRestTemplate asyncRestTemplate =SofaTracerRestTemplateBuilder
    .buildAsyncRestTemplate();
    ListenableFuture<ResponseEntity<String>> forEntity = asyncRestTemplate.getForEntity(
    "http://sac.alipay.net:8080/asyncrest",String.class);

获取 RestTemplate

以自动注入的方式获取 RestTemplate:

@Autowired
RestTemplate             restTemplate;

启动应用

启动 SOFABoot 应用,将会在控制台中看到启动打印的日志:

2018-10-2410:45:28.683  INFO 5081---[           main] o.s.j.e.a.AnnotationMBeanExporter:Registering beans for JMX exposure on startup
2018-10-2410:45:28.733  INFO 5081---[           main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat started on port(s):8080(http)
2018-10-2410:45:28.736  INFO 5081---[           main] c.a.s.t.e.r.RestTemplateDemoApplication:StartedRestTemplateDemoApplication in 2.163 seconds (JVM running for 3.603)

调用成功的日志如下:

2018-10-2410:45:28.989  INFO 5081---[           main] c.a.s.t.e.r.RestTemplateDemoApplication:Response is {"count":1}
2018-10-2410:45:34.014  INFO 5081---[           main] c.a.s.t.e.r.RestTemplateDemoApplication:AsyncResponse is {"count":2}
2018-10-2410:45:34.014  INFO 5081---[           main] c.a.s.t.e.r.RestTemplateDemoApplication: test finish .......

查看日志

在上面的 application.properties 里面,配置的日志打印目录是 ./logs,即当前应用的根目录(您可以根据自己的实践需要进行配置),在当前工程的根目录下可以看到类似如下结构的日志文件:

./logs
├── spring.log
└── tracelog
├── resttemplate-digest.log
├── resttemplate-stat.log
├── spring-mvc-digest.log
├── spring-mvc-stat.log
├──static-info.log
└── tracer-self.log

示例中通过构造两个 RestTemplate(一个同步一个异步)发起对同一个 RESTful 服务的调用,调用完成后可以在 restTemplate-digest.log 中看到类似如下的日志:

  • 摘要日志

    {"time":"2019-09-03 10:33:10.336","local.app":"RestTemplateDemo","traceId":"0a0fe9271567477985327100211176","spanId":"0","span.kind":"client","result.code":"200","current.thread.name":"SimpleAsyncTaskExecutor-1","time.cost.milliseconds":"5009ms","request.url":"http://localhost:8801/asyncrest","method":"GET","req.size.bytes":0,"resp.size.bytes":0,"sys.baggage":"","biz.baggage":""}
  • 统计日志

    {"time":"2019-09-03 10:34:04.130","stat.key":{"method":"GET","local.app":"RestTemplateDemo","request.url":"http://localhost:8801/asyncrest"},"count":1,"total.cost.milliseconds":5009,"success":"true","load.test":"F"}
  • 本页导读 (0)
文档反馈