OpenFeign 埋点接入

更新时间: 2019-06-21

在本文档将演示如何使用 SOFATracer 对 Spring Cloud OpenFeign 进行埋点,本示例工程地址

基础环境

本案例使用的各框架组件的版本如下:

  • Spring Cloud Greenwich.RELEASE
  • SOFABoot 3.1.1/SpringBoot 2.1.0.RELEASE
  • SOFATracer 3.0.4
  • JDK 8本案例包括两个子工程:

  • tracer-sample-with-openfeign-provider 服务提供方

  • tracer-sample-with-openfeign-consumer 服务调用方

新建 SOFABoot 工程作为父工程

在创建好一个 Spring Boot 的工程之后,接下来就需要引入 SOFABoot 的依赖,首先,需要将上文中生成的 Spring Boot 工程的 zip 包解压后,修改 Maven 项目的配置文件 pom.xml,将

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>${spring.boot.version}</version>
  5. <relativePath/>
  6. </parent>

替换为:

  1. <parent>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofaboot-dependencies</artifactId>
  4. <version>${sofa.boot.version}</version>
  5. </parent>

这里的 ${sofa.boot.version} 指定具体的 SOFABoot 版本,参考发布历史

新建 tracer-sample-with-openfeign-provider

  • 在工程模块的 pom 文件中添加 SOFATracer 依赖
  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>tracer-sofa-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-openfeign</artifactId>
  12. </dependency>

SOFATracer 版本受 SOFABoot 版本管控,如果使用的 SOFABoot 版本不匹配,则需要手动指定 tracer 版本,且版本需高于 3.0.4.

  • 在工程的 application.properties 文件下添加相关参数
  1. spring.application.name=tracer-provider
  2. server.port=8800
  3. spring.cloud.zookeeper.connect-string=localhost:2181
  4. spring.cloud.zookeeper.discovery.enabled=true
  5. spring.cloud.zookeeper.discovery.instance-id=tracer-provider
  • 简单的资源类
  1. @RestController
  2. public class UserController {
  3. @RequestMapping("/feign")
  4. public String testFeign(HttpServletRequest request) {
  5. return "hello tracer feign";
  6. }
  7. }

新建 tracer-sample-with-openfeign-consumer

  • 在工程模块的 pom 文件中添加 SOFATracer 依赖
  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>tracer-sofa-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-openfeign</artifactId>
  12. </dependency>
  • 在工程的 application.properties 文件下添加相关参数
  1. spring.application.name=tracer-consumer
  2. server.port=8082
  3. spring.cloud.zookeeper.connect-string=localhost:2181
  4. spring.cloud.zookeeper.discovery.enabled=true
  5. spring.cloud.zookeeper.discovery.instance-id=tracer-consumer
  • 定义 feign 资源
  1. @FeignClient(value = "tracer-provider",fallback = FeignServiceFallbackFactory.class)
  2. public interface FeignService {
  3. @RequestMapping(value = "/feign", method = RequestMethod.GET)
  4. String testFeign();
  5. }
  • 开启服务发现和feign注解
  1. @SpringBootApplication
  2. @RestController
  3. @EnableDiscoveryClient
  4. @EnableFeignClients
  5. public class FeignClientApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(FeignClientApplication.class,args);
  8. }
  9. @Autowired
  10. private FeignService feignService;
  11. @RequestMapping
  12. public String test(){
  13. return feignService.testFeign();
  14. }
  15. }

测试

先后启动 tracer-sample-with-openfeign-provider 和 tracer-sample-with-openfeign-consumer 两个工程; 然后浏览器访问:http://localhost:8082/ 。然后查看日志:

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

```

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

  1. 示例中通过 SpringMvc 提供的 Controller 作为请求入口,然后使用 openfeign client 发起向下游资源的访问调用,日志大致如下:
  2. ```json
  3. {"time":"2019-03-28 18:08:06.800","local.app":"tracer-consumer","traceId":"0a0fe88f1553767685981100124403","spanId":"0.1","request.url":"http://10.15.232.143:8800/feign","method":"GET","result.code":"200","error":"","req.size.bytes":0,"resp.size.bytes":18,"time.cost.milliseconds":206,"current.thread.name":"http-nio-8082-exec-1","remote.host":"10.15.232.143","remote.port":"","component.client.impl":"open-feign","baggage":""}