RestTemplate Integration

Update time: 2019-12-06

RestTemplate Integration

In this document will demonstrate how to use SOFATracer to track of RestTemplate, this example address.

Assuming you have built a simple Spring Web project based on SOFABoot, Then you can be operated by the following steps:

Introduce dependency

    <!-- SOFABoot version unified management -->

Project Configuration

Then, add the parameters to be used by SOFATracer in the project’s file, including that indicates the name of the current application and logging.path that specifies the log output directory.

# Application Name
# logging path

Add a Controller that provides RESTFul services

In the project, provide a simple Controller, for example:

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

    public Map<String, Object> asyncrest() throws InterruptedException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", counter.incrementAndGet());
        return map;

Construct the RestTemplate in API model to initiate a call to the RESTful service above

  • Construct a RestTemplate synchronous call instance
RestTemplate restTemplate = SofaTracerRestTemplateBuilder.buildRestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
            "", String.class);
  • Construct a RestTemplate asynchronous call instance
AsyncRestTemplate asyncRestTemplate = SofaTracerRestTemplateBuilder
ListenableFuture<ResponseEntity<String>> forEntity = asyncRestTemplate.getForEntity(
            "", String.class);

Get the RestTemplate in an automatic injection

RestTemplate             restTemplate;

Run the project

Start the SOFABoot app and see the log in the console as follows:

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

Successful call:

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

View log

In the, the log printing directory we configured is ./logs, which is the root directory of the current application (we can configure it based on actual situation). In the root directory, you can see log files in the structure similar to the followings:

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

In the example, a call to the same RESTful service is initiated by constructing two RestTemplates (one synchronous one asynchronous),After the call is completed, you can see a log similar to the following in the restTemplate-digest.log:

  • Digest log
{"time":"2018-10-24 10:45:28.977","":"RestTemplateDemo","traceId":"0a0fe8b3154034912878910015081","spanId":"0","request.url":"","method":"GET","result.code":"200","resp.size.bytes":0,"time.cost.milliseconds":188,"":"main","":"","baggage":""}
{"time":"2018-10-24 10:45:34.013","":"RestTemplateDemo","traceId":"0a0fe8b3154034912900410025081","spanId":"0","request.url":"","method":"GET","result.code":"200","resp.size.bytes":0,"time.cost.milliseconds":5009,"":"SimpleAsyncTaskExecutor-1","":"","baggage":""}
  • Statistical log
{"time":"2018-10-24 10:46:28.769","stat.key":{"method":"GET","":"RestTemplateDemo","request.url":""},"count":1,"total.cost.milliseconds":5009,"success":"true","load.test":"F"}
{"time":"2018-10-24 10:46:28.770","stat.key":{"method":"GET","":"RestTemplateDemo","request.url":""},"count":1,"total.cost.milliseconds":188,"success":"true","load.test":"F"}