配置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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM