全部产品

Spring Cloud OpenFeign 埋点接入

更新时间:2020-02-18 09:27:17

本文将演示如何使用 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-enterprise-dependencies</artifactId>
  4. <version>${sofa.boot.version}</version>
  5. </parent>

这里的 ${sofa.boot.version} 指定具体的 SOFABoot 开源版版本,参考 SOFABoot 版本说明

新建 tracer-sample-with-openfeign-provider

  • 在工程模块的 pom.xml 文件中添加 SOFATracer 依赖:

    1. <dependency>
    2. <groupId>com.alipay.sofa</groupId>
    3. <artifactId>tracer-enterprise-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-enterprise-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 即当前应用的根目录(您可以根据自己的实践需要进行配置),在当前工程的根目录下可以看到类似如下结构的日志文件:

  1. ./logs
  2. ├── spring.log
  3. └── tracelog
  4. ├── feign-digest.log
  5. ├── feign-stat.log
  6. ├── spring-mvc-digest.log
  7. ├── spring-mvc-stat.log
  8. ├── static-info.log
  9. └── tracer-self.log

示例中通过 SpringMvc 提供的 Controller 作为请求入口,然后使用 openfeign client 发起向下游资源的访问调用,日志大致如下:

  1. {"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":""}