Mongoosej.Blog.Software.Programming.Java.Framework.Spring.SpringCloud.API


API

org.springframework.cloud:spring-cloud-openfeign

  1. <groupId>org.springframework.cloud</groupId>
  2. <artifactId>spring-cloud-openfeign-core</artifactId>

o.s.c.o.FeignClient

Annotation for interfaces declaring that a REST client with that interface should becreated (e.g. for autowiring into another component). If ribbon is available it will beused to load balance the backend requests, and the load balancer can be configured using a @RibbonClient with the same name (i.e. value) as the feign client. 声明应创建具有该接口的REST客户端的接口注解(例如,用于自动连接到另一个组件)。如果ribbon可用,它将用于负载平衡后端请求,并且可以使用与外部客户端同名(即值)的@RibbonClient配置负载平衡器。

value

The name of the service with optional protocol prefix. Synonym for name. A name must be specified for all clients, whether or not a url is provided.Can be specified as property key, eg: ${propertyKey}. 带有可选协议前缀的服务的名称。name的同义词。无论是否提供url,都必须为所有客户端指定名称。可以指定为属性键,例如:${propertyKey}。即使用表达式来为value赋值。

o.s.c.o.FeignClientBuilder

A builder for creating Feign clients without using the FeignClient annotation. 一个用于在不适用FeignClient注解的情况下创建Feign客户端的生成器。 This builder builds the Feign client exactly like it would be created by using the FeignClient annotation. 这个生成器构造Feign客户端与使用FeignClient注解构造Feign客户端完全相同。

在需要动态指定Feign客户端服务名的情况下,使用FeignClientBuilder十分方便。

Aspect

SpringCloud

offical website
版本详细选型
image.png

OpenFeign

OpenFeign由Spring团队开发,源于Netflix的Feign,由于Netflix的Feign停更,并且Netflix负责Feign研发的核心成员离职,导致Feign的发展停滞。Spring在此基础上,研发了OpenFeign。

Reference:

使用步骤:

  1. 接口包创建api接口类添加@FeignClient注解,当该接口被调用时,会触发Feign的动态代理机制,到注册中心发现@FeignClient注解对应的微服务,然后调用发现的微服务的对应接口方法的URL地址。
  2. 微服务包创建api接口类的实现类,并至少申明为@Component,保证实现类能够被ioc容器注入。
  3. 调用者需要引用需要调用的接口的接口包,并且调用者的启动类需要添加@EnableFeignClients(value={“com.ksec.wcs4.api”})注解,@EnableFeignClients注解需要指明FeignClient的扫描路径,在加载时,会扫描被@FeignClient注解的接口,并形成对接口的动态代理。