Instrumenting Spring Cloud OpenFeign

更新时间:
复制 MD 格式

This topic describes how to use SOFATracer to instrument Spring Cloud OpenFeign.

Prerequisites

This demo uses the following component versions:

  • Spring Cloud Greenwich.RELEASE

  • SOFABoot 3.1.1/SpringBoot 2.1.0.RELEASE

  • SOFATracer 3.0.4

  • JDK 8

This demo includes two subprojects:

  • tracer-sample-with-openfeign-provider: the service provider

  • tracer-sample-with-openfeign-consumer: the service consumer

Create a SOFABoot project as the parent project

After you create a Spring Boot project, import the SOFABoot dependencies. Decompress the project's zip package and modify the Maven project's pom.xml file. Replace the following code:

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>${spring.boot.version}</version>
      <relativePath/>
</parent>

Replace with the following:

<parent>
      <groupId>com.alipay.sofa</groupId>
      <artifactId>sofaboot-enterprise-dependencies</artifactId>
      <version>${sofa.boot.version}</version>
</parent>

The ${sofa.boot.version} parameter specifies the version of the open source edition of SOFABoot. For more information, see SOFABoot Version Guide.

Create tracer-sample-with-openfeign-provider

  • Add the SOFATracer dependency to the pom.xml file of the project module:

    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    Note

    The SOFATracer version is managed by the SOFABoot version. If the managed version is not compatible with your SOFABoot version, you must manually specify a SOFATracer version of 3.0.4 or later.

  • Add the following parameters to the application.properties file of the project:

    spring.application.name=tracer-provider
    server.port=8800
    spring.cloud.zookeeper.connect-string=localhost:2181
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.instance-id=tracer-provider
  • Simple resource class

    @RestController
    public class UserController{
        @RequestMapping("/feign")
        public String testFeign(HttpServletRequest request){
            return"hello tracer feign";
        }
    }

Create tracer-sample-with-openfeign-consumer

  • Add the SOFATracer dependency to the pom.xml file of the project module:

    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  • Add the following parameters to the application.properties file of the project:

    spring.application.name=tracer-consumer
    server.port=8082
    spring.cloud.zookeeper.connect-string=localhost:2181
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.instance-id=tracer-consumer
  • Define the Feign resource:

    @FeignClient(value ="tracer-provider",fallback =FeignServiceFallbackFactory.class)
    public interface FeignService{
        @RequestMapping(value ="/feign", method =RequestMethod.GET)
        String testFeign();
    }
  • Enable service discovery and Feign annotations:

    @SpringBootApplication
    @RestController
    @EnableDiscoveryClient
    @EnableFeignClients
    public class FeignClientApplication{
    
        public static void main(String[] args){
            SpringApplication.run(FeignClientApplication.class,args);
        }
    
        @Autowired
        private FeignService feignService;
    
        @RequestMapping
        public String test(){
            return feignService.testFeign();
        }
    }

Test

Start the tracer-sample-with-openfeign-provider and tracer-sample-with-openfeign-consumer projects in sequence. Then, open http://localhost:8082/ in a browser and check the logs.

The application.properties file specifies ./logs as the log output directory, which is the root directory of the application. You can change this path if necessary. In the project's root directory, you can find log files with a structure similar to the following:

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

In this demo, the controller provided by Spring MVC acts as the request entry point. The OpenFeign client then calls the downstream resources. The log is similar to the following:

{"time":"2019-09-03 10:28:52.363","local.app":"tracer-consumer","traceId":"0a0fe9271567477731347100110969","spanId":"0.1","span.kind":"client","result.code":"200","current.thread.name":"http-nio-8082-exec-1","time.cost.milliseconds":"219ms","request.url":"http://10.15.233.39:8800/feign","method":"GET","error":"","req.size.bytes":0,"resp.size.bytes":18,"remote.host":"10.15.233.39","remote.port":"8800","sys.baggage":"","biz.baggage":""}