SpringBoot配置FeignClient打印请求日志

一、Feign原生方式配置(原文:https://blog.csdn.net/cen776727328/article/details/101273190

1、创建Feign的配置文件,并在其中设置日志等级

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        //这里记录所有,根据实际情况选择合适的日志level
        return Logger.Level.FULL;
    }
}

 2、在客户端接口指定此配置 


@FeignClient(value = "haoait-auth", configuration = FeignConfiguration.class, fallback = *****.class)
public interface UserServiceClient extends UserService {
 

3、配置文件开启日志记录
application.properties设置:
logging.level.com.haoait.client.UserServiceClient:debug


logging:
  level:
    com.haoait.client.UserServiceClient:debug

二、spring 代理

1. 在 application.yml(bootstrap.yml)中配置(一 、1 中也可以这样配置)

feign:
  okhttp: 
    enabled: false
  httpclient:
    #Apache的HTTP Client替换Feign原始的http client
    enabled: true
    max-connections: 20480
    max-connections-per-route: 512
    time-to-live: 60
    time-to-live-unit: SECONDS
    connection-timeout: 60000
    user-agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0'
  client:
    # 配置文件优先级高
    default-to-properties: true
    config:
      default:
        logger-level: BASIC
        connect-timeout: 60000
        read-timeout: 60000
        decode404: true

也可以代码里写 

public class FeignConfig {

    /**
     * feign 的日志记录
     * @return
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        /* NONE, 不记录日志 (默认)。
        BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。
        HEADERS, 记录请求和应答的头的基本信息。
        FULL, 记录请求和响应的头信息,正文和元数据。*/
        log.debug("feign输出full级别日志:[ 记录请求和响应的头信息,正文和元数据]");
        return Logger.Level.FULL;
    }

}

 

2. 配置feignconfig

   增加了几个非常有用的其他配置项

@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter({HttpClientFeignConfiguration.class})
@AutoConfigureBefore({FeignClientsConfiguration.class})
public class FeignConfig {
    
 /**
     * +使用Apache Httpclient实现FeignClient客户端
     * @param httpClient #HttpClientFeignConfiguration.customHttpClient()
     * @return ApacheHttpClient实例
     */
    @Bean
    @Primary
    public Client feignClient(HttpClient httpClient) {
        return new ApacheHttpClient(httpClient);
    }
/**
     * +用Spring定义的日志系统代理feign日志系统
     * @return 代理日志系统
     */
    @Bean
    @Primary
    public Logger logger() {
        return new FeignLog(this.getClass());
    }

 /**
     * Springboot的代理log
    
     *
     */
    final class FeignLog extends Logger {
        private Log log;

        public FeignLog(Class<?> clazz) {
            log = LogFactory.getLog(clazz);
        }

        @Override
        protected void log(String configKey, String format, Object... args) {
            //if (log.isDebugEnabled()) {
                log.info(String.format(methodTag(configKey) + format, args));
            //}
        }
    }
/**
     * +重试机制,任何情况下都不重试
     * @return 禁止重试机制
     */
    @Bean
    @Primary
    public Retryer feignRetryer() {
        return Retryer.NEVER_RETRY;
    }

 3. 在客户端接口指定此配置

主程序运行类添加 @EnableFeignClients(basePackages = {"com.****.feign"})

@FeignClient(name = "aaaFeignClient",
        url = "${url}",
        fallback = AAAFeignClientFallback.class,
        configuration = {FeignConfig.class})
public interface AAAFeignClient {

 

4.输出结果

 

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Spring Boot和FeignClient调用天气预报接口的示例代码: 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 然后,在应用程序主类上添加@EnableFeignClients注解,启用FeignClient: ```java @SpringBootApplication @EnableFeignClients public class WeatherApplication { public static void main(String[] args) { SpringApplication.run(WeatherApplication.class, args); } } ``` 接下来,创建一个FeignClient接口来定义调用天气预报API的方法: ```java @FeignClient(name = "weather-api", url = "https://api.weather.com") public interface WeatherApiClient { @GetMapping("/forecast") ResponseEntity<String> getForecast(@RequestParam("city") String city, @RequestParam("apiKey") String apiKey); } ``` 在这个接口上,使用了FeignClient注解来指定要调用的API的名称和URL。然后,定义了一个getForecast方法,它使用@GetMapping注解来指定调用API的HTTP方法和路径,并使用@RequestParam注解来指定必需的查询参数。这个方法返回一个ResponseEntity<String>,其中包含API响应的主体。 最后,在需要调用天气预报API的代码中,注入WeatherApiClient接口,并调用getForecast方法: ```java @RestController public class WeatherController { @Autowired private WeatherApiClient weatherApiClient; @GetMapping("/weather") public ResponseEntity<String> getWeather(@RequestParam("city") String city, @RequestParam("apiKey") String apiKey) { return weatherApiClient.getForecast(city, apiKey); } } ``` 在这个控制器中,注入了WeatherApiClient接口,并在getWeather方法中调用了getForecast方法来获取天气预报数据。这个方法返回了一个ResponseEntity<String>,它包含了API响应的主体。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值