OpenFeigin service interface calls

I. Introduction

1, OpenFeign Profile

Feign is a declarative WebService client. Feign make use of written Web Service clients easier. Its use is to define a service interface and then add annotations on top. Having inserted annotation support, including Feign notes and annotations JAX-RS; pluggable type encoder and decoder. Spring Cloud to Feign the package, to support the SpringMVC standard annotations and HttpMessageConverters. Feign Ribbon can be used in combination with Eureka and to support load balancing. Official documents address: https://cloud.spring.io/spring-cloud-openfeign/reference/html/ .

2 difference, Feign with the OpenFeign

  • Feign is a lightweight HTTP RESTful service client of SpringCloud components, built Ribbon, used to make the client load balancing, to call the service registration service center. Feign use: Use Feign notes define the interface, the interface can call this service call the service registry.

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    
  • OpenFeign is SpringCloud support SpringMVC comment on the basis of Feign, such as @RequestMapping and so on. OpenFeign of @FeignClient interfaces can be resolved under @RequestMapping notes SpringMVC and generate dynamic proxy implementation class by the way, do load balancing implementation class and calls to other services.

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

Two, OpenFeign using procedure

1. Create a new module, introduced in dependence Feign pom file

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

2, based on the master boot annotated @EnableFeignClients open Feign;

3, create an interface in the consumer side, the interface method is a method in the service provider;

@Component
//添加Feign注解,value=要寻找的微服务名称
@FeignClient("CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    CommonResult getPaymentById(@PathVariable("id") Long id);
}

4, define your own business class, call the method Feign interface can be, without introducing RestTempLate go in manually.

@Resource
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
    return paymentFeignService.getPaymentById(id);
}

Three, OpenFeign timeout control

1. Description

There is a time difference of service request processing time between the consumer and the service provider side, the program will lead to a timeout error. To solve this problem, the two sides agreed to make some business for some, to ensure the normal operation of the program. Request timed out error page as follows:

2, timeout setting, add the following configuration at the end of application.yml consumption.

#设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
  # 指的是建立连接后从服务器读取到可用资源所用的时间,单位ms
  ReadTimeout: 5000
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间,单位ms
  ConnectTimeout: 5000

Four, OpenFeign log print

. 1, Feign printing function provides a log, the log level can be adjusted by arranging to understand the details of the Http request Feign, a call to the interface to monitor the situation and output.

2, log level

  • NONE: default, does not show any log;
  • BASIC: Only requests recording method, URL, and the response status code execution time;
  • HEADERS: Basic information at defined, there are header information of the request and response;
  • FULL: the information HEADERS defined, there are text and metadata requests and responses.

3, the configuration method

  • Define a class configuration

    @Configuration
    public class FeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  • Modify yml file

    logging:
      level:
        # feign日志以什么级别监控哪个接口
        com.xhanglog.springcloud.service.PaymentFeignService: debug
    

Guess you like

Origin www.cnblogs.com/Mhang/p/12558299.html