Spring Cloud OpenFeign

 

1. 声明性 REST 客户端:Feign

  Feign是声明性Web服务客户端。 它使编写Web服务客户端更加容易。 要使用Feign,请创建一个接口并对其进行注释。 它具有可插入注释支持,包括Feign注释和JAX-RS注释。 Feign还支持可插拔编码器和解码器。 Spring Cloud添加了对Spring MVC注释的支持,并支持使用Spring Web中默认使用的注释。 Spring Cloud集成了Ribbon和Eureka以及Spring Cloud LoadBalancer以在使用Feign.HttpMessageConverters时提供负载平衡的HTTP客户端

1.1. 如何包括费因

  要将Feign包含在您的项目中,请使用具有组和工件ID的启动器。 

Example spring boot 

@SpringBootApplication
@EnableFeignClients
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
StoreClient.java
@FeignClient("stores")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();

    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
    Store update(@PathVariable("storeId") Long storeId, Store store);
}

在注释中,字符串值(上面的“ stores”)是一个任意的客户端名称,用于创建Ribbon负载平衡器(请参阅下面的功能区支持详细信息)或Spring Cloud LoadBalancer。 您还可以使用属性(绝对值或仅主机名)指定URL。 应用程序上下文中的Bean名称是接口的标准名称。 要指定自己的别名值,可以使用批注的值。@ FeignClienturlqualifier @ FeignClient

上面的负载平衡器客户端将希望发现“商店”服务的物理地址。 如果您的应用程序是Eureka客户端,则它将在Eureka服务注册表中解析该服务。 如果您不想使用Eureka,只需使用SimpleDiscoveryClient在外部配置中配置服务器列表即可。

  为了保持向后兼容性,被用作默认的负载均衡器实现。 但是,Spring Cloud Netflix Ribbon现在处于维护模式,因此我们建议改用Spring Cloud LoadBalancer。spring.cloud.loadbalancer.ribbon.enabled false

1.2. Overriding Feign Defaults

Spring Cloud的Feign支持的中心概念是指定客户的概念。 每个伪客户端都是组件集合的一部分,这些组件可以一起工作以按需联系远程服务器,并且该集合的名称是您使用注释将其指定为应用程序开发人员的。 Spring Cloud使用来为每个命名客户端按需创建一个新集合。 这包含(除其他事项外)an,a和a。 通过使用批注的属性,可以覆盖该集合的名称。@ FeignClientApplicationContextFeignClientsConfigurationfeign.Decoderfeign.Encoderfeign.ContractcontextId @ FeignClient

Spring Cloud通过使用声明其他配置(位于之上),使您可以完全控制假客户端。

示例:FeignClientsConfiguration @ FeignClient

@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
   
}

在这种情况下,客户端由已经存在的组件以及任何in组成(后者将覆盖前者).FeignClientsConfigurationFooConfiguration

  FooConfiguration不需要使用注释。 但是,如果是这样,请注意将其从任何其他不包括此配置的配置中排除,因为当指定时它将成为,,等的默认来源。 可以通过将其与or或放在单独的,不重叠的包中来避免这种情况,也可以在中明确排除它。 @ Configuration @ ComponentScanfeign.Decoderfeign.Encoderfeign.Contract @ ComponentScan @ SpringBootApplication @ ComponentScan
  现在不推荐使用该属性,而推荐使用该属性。 serviceIdname
  除了更改集合的名称之外,还使用注释的属性,它将覆盖客户机名称的别名,并将其用作为该客户机创建的配置Bean名称的一部分。 contextId @ FeignClientApplicationContext
  以前,使用属性不需要属性。 现在需要使用。 url name name

and 属性中支持占位符。name url

@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {
   
}

Spring Cloud Netflix默认为伪装提供以下bean(beanName:)BeanType ClassName

  • Decoder feignDecoder: (which wraps a ResponseEntityDecoder SpringDecoder)

  • Encoder feignEncoder: SpringEncoder

  • Logger feignLogger: Slf4jLogger

  • Contract feignContract: SpringMvcContract

  • Feign.Builder feignBuilder: HystrixFeign.Builder

  • Client  feignClient:如果Ribbon在类路径中且已启用,则为a;否则,如果Spring Cloud LoadBalancer在类路径中,则使用。 如果它们都不在类路径中,则使用默认的伪装客户端。LoadBalancerFeignClient FeignBlockingLoadBalancerClient

  spring-cloud-starter-openfeign 同时包含 pring-cloud-starter-netflix-ribbonspring-cloud-starter-loadbalancer

OkHttpClient和ApacheHttpClient伪装客户端可以通过分别将或设置为,并将它们放在类路径中来使用。 您可以通过在使用Apache或使用OK HTTP时提供一个bean来定制HTTP客户端。feign.okhttp.enabled feign.httpclient.enabled true org.apache.http.impl.client.CloseableHttpClient okhttp3.OkHttpClient

默认情况下,Spring Cloud Netflix不会为伪装提供以下bean,但仍会从应用程序上下文中查找这些类型的bean以创建伪装客户端:

  • Logger.Level

  • Retryer

  • ErrorDecoder

  • Request.Options

  • Collection<RequestInterceptor>

  • SetterFactory

  • QueryMapEncoder

创建这些类型之一的bean并将其放置在配置中(例如上述配置),您可以覆盖所描述的每个bean。

 Example: @FeignClient FooConfiguration

@Configuration
public class FooConfiguration {
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password");
    }
}

替换并添加到 SpringMvcContract   feign.Contract.Default   RequestInterceptor  RequestInterceptor 

@FeignClient 也可以使用配置属性进行配置。

application.yml

feign:
  client:
    config:
      feignName:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: full
        errorDecoder: com.example.SimpleErrorDecoder
        retryer: com.example.SimpleRetryer
        requestInterceptors:
          - com.example.FooRequestInterceptor
          - com.example.BarRequestInterceptor
        decode404: false
        encoder: com.example.SimpleEncoder
        decoder: com.example.SimpleDecoder
        contract: com.example.SimpleContract

可以按照与上述类似的方式在属性中指定默认配置。 区别在于此配置将应用于所有虚拟客户端。@ EnableFeignClients  defaultConfiguration

application程序.yml

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic

如果我们同时创建bean和配置属性,则配置属性将获胜。 它将覆盖值。

但是,如果要将优先级更改为,则可以更改为

@Configuration  @Configuration  @Configuration  feign.client.default-to-properties  false

 

如果您需要使用绑定变量或在Feign中禁用Hystrix。

ThreadLocalRequestInterceptor`s you will need to either set the thread isolation strategy for Hystrix to `SEMAPHORE

应用程序.yml

# To disable Hystrix in Feign
feign:
  hystrix:
    enabled: false

# To set thread isolation to SEMAPHORE
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE

如果我们要创建多个具有相同名称或URL的伪客户端,以便它们指向同一台服务器,但每个客户端具有不同的自定义配置,则必须使用的属性,以避免这些配置Bean发生名称冲突。 contextId  @FeignClient

@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class)
public interface FooClient {
    
}
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class)
public interface BarClient {
   
}

1.3. 手动创建 Feign Clients

在某些情况下,可能有必要使用上述方法无法实现的方式自定义Feign客户。 在这种情况下,您可以使用Feign Builder API创建客户端。 下面是一个示例,该示例创建具有相同接口的两个Feign Client,但为每个Feign Client配置一个单独的请求拦截器。

@Import(FeignClientsConfiguration.class)
class FooController {

    private FooClient fooClient;

    private FooClient adminClient;

        @Autowired
    public FooController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.fooClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
                .target(FooClient.class, "https://PROD-SVC");

        this.adminClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
                .target(FooClient.class, "https://PROD-SVC");
    }
}
  在上面的示例中是春云 Netflix 提供的默认配置。FeignClientsConfiguration.class
 

PROD-SVC是客户端将向其请求的服务的名称。

  Feign对象定义在接口上有效的注释和值。 自动装配的bean提供对SpringMVC注释的支持,而不是默认的Feign本机注释。Contract  Contract

1.4. Feign Hystrix 支持

如果Hystrix在classpath和上,Feign将使用断路器包装所有方法。 还可以返回a。 这样一来,您就可以使用反应性模式(调用或或异步使用(调用)

feign.hystrix.enabled=true  com.netflix.hystrix.HystrixCommand.toObservable().observe().queue()

要基于每个客户端禁用Hystrix支持,请使用 "prototype"作用域

例如:Feign.Builder

@Configuration
public class FooConfiguration {
        @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }
}
 

在Spring Cloud Dalston发行之前,如果Hystrix在类路径中,Feign默认会将所有方法包装在断路器中。 Spring Cloud Dalston中更改了此默认行为,以支持选择加入方法

1.5. Feign Hystrix Fallbacks

Hystrix支持回退的概念:当它们的电路断开或出现错误时执行的默认代码路径。

要为给定集启用后备,该属性应为实现后备的类名称。

您还需要将实现声明为Spring bean。

@FeignClient  fallback

@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

static class HystrixClientFallback implements HystrixClient {
    @Override
    public Hello iFailSometimes() {
        return new Hello("fallback");
    }
}

如果需要访问引起回退触发器的原因,则可以使用内部的属性。fallbackFactory  @FeignClient

@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
    @Override
    public HystrixClient create(Throwable cause) {
        return new HystrixClient() {
            @Override
            public Hello iFailSometimes() {
                return new Hello("fallback; reason was: " + cause.getMessage());
            }
        };
    }
}
 

 

 

1.6.Feign and @Primary

将Feign与Hystrix后备一起使用时,同一类型的多个bean。 这将导致无法正常运行,因为实际上并没有一个bean,也没有一个标记为主要的bean。 为了解决这个问题,Spring Cloud Netflix将所有Feign实例标记为,因此Spring Framework将知道要注入哪个bean。 在某些情况下,这可能不是理想的。 要关闭此行为,请将属性设置为false。ApplicationContext  @Autowired  @Primaryprimary  @FeignClient

@FeignClient(name = "hello", primary = false)
public interface HelloClient {
  
}

1.7. Feign 继承 支持

Feign通过单继承接口支持样板API。 这允许将常用操作分组为方便的基本接口。

UserService.java

public interface UserService {

    @RequestMapping(method = RequestMethod.GET, value ="/users/{id}")
    User getUser(@PathVariable("id") long id);
}
UserResource.java
@RestController
public class UserResource implements UserService {

}
UserClient.java
package project.user;

@FeignClient("users")
public interface UserClient extends UserService {

}
  通常不建议在服务器和客户端之间共享接口。 它引入了紧密耦合,并且实际上也无法以当前形式与Spring MVC一起使用(方法参数映射不被继承)。1.8. 费恩请求/响应压缩

1.8. Feign request/response 压缩

您可以考虑为您的Feign请求启用请求或响应GZIP压缩。 您可以通过启用以下属性之一来做到这一点:

feign.compression.request.enabled=true feign.compression.response.enabled=true

Feign 请求压缩为您提供了类似于为 Web 服务器设置的设置:

feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

这些属性允许您对压缩介质类型和最小请求阈值长度进行选择性选择。

对于除 OkHttpClient 外的 http 客户端,可以启用默认 gzip 解码器以 UTF-8 编码解码 gzip 响应:

feign.compression.response.enabled=true
feign.compression.response.useGzipDecoder=true

1.9. Feign Logging

为每个创建的 Feign 客户端创建一个记录器。默认情况下,记录器的名称是用于创建 Feign 客户端的接口的完整类名称。Feign 日志记录仅响应级别。DEBUG

application程序.yml
logging.level.project.user.UserClient: DEBUG

每个客户端可以配置的对象告诉 Feign 要记录多少。选项包括:Logger.Level

  • NONE,无日志记录(DEFAULT)。

  • BASIC,只记录请求方法和 URL 以及响应状态代码和执行时间。

  • HEADERS,将基本信息连同请求和响应标头一起记录。

  • FULL,记录请求和响应的标头、正文和元数据。

例如,以下将 设置为 :Logger.LevelFULL

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

1.10.Feign @QueryMap support

OpenFeign批注支持将POJO用作GET参数映射。 不幸的是,默认的OpenFeign QueryMap注释与Spring不兼容,因为它缺少属性。@QueryMap  value

Spring Cloud OpenFeign提供了等效的注释,用于将POJO或Map参数注释为查询参数map。@ SpringQueryMap

例如,定义参数 :Params param1 param2

// Params.java
public class Params {
    private String param1;
    private String param2;

    // [Getters and setters omitted for brevity]
}

以下Feign客户端使用注释使用 类:Params  @SpringQueryMap

@FeignClient("demo")
public interface DemoTemplate {

    @GetMapping(path = "/demo")
    String demoEndpoint(@SpringQueryMap Params params);
}

如果需要对生成的查询参数映射进行更多控制,则可以实现一个自定义bean.QueryMapEncoder

1.11. HATEOAS 支持

Spring 提供了一些 API,以创建遵循HATEOAS原则的 REST 表示形式。

如果项目使用启动器或启动器,则默认启用 Feign HATEOAS 支持。org.springframework.boot:spring-boot-starter-hateoasorg.springframework.boot:spring-boot-starter-data-rest

启用 HATEOAS 支持后,允许 Feign 客户端序列化并反序列化 HATEOAS 表示模型:实体模型、集合模型和pagedModel。

@FeignClient("demo")
public interface DemoTemplate {

    @GetMapping(path = "/stores")
    CollectionModel<Store> getStores();
}

1.12. Spring @MatrixVariable 支持

春云 OpenFeign 为春季注释提供支持。@MatrixVariable

如果映射作为方法参数传递,则路径段是通过将键值对从映射与 联接 来创建。@MatrixVariable=

如果传递了其他对象,则注释中提供(如果已定义)或注释化变量名称将使用 提供的方法参数与 联接。name@MatrixVariable=

重要

即使在服务器端,Spring 并不要求用户命名路径段占位符与矩阵变量名称相同,因为它在客户端上过于模糊,Sprig Cloud OpenFeign 要求您添加具有与注释中提供的名称(如果已定义)或注释化变量名称匹配的路径段占位符。name@MatrixVariable

例如:

@GetMapping("/objects/links/{matrixVars}")
Map<String, List<String>> getObjects(@MatrixVariable Map<String, List<String>> matrixVars);

请注意,变量名称和路径段占位符都称为 。matrixVars

@FeignClient("demo")
public interface DemoTemplate {

    @GetMapping(path = "/stores")
    CollectionModel<Store> getStores();
}

1.13. 故障排除

1.13.1. 早期初始化错误

根据使用 Feign 客户端的方式,在启动应用程序时可能会看到初始化错误。要解决此问题,您可以在自动连接客户端时使用 。ObjectProvider

@Autowired
ObjectProvider<TestFeginClient> testFeginClient;

2. 配置属性

原文地址:https://www.cnblogs.com/i-xq/p/12835567.html