4

我正在使用 Feign Builder 发出顺序请求。请求的标题中没有 x-b3-traceid,x-b3-spanid ..。这就是为什么我的最后一个客户的日志出现在 zipkin 上的原因。

我使用 spring boot 2.4.2、spring cloud 2020.0.0、feign-core 10.10.1、feign-okhttp 10.10.1。我已经尝试过 spring-cloud-openfeign 并取得了想要的结果。但我不想使用这个库。这里有使用 Feign Builder 和 Rest Template 的请求。我在 zipkin 上没有看到相同的日志。

我的 Client1 应用程序。我正在发送请求 http://localhost:8082/

import feign.Client;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sample.feign2.Client2Feign;

import javax.servlet.http.HttpServletRequest;
@RestController
public class SampleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Client client;

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient();
    }

    @GetMapping("/hello/feignBuilder")
    public String sayHelloFeignBuilder(HttpServletRequest httpServletRequest){
        logger.info("Send to request client2");
        Client2Feign client2Feign = Feign.builder()
                .client(client)
                .decoder(new Decoder.Default())
                .encoder(new Encoder.Default())
                .target(Client2Feign.class, "http://localhost:8082/");
       return client2Feign.sayHelloFeignBuilder();
    }

    @GetMapping("/hello/say")
    public String sayHelloFeignBuilder1(){
        return "Hello";
    }

    @GetMapping("/hello/rest")
    public String sayHelloRest(){
        System.out.println(tracer.currentSpan());
        logger.info("Inside rest 1");
        String baseUrl = "http://localhost:8082/sayHelloRest";
        String response = (String) restTemplate.exchange(baseUrl, HttpMethod.GET, null, String.class).getBody();
        logger.info("The response received by client2 is " + response);
        return response;
    }

}

这是我的 client1 应用程序的 yml。我在 client2 和 clint3 的其他客户端应用程序上使用相同的 yml conf。仅更改端口和应用程序名称。


server:
  port: 8081
spring:
  application:
    name: euraka-client1
  zipkin:
    enabled: true
    service.name: euraka-client1
    sender.type: web
    base-url: http://localhost:9411
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

这是我在 Client2 应用中的 Feign。

import feign.RequestLine;

public interface Client2Feign {

    @RequestLine("GET /sayHelloBuilder")
    String sayHelloFeignBuilder();

}

这里是 ClientFeign2 的 impl。

import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

public interface FeignBuilderController {

    @GetMapping("/sayHelloBuilder")
    String sayHelloBuilder(HttpServletRequest httpServletRequest);

    @GetMapping("/sayHelloRest")
    String sayHelloRest(HttpServletRequest httpServletRequest);
}
import feign.Client;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sample.feign.Client3Feign;
import sample.feign2.Client2Feign;

import javax.servlet.http.HttpServletRequest;

@RestController
public class FeignBuilderControllerImpl implements FeignBuilderController{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Client client;

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient();
    }

    @Override
    public String sayHelloBuilder(HttpServletRequest httpServletRequest) {
        logger.info("Send to request client3");
        Client3Feign client3Feign = Feign.builder()
                .client(client)
                .decoder(new Decoder.Default())
                .encoder(new Encoder.Default())
                .target(Client3Feign.class, "http://localhost:8083/");
        return client3Feign.sayHelloFeignBuilder3();
    }

    @Override
    public String sayHelloRest(HttpServletRequest httpServletRequest) {
        logger.info("Inside rest 2");
        String baseUrl = "http://localhost:8083/sayHelloRestClient3";
        String response = (String) restTemplate.exchange(baseUrl, HttpMethod.GET, null, String.class).getBody();
        logger.info("The response received by client3 is " + response);
        return response;
    }

这是我在 Client3 应用中的 Feign。

import feign.RequestLine;

public interface Client3Feign {

    @RequestLine("GET /sayHelloBuilderClient3")
    String sayHelloFeignBuilder3();

}

这里是 Client3 Feign 的 impl。

import javax.servlet.http.HttpServletRequest;

public interface FeignController3 {

    @GetMapping("/sayHelloBuilderClient3")
    String sayHello3(HttpServletRequest httpServletRequest);

    @GetMapping("/sayHelloRestClient3")
    String sayHelloRest3(HttpServletRequest httpServletRequest);
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping
public class FeignController3Impl implements FeignController3 {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String sayHello3(HttpServletRequest httpServletRequest) {
        logger.info("Response returned from client3");
        return "Hello from client3 using FeignClientBuilder";
    }

    @Override
    public String sayHelloRest3(HttpServletRequest httpServletRequest) {
        logger.info("Response returned from client3");
        return "Hello from client3 using RestClient";
    }
}

来自client3的pom.xml,我在client2/pom使用client3,与client1相同。


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>oguzhan.example</groupId>
    <artifactId>client3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.4.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-info</id>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <release>${java.version}</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

diff-feign-rest-image

伪装-zipkin-img

假装请求-img

rstlet-rest-tmplte-img

休息-zipkin-img

4

1 回答 1

3

问题可能与您通过Feign.builder()工厂方法手动创建 Feign 构建器有关。我们无法检测该呼叫。您应该创建一个 bean(通过SleuthFeignBuilder.builder)并将其注入到您的代码中。

于 2021-02-15T10:01:11.470 回答