zoukankan      html  css  js  c++  java
  • 配置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
    
  • 相关阅读:
    关于上网内容
    lua 学习笔记1
    庖丁解牛Linux基本系统组成分析
    使用163.com的Centos6 yum源,更新RHEL6系统
    安装FreeBSD 8.2
    虚拟机安装FreeBSD 8.2
    也谈苹果
    2011年国庆老家记录
    Common lisp 学习笔记
    JDBC | 第八章: JDBC常用数据库连接池c3p0,dbcp,durid,hikariCP,tomcatjdbc性能及区别
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/15731761.html
Copyright © 2011-2022 走看看