23. 声明性 REST 客户端: Feign

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

23.1 如何包含 Feign

要在项目中包含 Feign,请使用 group ID 为 org.springframework.cloud 和 artifact ID 为 spring-cloud-starter-openfeign 的 starter。了解有关使用当前 Spring Cloud Release Train 构建系统设置的详细信息请参阅 Spring Cloud 项目页面。

示例 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);
}

在 @FeignClient 注解中,字符串值(上面的 "stores")是一个任意的客户端名称,用于创建一个 Ribbon 负载均衡器(请参见有关 Ribbon 支持的详细信息)。还可以使用 url 属性(绝对值或仅主机名)指定 URL。应用程序上下文中 bean 的名称是接口的完全限定名。要指定自己的别名值,可以使用 @FeignClient 注解的 qualifier 值。

上面的 Ribbon 客户端将希望发现 "stores" 服务的物理地址。如果你的应用程序是 Eureka 客户端,那么它将解析 Eureka 服务注册表中的服务。如果你不想使用 Eureka,只需在外部配置中配置服务器列表(请参见上面的示例)。

23.2 覆盖 Feign 默认值

Spring Cloud 的 Feign 支持的一个核心概念是命名客户端。每个虚拟客户端都是一组组件的一部分,这些组件共同工作以按需联系远程服务器,并且该集合有一个名称,你可以使用 @FeignClient 注解将其作为应用程序开发人员提供。Spring Cloud 使用 FeignClientsConfiguration 为每个命名客户端创建了一个新的集成作为 ApplicationContext。其中包括一个 feign.Decoder、一个 feign.Encoder 和一个 feign.Contract。

Spring Cloud 允许你通过使用 @FeignClient 声明附加配置(在 FeignClientsConfiguration 之上)来完全控制虚客户端。例子:

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

在这种情况下,客户端由已经在 FeignClientsConfiguration 中的组件和任何在 FooConfiguration 中的组件组成(后者将覆盖前者)。

[Note] Note

FooConfiguration 不需要用@Configuration 注解。但是,如果是这样,请注意将其从任何 @ComponentScan 中排除,否则将包括此配置,因为它将成为指定时的 feign.Decoder、feign.Encoder、feign.Contract 等的默认源。这可以通过将其放在任何 @ComponentScan 或 @SpringBootApplication 的单独、不重叠的包中来避免,也可以在 @ComponentScan 中显式排除它。

[Note] Note

现在不推荐使用 serviceId 属性而使用 name 属性。

[Warning] Warning

以前,使用 url 属性不需要 name 属性。现在需要使用 name。

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

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

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

  • Decoder feignDecoder: ResponseEntityDecoder (包含一个 SpringDecoder)
  • Encoder feignEncoder: SpringEncoder
  • Logger feignLogger: Slf4jLogger
  • Contract feignContract: SpringMvcContract
  • Feign.Builder feignBuilder: HystrixFeign.Builder
  • Client feignClient: 如果启用了 Ribbon,则它是一个 LoadBalancerFeignClient,否则将使用默认的 feign 客户端。

通过分别将 feign.okhttp.enabled 或 feign.httpclient.enabled 设置为 true 并将其放在类路径上,可以使用 OkHttpClient 和 ApacheHttpClient feign 客户端。你可以通过在使用 Apache 时提供 ClosableHttpClient 的 bean 或在使用 OK HTTP 时提供 OkHttpClient 来定制使用的 HTTP 客户端。

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

  • Logger.Level
  • Retryer
  • ErrorDecoder
  • Request.Options
  • Collection<RequestInterceptor>
  • SetterFactory

创建一个这种类型的 bean 并将其放置在 @FeignClient 配置中(如上面的 FooConfiguration),可以覆盖所描述的每个 bean。例子:

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

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

这将使用 feign.Contract.Default 替换 SpringMvcContract,并向 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 中以类似于上述的方式指定。区别在于,此配置将应用于所有 feign 客户端。

如果你喜欢使用配置属性来配置所有 @FeignClient,则可以使用默认的 feign 名称创建配置属性。

application.yml

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

如果我们同时创建 @Configuration bean 和 configuration 属性,那么 configuration 属性将获胜。它将覆盖 @Configuration 值。但是,如果要将优先级更改为 @Configuration,可以将 feign.client.default-to-properties 更改为 false。

[Note] Note

如果你需要在 RequestInterceptor 中使用 ThreadLocal 绑定变量,你需要将 Hystrix 的线程隔离策略设置为 SEMAPHORE,或者在 Feign 中禁用 Hystrix。

application.yml

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

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

23.3 手动创建 Feign 客户端

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

@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, "http://PROD-SVC");

		this.adminClient = Feign.builder().client(client)
				.encoder(encoder)
				.decoder(decoder)
				.contract(contract)
				.requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
				.target(FooClient.class, "http://PROD-SVC");
    }
}
[Note] Note

在上面的示例中,FeignClientsConfiguration.class 是 Spring Cloud Netflix 提供的默认配置。

[Note] Note

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

[Note] Note

Feign Contract 对象定义哪些注解和值在接口上有效。自动连接的 Contract bean 提供对 SpringMVC 注解的支持,而不是默认的 Feign 本地注解。

23.4 Feign Hystrix 支持

如果 Hystrix 在类路径上,并且 feign.hystrix.enabled=true,那么 Feign 将用断路器包装所有方法。还可以返回 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();
	}
}
[Warning] Warning

在 Spring Cloud Dalston 发布之前,如果 Hystrix 在类路径上,那么默认情况下,Feign 会将所有方法包装在一个断路器中。这个默认行为在 Spring Cloud Dalston 中被更改,以支持选择加入方法。

23.5 Feign Hystrix 回退

Hystrix 支持回退的概念:一个默认的代码路径,当它们的电路断开或出错时执行。要为给定的 @FeignClient 启用回退,请将 fallback 属性设置为实现回退的类名。你还需要将实现声明为 Spring Bean。

@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");
    }
}

如果需要访问导致回退触发器的原因,可以在 @FeignClient 内使用 fallbackFactory 属性。

@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());
			}
		};
	}
}
[Warning] Warning

Feign 中回退的实现以及 Hystrix 回退的工作方式存在局限性。对于返回 com.netflix.hystrix.HystrixCommand 和 rx.Observable 的方法,当前不支持回退。

23.6 Feign 和 @Primary

在对 Hystrix 回退使用 Feign 时,同一类型的 ApplicationContext 中有多个 bean。这将导致 @Autowired 无法工作,因为没有只存在一个 bean,或有一个标记为 primary。为了解决这个问题,Spring Cloud Netflix 将所有虚实例标记为 @Primary,因此 Spring Framework 将知道要注入哪个 bean。在某些情况下,这可能是不可取的。若要关闭此行为,请将 @FeignClient 的 primary 属性设置为 false。

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

23.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 {

}

[Note] Note

通常不建议在服务器和客户端之间共享接口。它引入了紧密耦合,并且实际上在当前形式下也不适用于 Spring MVC(方法参数映射不是继承的)。

23.8 Feign 请求/响应压缩

你可以考虑为你的 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

这些属性允许你选择压缩媒体类型和最小请求阈值长度。

23.9 Feign 日志

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

application.yml. 

logging.level.project.user.UserClient: DEBUG

你可以为每个客户端配置 Logger.Level 对象,它告诉我们要记录多少。选择如下:

  • NONE, 没有日志记录 (DEFAULT).
  • BASIC, 只记录请求方法和 URL 以及响应状态代码和执行时间。
  • HEADERS, 记录基本信息以及请求和响应头。
  • FULL, 记录请求和响应的头、正文和元数据。

例如,下面将 Logger.Level 设置为 FULL:

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

23.10 Feign @QueryMap 支持

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

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

例如,Params 类定义参数 param1 和 param2:

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

    // [Getters and setters omitted for brevity]
}

以下 feign 客户端使用 @SpringQueryMap 注解使用 Params 类:

@FeignClient("demo")
public class DemoTemplate {

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