Introduction and use of SpringCloud Feign

Feign is the component responsible for service invocation in Spring Cloud and does the same thing as Ribbon+RestTemplate. The bottom layer of Feign uses Ribbon for load balancing by default. Different from the Ribbon+RestTemplate calling service interface is that when using Feign client, you only need to add annotations to the interface, and the corresponding service method to be called can call the service. The underlying calling process does not require developers to consider, in the controller layer Realize business calls like calling your own local service. OpenFeign has encapsulated and enhanced the original Feign.

Use of OepnFeign

  1. Pom introduces dependencies

    <!--openFeign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    server:
      port: 80
    
    #eureka client
    eureka:
      client:
        #是否将自己注册进eurekaServer,默认true
        register-with-eureka: false
        service-url:
          #服务注册地址
          defaultZone: http://localhost:7001/eureka
    
  2. Add annotation @EnableFeignClients to the SpringBoot startup class

    @SpringBootApplication
    @EnableFeignClients
    public class OrderFeignApplication80 {
          
          
        public static void main(String[] args) {
          
          
            SpringApplication.run(OrderFeignApplication80.class,args);
        }
    }
    
  3. OpenFeign use (there is a corresponding service provider)

    step1. Create service interface

    // 1.添加spirng注解 注入到spring容器
    @Service
    // 2.添加oepnFeign注解,传入微服务名称
    @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
    public interface PaymentService {
          
          
    
        //3. 构建方法,对应的服务提供者
        @GetMapping(value = "/payment/get/{id}")
        BaseResult findById(@PathVariable(value = "id")String id);
    }
    

    step2. Write the controller of the service client

    @RestController
    @RequestMapping(value = "/order")
    public class OrderFeignController {
          
          
    
        //1.注入,这里就跟正常项目一样
        @Resource
        private PaymentService paymentService;
    
        //2.controller里的方法
        @GetMapping(value = "/get/{id}")
        public BaseResult findById(@PathVariable(value = "id")String id){
          
          
            return paymentService.findById(id);
        }
    }
    

    step3. Call the test.

OpenFeign timeout control

​ By default, the Feign client only waits for 1 second. If the server response time exceeds 1 second, the Feign client will directly return to save. In order to avoid this situation, sometimes we need to set the Feign client's timeout control.

The bottom layer of Feign is implemented with Ribbon, and setting the Feign timeout time is to set the Ribbon timeout time:

#设置feign客户端超时时间,OpenFeign默认支持Ribbon,也就是设置Ribbon的超时时间
ribbon:
  #建立连接所用的时间,5s
  ReadTimeout: 5000
  #连接建立后从服务器读取资源所用时间(连接建立等服务器返回的时间)5s
  ConnectTimeout: 5000

OpenFeign log printing function

​ Feign provides log printing function, we can adjust the log level through configuration, so as to understand the details of Http request in Feign. Monitor and output the calling status of Feign interface.

Feign log level :

  • NONE: By default, no log is displayed
  • BASIC: Only record the request method, URL, response status code and execution time
  • HEADERS: In addition to the information defined in BASIC, there are request and response header information
  • FULL: In addition to the information defined in HEADERS, there are also the body and metadata of the request and response

Set up the feign log monitoring interface call

  1. Inject the log configuration class into the spring container

    @Configuration
    public class FeignConfig {
          
          
    
        @Bean
        Logger.Level feignLogggerLevel(){
          
          
            return Logger.Level.FULL;
        }
    }
    
  2. Add log monitoring configuration

    logging:
      level:
        #feign日志以什么级别监听接口
        com.zqq.springcloud.service.PaymentService: debug
    
  3. Call test

    2021-03-13 00:32:23.987 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] <--- HTTP/1.1 200 (3688ms)
    2021-03-13 00:32:23.988 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] connection: keep-alive
    2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] content-type: application/json
    2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] date: Fri, 12 Mar 2021 16:32:23 GMT
    2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] keep-alive: timeout=60
    2021-03-13 00:32:23.990 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] transfer-encoding: chunked
    2021-03-13 00:32:23.990 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] 
    2021-03-13 00:32:23.997 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] {"code":200,"msg":"成功","data":{"id":4,"amount":22.10,"comment":"备注2"}}
    2021-03-13 00:32:23.997 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService   : [PaymentService#findById] <--- END HTTP (78-byte body)
    

Guess you like

Origin blog.csdn.net/zhaoqingquanajax/article/details/114715619
Recommended