The way Feign sets the header and log printing

header settings

       When using Feign to make remote calls between microservices, you need to add information to the header, so springcloud open feign has 5 ways to set the request header information:

  • Add headers attribute in @RequestMapping, @GetMapping, @PostMapping and other interface annotations
  • Add the @RequestHeader annotation in front of the method parameters
  • Add @Headers annotations to methods or classes
  • Add the @HeaderMap annotation in front of the method parameters
  • Implement the RequestInterceptor interface

Add headers attribute in @RequestMapping, @GetMapping, @PostMapping annotations

@PostMapping(value = "/get", headers = {"Content-Type=application/json;charset=UTF-8")
String get();

@RequestMapping(value = "/post", method = RequestMethod.POST)
String post();

Add the @RequestHeader annotation in front of the method parameters

//设置单个header
@GetMapping(value = "/single")
public Stringsingle(@RequestHeader("Authorization") String token);

//设置多个header
@PostMapping(value = "/multi")
public String multi(@RequestHeader MultiValueMap<String, String> headers);

Add @Headers annotations to methods or classes

//先配置feign自带契约
@Configuration
public class FeignConfiguration {
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }
}


//使用@Headers 注解
@FeignClient(url = "${user.api.url}", name = "user", configuration = FeignConfiguration .class)
public interface UserFeignClient {
    @RequestLine("GET /get/{id}")
    @Headers({"Content-Type: application/json;charset=UTF-8", "Authorization: {token}"})
    public User findById(@Param("id") String id, @Param("token") String token);
}

Notice:

FeignClient uses @RequestLineannotations, but Contractif feign's own contract is not configured, @Headersit will not work, and an error will be reported when starting the project.

Method xxx not annotated with HTTP method type (ex. GET, POST)

This is because feign uses spring mvc annotations (such as RequestMapping) by default, so it is necessary to modify its contract by adding a new configuration class.

Add the @HeaderMap annotation in front of the method parameters

//先配置feign自带契约
@Configuration
public class FeignConfiguration {
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }
}

@FeignClient(url = "${user.api.url}", name = "user", configuration = FeignConfiguration .class)
public interface UserFeignClient {
    @RequestLine("GET /get/{id}")
    public User findById(@Param("id") String id, @HeaderMap HttpHeaders headers);
}

Implement the RequestInterceptor interface

By configuring @Component or @Service or @Configuration, the configuration can be injected into the spring container, and the global configuration can be realized, so that all feign interfaces of FeignClient in the project can use the configuration.

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        template.header(HttpHeaders.AUTHORIZATION, "XXXXXXX");
    }
}

If FeignRequestInterceptor is injected into the spring container, it will take effect globally, that is to say, the configuration will take effect even in FeignClient that does not specify the configuration attribute. So if you only want to use this configuration for the feign interface that specifies FeignClient, do not inject this type of configuration into spring

Log printing configuration

1. Configure the log level

@Configuration
public class FeignClientConfig {
    @Bean
    Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }
}

2. Configure logback.xml output

<logger name="com.business.gateway.iao" level="DEBUG" additivity="false">
            <Appender-ref ref="ROLLINGFILE" />
            <Appender-ref ref="WARN" />
            <appender-ref ref="STDOUT"/>
        </logger>

3. Specify the folder where the output feigin class is located

yml configuration:

logging:
  level:
    com.eshore.be.platform.external.core.service.ginfeign: DEBUG

properties configuration:

logging.level. com.eshore.be.platform.external.core.service.ginfeign=DEBUG 

Guess you like

Origin blog.csdn.net/qq_34484062/article/details/128011166