配置Feign的日志打印和超时时间

配置Feign的日志打印和超时时间

使用OpenFeign

引入依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

启动类加入注解

@SpringBootApplication
@EnableFeignClients

定义feign接口

package cn.vantee.proxy;

import cn.vantee.entity.AjaxResult;
import cn.vantee.entity.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author :rayfoo@qq.com
 * @date :Created in 2021/12/25 4:28 下午
 * @description:订单模块的控制层
 * @modified By:
 * @version: 1.0.0
 */
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OrderProxy {

    /**
     * 通过RestTemplate远程调用支付服务
     * @param id
     * @return
     */
    @GetMapping("/payment/findOne/{id}")
    AjaxResult<Payment> findPaymentById(@PathVariable("id") long id);

}

使用OpenFeign接口

package cn.vantee.controller;

import cn.vantee.entity.AjaxResult;
import cn.vantee.entity.Payment;
import cn.vantee.proxy.OrderProxy;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author :rayfoo@qq.com
 * @date :Created in 2021/12/25 7:59 下午
 * @description:订单控制层
 * @modified By:
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderProxy orderProxy;

    @GetMapping("/payment/findOne/{id}")
    AjaxResult<Payment> findPaymentById(@PathVariable("id") long id){
        try{
            return orderProxy.findPaymentById(id);
        }catch (Exception ex){
            return new AjaxResult<Payment>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"payment8002:"+ex.getMessage());
        }

    }

}

配置超时时间

ribbon有一个坑,直接按照下面的形式设置超时时间不会生效:

ribbon:
  restclient:
    enabled: true
  connect-timeout: 60000
  read-timeout: 60000

需要设置feign内的ribbon:

# 设置feign内置ribbon端超时时间 使用ribbon的配置方式方式不可行。
feign:
  client:
    config:
      default:
        # 请求处理的超时时间
        ReadTimeout: 3000
        # 请求连接的超时时间
        ConnectTimeout: 3000

配置OpenFeign日志打印

添加配置类

配置类需要加在扫描包能扫描到的目录

package cn.vantee.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

添加配置文件

包名根据实际情况配置,也可以设置接口名,类名。

# 配置feign的日志打印
logging:
  level:
    cn.vantee.proxy: debug
posted @ 2021-12-25 22:04  张瑞丰  阅读(823)  评论(0编辑  收藏  举报