OpenFeign

OpenFeign

 

feign a declarative WebService client, using Feign make written Web Service clients easier.

 

His method is to use the definition of a service interface, then add annotations on top. Feign also supports pluggable encoder and decoder. Spring Cloud to Feign the package, to support Spring MVC standard annotation and HttpMessageConverters. Feign Ribbon can be used in combination with Eureka and to support load balancing. https://spring.io/projects/spring-cloud-openfeign

 

 Feign can I do?

 

Feign makes writing java Http client easier

 

When used previously Ribbon + RestTemplate, encapsulation using RestTemplate Http request processing, the method of forming a set of templates of call, but in actual development, since more than one call may depend on the service, the interface tends to be a multiple invocation. So Feign On this basis, made a further package by him to help us define and implement depend on the service interface. In the realization of Feign, we only need to create an interface and use annotations way to configure it (previously marked Dao Interface Mapper above notes, now is a microblogging service interface to the top mark a comment Feign) to complete the service provides an interface to bind parties to simplify the workload using Spring Cloud Ribbon, automatic packaging service calls the client.

Feign integrated Ribbon

  Ribbon maintain the service list information, and client load balancing by way of polling. The difference is that with the Ribbon, through Feign only need to define the binding interface services and to methods declarative, elegant and simple implementation of a service call.

Feign and OpenFeign difference?

Feign

OpenFeign

Feign is Http service client Feign a lightweight RestFul of Spring Cloud components built Ribbon, used for load balancing client service to call the service registry. Feign use is: using Feign notes define the interface, the interface can call this service call the service registry.

OpenFeign is Spring Cloud on the basis feign support SpringMvc annotations, such as @RequestMapping etc.

OpenFeign of @FeignClient interfaces can be resolved under @RequestMapping notes SpringMVC and generate dynamic proxy implementation class by the way, the implementation class load balancing and call other services

<dependency>

<groupId>org.springframework.cloud</ groupId >

<artifactId>spring-cloud-starter-feign</ artifactId>

</ dependency >

<dependency>

<groupId>org.springframework.cloud</ groupId >

<artifactId>spring-cloud-starter-openfeign</artifactId>

</ dependency >

 

OpenFeign use steps:

Interface annotated: Micro service call interface + @FeignClient

 

 

 

Master Boot categories: Add @EnableFeignClients

 

 

 

OpenFeign default wait time 1 second, after a timeout error (modified Yml):

server:
  port: 80

eureka:
  client:
    register-with-eureka: true
    service-url:
     defaultZone: defaultZone:http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

spring:
  application:
    name: feign-customer-order

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

 

配置OpenFeign的日志级别:

1:新建config配置文件

package com.dw.springcloud.config;

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

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

2:修改yml:

server:
  port: 80

eureka:
  client:
    register-with-eureka: true
    service-url:
     defaultZone: defaultZone:http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

spring:
  application:
    name: feign-customer-order

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

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

 

Guess you like

Origin www.cnblogs.com/dw3306/p/12636496.html