声明式 REST 客户端:Feign

Feign 是一个声明式 Web 服务客户端。 它使编写 Web 服务客户端变得更加容易。 要使用 Feign,请创建一个接口并对其进行注释。 它具有可插拔的注释支持,包括 Feign 注释和 JAX-RS 注释。 Feign 还支持可插拔编码器和解码器。 Spring Cloud 增加了对 Spring MVC 注解的支持,以及使用 Spring Web 中默认使用的注解。 Spring Cloud 集成了 Eureka、Spring Cloud CircuitBreaker 以及 Spring Cloud LoadBalancer,以便在使用 Feign 时提供负载均衡的 http 客户端。HttpMessageConvertersSpring中文文档

如何包括假装

要将 Feign 包含在项目中,请使用带有 group 和 artifact id 的 starter。有关使用当前 Spring Cloud 发布系列设置构建系统的详细信息,请参阅 Spring Cloud 项目页面org.springframework.cloudspring-cloud-starter-openfeignSpring中文文档

示例 Spring Boot 应用Spring中文文档

@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();

	@GetMapping("/stores")
	Page<Store> getStores(Pageable pageable);

	@PostMapping(value = "/stores/{storeId}", consumes = "application/json",
				params = "mode=upsert")
	Store update(@PathVariable("storeId") Long storeId, Store store);

	@DeleteMapping("/stores/{storeId:\\d+}")
	void delete(@PathVariable Long storeId);
}

在注解中,String 值(上面的“stores”)是一个任意客户端名称,用于创建 Spring Cloud LoadBalancer 客户端。 您还可以使用属性指定 URL (绝对值或只是一个主机名)。Bean 中 Bean 的名称 应用程序上下文是接口的完全限定名称。 若要指定自己的别名值,可以使用该值 的注释。@FeignClienturlqualifiers@FeignClientSpring中文文档

上面的负载均衡器客户端将希望发现物理地址 对于“商店”服务。如果你的应用程序是 Eureka 客户端,那么 它将解析 Eureka 服务注册表中的服务。如果你 不想使用 Eureka,您可以配置服务器列表 在外部配置中使用 SimpleDiscoveryClientSpring中文文档

Spring Cloud OpenFeign 支持 Spring Cloud LoadBalancer 阻塞模式的所有可用功能。您可以在项目文档中阅读有关它们的更多信息。Spring中文文档

要在 -annotated-classes 上使用注解,请确保指定客户端所在的位置,例如: 或显式列出它们:@EnableFeignClients@Configuration@EnableFeignClients(basePackages = "com.example.clients")@EnableFeignClients(clients = InventoryServiceFeignClient.class)

属性解析模式

在创建客户机 Bean 时,我们解析通过注释传递的值。截至 ,这些值正在急切地解决。对于大多数用例来说,这是一个很好的解决方案,它还允许 AOT 支持。Feign@FeignClient4.xSpring中文文档

如果需要延迟解析属性,请将属性值设置为 。spring.cloud.openfeign.lazy-attributes-resolutiontrueSpring中文文档

对于 Spring Cloud Contract 测试集成,应使用延迟属性解析。

覆盖假装默认值

Spring Cloud 的 Feign 支持的一个核心概念是命名客户端。每个假装客户端都是组件集合的一部分,这些组件协同工作以按需联系远程服务器,并且该集合具有您作为应用程序开发人员使用注释为其指定的名称。Spring Cloud 使用 .它包含(除其他外)an , a 和 a 。 可以使用注释的属性覆盖该集成的名称。@FeignClientApplicationContextFeignClientsConfigurationfeign.Decoderfeign.Encoderfeign.ContractcontextId@FeignClientSpring中文文档

Spring Cloud 允许您通过声明其他配置(在 之上)来完全控制假装客户端。例:FeignClientsConfiguration@FeignClientSpring中文文档

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

在这种情况下,客户端由已经包含的组件和任何 in 组成(后者将覆盖前者)。FeignClientsConfigurationFooConfigurationSpring中文文档

FooConfiguration不需要用 进行注释。但是,如果是,请注意将其排除在包含此配置的任何内容之外,因为它将在指定时成为 、 、 等的默认源。这可以通过将其放在与任何 or 分开的、不重叠的包中来避免,也可以将其显式排除在 中。@Configuration@ComponentScanfeign.Decoderfeign.Encoderfeign.Contract@ComponentScan@SpringBootApplication@ComponentScan
使用注释的属性,以及更改名称 ensemble,它将覆盖客户端名称的别名 它将用作为该客户机创建的配置 Bean 名称的一部分。contextId@FeignClientApplicationContext
以前,使用该属性不需要该属性。现在需要使用。urlnamename

和 属性支持占位符。nameurlSpring中文文档

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

Spring Cloud OpenFeign 默认为 feign ( beanName: ) 提供以下 bean :BeanTypeClassNameSpring中文文档

  • DecoderfeignDecoder:(包装一个ResponseEntityDecoderSpringDecoder)Spring中文文档

  • EncoderfeignEncoder:SpringEncoderSpring中文文档

  • LoggerfeignLogger:Slf4jLoggerSpring中文文档

  • MicrometerObservationCapabilitymicrometerObservationCapability:如果在类路径上并且可用feign-micrometerObservationRegistrySpring中文文档

  • MicrometerCapabilitymicrometerCapability:如果在类路径上,则可用和不可用feign-micrometerMeterRegistryObservationRegistrySpring中文文档

  • CachingCapabilitycachingCapability:如果使用了注释。可以通过 禁用。@EnableCachingspring.cloud.openfeign.cache.enabledSpring中文文档

  • Contract假装合同:SpringMvcContractSpring中文文档

  • Feign.BuilderfeignBuilder:FeignCircuitBreaker.BuilderSpring中文文档

  • ClientfeignClient:如果 Spring Cloud LoadBalancer 在类路径上,则使用 feignClient。 如果它们都不在类路径上,则使用默认的假装客户端。FeignBlockingLoadBalancerClientSpring中文文档

spring-cloud-starter-openfeign支持。但是,由于这是一个可选的依赖项,如果要使用它,则需要确保它已添加到项目中。spring-cloud-starter-loadbalancer

若要使用 OkHttpClient 支持的 Feign 客户端和 Http2Client Feign 客户端,请确保要使用的客户端位于类路径上,并分别设置为 或 to。spring.cloud.openfeign.okhttp.enabledspring.cloud.openfeign.http2client.enabledtrueSpring中文文档

对于支持 Apache HttpClient 5 的 Feign 客户端,只需确保 HttpClient 5 位于类路径上就足够了,但您仍然可以通过设置为 来禁用其对 Feign 客户端的使用。 您可以通过在使用 Apache HC5 时提供其中任何一个的 Bean 来自定义所使用的 HTTP 客户端。spring.cloud.openfeign.httpclient.hc5.enabledfalseorg.apache.hc.client5.http.impl.classic.CloseableHttpClientSpring中文文档

您可以通过在属性中设置值来进一步自定义 http 客户端。仅以 为前缀的客户端适用于所有客户端,以 Apache HttpClient 5 为前缀的客户端,以 OkHttpClient 为前缀的客户端,以 Http2Client 为前缀的客户端。您可以在附录中找到可以自定义的属性的完整列表。 如果无法使用属性配置 Apache HttpClient 5,则有一个用于编程配置的接口。spring.cloud.openfeign.httpclient.xxxhttpclienthttpclient.hc5httpclient.okhttphttpclient.http2HttpClientBuilderCustomizerSpring中文文档

从 Spring Cloud OpenFeign 4 开始,不再支持 Feign Apache HttpClient 4。我们建议改用 Apache HttpClient 5。

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

缺省情况下,将创建具有该类型的 Bean,这将禁用重试。 请注意,此重试行为与 Feign 默认行为不同,在 Feign 默认行为中,它将自动重试 IOExceptions, 将它们视为暂时性网络相关异常,以及从 ErrorDecoder 引发的任何 RetryableException。Retryer.NEVER_RETRYRetryerSpring中文文档

创建其中一种类型的 Bean 并将其放置在配置中(如上所示)允许您覆盖所描述的每个 Bean。例:@FeignClientFooConfigurationSpring中文文档

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

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

这将替换 with 并将 a 添加到 的集合中。SpringMvcContractfeign.Contract.DefaultRequestInterceptorRequestInterceptorSpring中文文档

@FeignClient也可以使用配置属性进行配置。Spring中文文档

application.ymlSpring中文文档

spring:
	cloud:
		openfeign:
			client:
				config:
					feignName:
                        url: http://remote-service.com
						connectTimeout: 5000
						readTimeout: 5000
						loggerLevel: full
						errorDecoder: com.example.SimpleErrorDecoder
						retryer: com.example.SimpleRetryer
						defaultQueryParameters:
							query: queryValue
						defaultRequestHeaders:
							header: headerValue
						requestInterceptors:
							- com.example.FooRequestInterceptor
							- com.example.BarRequestInterceptor
						responseInterceptor: com.example.BazResponseInterceptor
						dismiss404: false
						encoder: com.example.SimpleEncoder
						decoder: com.example.SimpleDecoder
						contract: com.example.SimpleContract
						capabilities:
							- com.example.FooCapability
							- com.example.BarCapability
						queryMapEncoder: com.example.SimpleQueryMapEncoder
						micrometer.enabled: false

feignName在此示例中,它也与 和 别名。在负载均衡方案中,它还对应于将用于检索实例的服务器应用。decoders、retryer 和其他类的指定类必须在 Spring 上下文中具有 bean 或具有默认构造函数。@FeignClientvalue@FeignClientname@FeignClientcontextIdserviceIdSpring中文文档

默认配置可以在属性中以与上述类似的方式指定。不同之处在于,此配置将应用于所有假装客户端。@EnableFeignClientsdefaultConfigurationSpring中文文档

如果您更喜欢使用配置属性来配置所有 ,则可以使用假名创建配置属性。@FeignClientdefaultSpring中文文档

您可以使用 和 指定将与名为 的客户端的每个请求一起发送的查询参数和标头。spring.cloud.openfeign.client.config.feignName.defaultQueryParametersspring.cloud.openfeign.client.config.feignName.defaultRequestHeadersfeignNameSpring中文文档

application.ymlSpring中文文档

spring:
	cloud:
		openfeign:
			client:
				config:
					default:
						connectTimeout: 5000
						readTimeout: 5000
						loggerLevel: basic

如果我们同时创建 Bean 和配置属性,则配置属性将获胜。 它将覆盖值。但是如果要将优先级更改为 , 您可以更改为 .@Configuration@Configuration@Configurationspring.cloud.openfeign.client.default-to-propertiesfalseSpring中文文档

如果我们想创建多个具有相同名称或 url 的假装客户端 这样它们就会指向同一个服务器,但每个服务器都有不同的自定义配置 为了避免命名,我们必须使用 的属性 这些配置 Bean 的冲突。contextId@FeignClientSpring中文文档

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

也可以将 FeignClient 配置为不从父上下文继承 bean。 为此,您可以重写 in a bean 以返回:inheritParentConfiguration()FeignClientConfigurerfalseSpring中文文档

@Configuration
public class CustomConfiguration {
	@Bean
	public FeignClientConfigurer feignClientConfigurer() {
		return new FeignClientConfigurer() {
			@Override
			public boolean inheritParentConfiguration() {
				 return false;
			}
		};
	}
}
默认情况下,Feign 客户端不对斜杠字符进行编码。您可以通过将 的值设置为 来更改此行为。/spring.cloud.openfeign.client.decodeSlashfalse

SpringEncoder配置

在我们提供的 中,我们为二进制内容类型和所有其他内容类型设置了字符集。SpringEncodernullUTF-8Spring中文文档

您可以修改此行为,通过将 的值设置为 来从标头字符集派生字符集。Content-Typespring.cloud.openfeign.encoder.charset-from-content-typetrueSpring中文文档

超时处理

我们可以在默认客户端和命名客户端上配置超时。OpenFeign 使用两个超时参数:Spring中文文档

  • connectTimeout防止由于服务器处理时间过长而阻止调用方。Spring中文文档

  • readTimeout从建立连接时开始应用,并在返回响应时间过长时触发。Spring中文文档

如果服务器未运行或不可用,则数据包会导致连接被拒绝。通信以错误消息或回退结束。如果它设置得很低,这可能会之前发生。执行查找和接收此类数据包所花费的时间是造成此延迟的很大一部分原因。它可能会根据涉及 DNS 查找的远程主机而更改。connectTimeout

手动创建假装客户端

在某些情况下,可能需要以一种不 可以使用上述方法。在这种情况下,您可以使用 Feign Builder API 创建客户端。下面是一个示例 它创建了两个具有相同接口的 Feign 客户端,但每个客户端都使用 一个单独的请求拦截器。Spring中文文档

@Import(FeignClientsConfiguration.class)
class FooController {

	private FooClient fooClient;

	private FooClient adminClient;

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

		this.adminClient = Feign.builder().client(client)
				.encoder(encoder)
				.decoder(decoder)
				.contract(contract)
				.addCapability(micrometerObservationCapability)
				.requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
				.target(FooClient.class, "https://PROD-SVC");
	}
}
在上面的示例中是默认配置 由 Spring Cloud OpenFeign 提供。FeignClientsConfiguration.class
PROD-SVC是客户将向其发出请求的服务的名称。
Feign 对象定义哪些注释和值在接口上有效。这 autowired bean 提供对 SpringMVC 注解的支持,而不是 默认的 Feign 本机注解。ContractContract

您也可以使用 on .Builder`to configure FeignClient not to inherit beans from the parent context. You can do this by overriding calling `inheritParentContext(false)BuilderSpring中文文档

Feign Spring Cloud CircuitBreaker 支持

如果 Spring Cloud CircuitBreaker 在类路径上,则 Feign 会用断路器包装所有方法。spring.cloud.openfeign.circuitbreaker.enabled=trueSpring中文文档

要基于每个客户端禁用 Spring Cloud CircuitBreaker 支持,请创建一个具有“原型”范围的香草,例如:Feign.BuilderSpring中文文档

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

断路器名称遵循此模式。当调用 with 接口时,调用的接口方法没有参数,则断路器名称将为 。<feignClientClassName>#<calledMethod>(<parameterTypes>)@FeignClientFooClientbarFooClient#bar()Spring中文文档

自 2020.0.2 起,断路器名称模式已从 更改为 。 使用 2020.0.4 中引入的断路器名称可以保留旧模式。<feignClientName>_<calledMethod>CircuitBreakerNameResolver

提供 的 bean,可以更改断路器名称模式。CircuitBreakerNameResolverSpring中文文档

@Configuration
public class FooConfiguration {
	@Bean
	public CircuitBreakerNameResolver circuitBreakerNameResolver() {
		return (String feignClientName, Target<?> target, Method method) -> feignClientName + "_" + method.getName();
	}
}

要启用 Spring Cloud CircuitBreaker 组,请将该属性设置为(默认)。spring.cloud.openfeign.circuitbreaker.group.enabledtruefalseSpring中文文档

使用配置属性配置断路器

您可以通过配置属性配置断路器。Spring中文文档

例如,如果您有这个 Feign 客户端Spring中文文档

@FeignClient(url = "http://localhost:8080")
public interface DemoClient {

    @GetMapping("demo")
    String getDemo();
}

您可以通过执行以下操作来使用配置属性对其进行配置Spring中文文档

spring:
  cloud:
    openfeign:
      circuitbreaker:
        enabled: true
        alphanumeric-ids:
          enabled: true
resilience4j:
  circuitbreaker:
    instances:
      DemoClientgetDemo:
        minimumNumberOfCalls: 69
  timelimiter:
    instances:
      DemoClientgetDemo:
        timeoutDuration: 10s
如果要切换回 Spring Cloud 之前使用的断路器名称 2022.0.0 您可以设置为 .spring.cloud.openfeign.circuitbreaker.alphanumeric-ids.enabledfalse

假装 Spring Cloud CircuitBreaker 回退

Spring Cloud CircuitBreaker 支持回退的概念:当电路打开或出现错误时执行的默认代码路径。若要为给定对象启用回退,请将属性设置为实现回退的类名。您还需要将您的实现声明为 Spring Bean。@FeignClientfallbackSpring中文文档

@FeignClient(name = "test", url = "http://localhost:${server.port}/", fallback = Fallback.class)
protected interface TestClient {

	@GetMapping("/hello")
	Hello getHello();

	@GetMapping("/hellonotfound")
	String getException();

}

@Component
static class Fallback implements TestClient {

	@Override
	public Hello getHello() {
		throw new NoFallbackAvailableException("Boom!", new RuntimeException());
	}

	@Override
	public String getException() {
		return "Fixed response";
	}

}

如果需要访问触发回退的原因,可以使用 中的属性。fallbackFactory@FeignClientSpring中文文档

@FeignClient(name = "testClientWithFactory", url = "http://localhost:${server.port}/",
			fallbackFactory = TestFallbackFactory.class)
protected interface TestClientWithFactory {

	@GetMapping("/hello")
	Hello getHello();

	@GetMapping("/hellonotfound")
	String getException();

}

@Component
static class TestFallbackFactory implements FallbackFactory<FallbackWithFactory> {

	@Override
	public FallbackWithFactory create(Throwable cause) {
		return new FallbackWithFactory();
	}

}

static class FallbackWithFactory implements TestClientWithFactory {

	@Override
	public Hello getHello() {
		throw new NoFallbackAvailableException("Boom!", new RuntimeException());
	}

	@Override
	public String getException() {
		return "Fixed response";
	}

}

假装和@Primary

将 Feign 与 Spring Cloud CircuitBreaker 回退一起使用时,同一类型中有多个 bean。这将导致无法工作,因为没有正好一个 Bean,或者没有一个标记为主 Bean。为了解决这个问题,Spring Cloud OpenFeign 将所有 Feign 实例标记为 ,因此 Spring Framework 将知道要注入哪个 bean。在某些情况下,这可能并不可取。若要关闭此行为,请将 的属性设置为 false。ApplicationContext@Autowired@Primaryprimary@FeignClientSpring中文文档

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

假装继承支持

Feign 通过单继承接口支持样板 API。 这允许将常见操作分组到方便的基本接口中。Spring中文文档

UserService.java
public interface UserService {

	@GetMapping("/users/{id}")
	User getUser(@PathVariable("id") long id);
}
UserResource.java
@RestController
public class UserResource implements UserService {

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

}
@FeignClient不应在服务器和客户端之间共享接口,并且不再支持使用 ON 类级别注释接口。@FeignClient@RequestMapping

[[假装请求/响应压缩]] === 假装请求/响应压缩Spring中文文档

您可以考虑为您的 假装请求。为此,您可以启用以下属性之一:Spring中文文档

spring.cloud.openfeign.compression.request.enabled=true
spring.cloud.openfeign.compression.response.enabled=true

假装请求压缩为您提供的设置类似于您可能为 Web 服务器设置的设置:Spring中文文档

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

通过这些属性,您可以选择压缩媒体类型和最小请求阈值长度。Spring中文文档

由于 OkHttpClient 使用“透明”压缩(如果存在 or 标头,则禁用该压缩),因此当类路径上存在并设置为 时,我们不会启用压缩。content-encodingaccept-encodingfeign.okhttp.OkHttpClientspring.cloud.openfeign.okhttp.enabledtrue

假装记录

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

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

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

例如,以下命令会将 设置为:Logger.LevelFULLSpring中文文档

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

Feign Capability 支持

Feign 功能公开核心 Feign 组件,以便可以修改这些组件。例如,这些功能可以获取 、装饰它,并将装饰后的实例返回给 Feign。 对 Micrometer 的支持就是一个很好的现实生活中的例子。请参阅千分尺支持ClientSpring中文文档

通过创建一个或多个 Bean 并将它们放置在配置中,您可以注册它们并修改所涉及的客户机的行为。Capability@FeignClientSpring中文文档

@Configuration
public class FooConfiguration {
	@Bean
	Capability customCapability() {
		return new CustomCapability();
	}
}

千分尺支持

如果满足以下所有条件,则创建并注册一个 Bean,以便 Micrometer 可以观察到 Feign 客户端:MicrometerObservationCapabilitySpring中文文档

如果您的应用程序已使用 Micrometer,则启用此功能就像放入类路径一样简单。feign-micrometer

您还可以通过以下任一方式禁用该功能:Spring中文文档

spring.cloud.openfeign.micrometer.enabled=false禁用对所有 Feign 客户端的 Micrometer 支持,而不考虑客户端级标志的值:。 如果要启用或禁用每个客户端的 Micrometer 支持,请不要设置和使用 。spring.cloud.openfeign.client.config.feignName.micrometer.enabledspring.cloud.openfeign.micrometer.enabledspring.cloud.openfeign.client.config.feignName.micrometer.enabled

您还可以通过注册自己的 Bean 来自定义:MicrometerObservationCapabilitySpring中文文档

@Configuration
public class FooConfiguration {
	@Bean
	public MicrometerObservationCapability micrometerObservationCapability(ObservationRegistry registry) {
		return new MicrometerObservationCapability(registry);
	}
}

仍然可以与 Feign(仅指标支持)一起使用,您需要禁用 Micrometer 支持 () 并创建一个 bean:MicrometerCapabilityspring.cloud.openfeign.micrometer.enabled=falseMicrometerCapabilitySpring中文文档

@Configuration
public class FooConfiguration {
	@Bean
	public MicrometerCapability micrometerCapability(MeterRegistry meterRegistry) {
		return new MicrometerCapability(meterRegistry);
	}
}

假装缓存

如果使用注解,则会创建并注册一个 bean,以便您的 Feign 客户端识别其接口上的注解:@EnableCachingCachingCapability@Cache*Spring中文文档

public interface DemoClient {

	@GetMapping("/demo/{filterParam}")
    @Cacheable(cacheNames = "demo-cache", key = "#keyParam")
	String demoEndpoint(String keyParam, @PathVariable String filterParam);
}

您也可以通过 属性 禁用该功能。spring.cloud.openfeign.cache.enabled=falseSpring中文文档

Spring @RequestMapping 支持

Spring Cloud OpenFeign 支持 Spring 注解及其派生注解(如、 等)。 注释上的属性(包括 、 、 、 、 和 )被解析为请求的内容。@RequestMapping@GetMapping@PostMapping@RequestMappingvaluemethodparamsheadersconsumesproducesSpringMvcContractSpring中文文档

请看以下示例:Spring中文文档

使用属性定义接口。paramsSpring中文文档

@FeignClient("demo")
public interface DemoTemplate {

        @PostMapping(value = "/stores/{storeId}", params = "mode=upsert")
        Store update(@PathVariable("storeId") Long storeId, Store store);
}

在上面的示例中,请求 url 解析为 。
params 属性还支持使用多个或仅一个:
/stores/storeId?mode=upsertkey=valuekey
Spring中文文档

  • 当 时,请求 url 解析为 。params = { "key1=v1", "key2=v2" }/stores/storeId?key1=v1&key2=v2Spring中文文档

  • 当 时,请求 url 解析为 。params = "key"/stores/storeId?keySpring中文文档

假装@QueryMap支持

Spring Cloud OpenFeign 提供了一个等效的注解,它 用于将 POJO 或 Map 参数批注为查询参数映射。@SpringQueryMapSpring中文文档

例如,该类定义了参数和:Paramsparam1param2Spring中文文档

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

	// [Getters and setters omitted for brevity]
}

以下 feign 客户端通过使用注释来使用该类:Params@SpringQueryMapSpring中文文档

@FeignClient("demo")
public interface DemoTemplate {

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

如果您需要对生成的查询参数映射进行更多控制,则可以实现自定义 Bean。QueryMapEncoderSpring中文文档

HATEOAS 支持

Spring 提供了一些 API 来创建遵循 HATEOAS 原则的 REST 表示,Spring HateoasSpring Data REST。Spring中文文档

如果您的项目使用启动器 或启动器,默认情况下启用 Feign HATEOAS 支持。org.springframework.boot:spring-boot-starter-hateoasorg.springframework.boot:spring-boot-starter-data-restSpring中文文档

启用 HATEOAS 支持后,允许 Feign 客户端序列化 并反序列化 HATEOAS 表示模型:EntityModelCollectionModelPagedModelSpring中文文档

@FeignClient("demo")
public interface DemoTemplate {

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

Spring @MatrixVariable 支持

Spring Cloud OpenFeign 提供对 Spring 注解的支持。@MatrixVariableSpring中文文档

如果将映射作为方法参数传递,则通过将映射中的键值对与 .@MatrixVariable=Spring中文文档

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

重要

尽管在服务器端,Spring 不要求用户将路径段占位符命名为与矩阵变量名称相同的占位符,因为它在客户端上过于模糊,但 Spring Cloud OpenFeign 要求您添加一个路径段占位符,其名称与注释中提供的名称(如果已定义)或带注释的变量名称匹配。name@MatrixVariableSpring中文文档

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

请注意,变量名称和路径段占位符都称为 。matrixVarsSpring中文文档

@FeignClient("demo")
public interface DemoTemplate {

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

佯装支持CollectionFormat

我们通过提供注释来支持。 您可以通过传递所需的注释值来注释 Feign 客户端方法(或整个类以影响所有方法)。feign.CollectionFormat@CollectionFormatfeign.CollectionFormatSpring中文文档

在下面的示例中,使用格式而不是默认格式来处理方法。CSVEXPLODEDSpring中文文档

@FeignClient(name = "demo")
protected interface DemoFeignClient {

    @CollectionFormat(feign.CollectionFormat.CSV)
    @GetMapping(path = "/test")
    ResponseEntity performRequest(String test);

}

反应式支持

由于 OpenFeign 项目目前不支持响应式客户端,例如 Spring WebClient,因此 Spring Cloud OpenFeign 也不支持。Spring中文文档

由于 Spring Cloud OpenFeign 项目现在被认为是功能完整的,因此即使它在上游项目中可用,我们也不打算添加支持。我们建议迁移到 Spring Interface 客户端。那里支持阻塞堆栈和反应式堆栈。Spring中文文档

在此之前,我们建议使用 feign-reactive 来支持 Spring WebClient。Spring中文文档

早期初始化错误

我们不鼓励在应用程序生命周期的早期阶段使用 Feign 客户端,同时处理配置和初始化 Bean。不支持在 Bean 初始化期间使用客户端。Spring中文文档

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

@Autowired
ObjectProvider<TestFeignClient> testFeignClient;

Spring Data 支持

如果 Jackson Databind 和 Spring Data Commons 在类路径上,则 和 的转换器将自动添加。org.springframework.data.domain.Pageorg.springframework.data.domain.SortSpring中文文档

禁用此行为集Spring中文文档

spring.cloud.openfeign.autoconfiguration.jackson.enabled=false

有关详细信息,请参阅。org.springframework.cloud.openfeign.FeignAutoConfiguration.FeignJacksonConfigurationSpring中文文档

Spring 支持@RefreshScope

如果启用了 Feign 客户端刷新,则使用以下命令创建每个 Feign 客户端:Spring中文文档

  • feign.Request.Options作为刷新范围的 Bean。这意味着可以针对任何 Feign 客户端实例刷新 和 等属性。connectTimeoutreadTimeoutSpring中文文档

  • 包装在 . 下的 url这意味着 Feign 客户端的 URL(如果已定义) with 属性,可以针对任何 Feign 客户端实例刷新。org.springframework.cloud.openfeign.RefreshableUrlspring.cloud.openfeign.client.config.{feignName}.urlSpring中文文档

您可以通过 刷新这些属性。POST /actuator/refreshSpring中文文档

默认情况下,Feign 客户端中的刷新行为处于禁用状态。使用以下属性启用刷新行为:Spring中文文档

spring.cloud.openfeign.client.refresh-enabled=true
不要用注释对界面进行注释。@FeignClient@RefreshScope

OAuth2 支持

可以通过将依赖项添加到项目并设置以下标志来启用 OAuth2 支持:spring-boot-starter-oauth2-clientSpring中文文档

spring.cloud.openfeign.oauth2.enabled=true

当标志设置为 true 并且存在 oauth2 客户机上下文资源详细信息时,将创建一个 class 的 bean。在每次请求之前,拦截器都会解析所需的访问令牌,并将其作为标头包含在内。 使用 to 获取 保存 .如果用户已使用该属性指定了 OAuth2,则该属性将用于检索令牌。如果未检索令牌或未指定令牌,则将使用从主机段检索到的令牌。OAuth2AccessTokenInterceptorOAuth2AccessTokenInterceptorOAuth2AuthorizedClientManagerOAuth2AuthorizedClientOAuth2AccessTokenclientRegistrationIdspring.cloud.openfeign.oauth2.clientRegistrationIdclientRegistrationIdserviceIdurlSpring中文文档

提示

使用 as OAuth2 客户端 registrationId 对于负载均衡的 Feign 客户端来说非常方便。对于非负载均衡的,基于属性是一种合适的方法。serviceIdclientRegistrationIdSpring中文文档

提示

如果您不想使用 的默认设置,则可以在配置中实例化此类型的 bean。OAuth2AuthorizedClientManagerSpring中文文档

转换负载均衡的 HTTP 请求

您可以使用所选内容来转换负载平衡的 HTTP 请求。ServiceInstanceSpring中文文档

对于 ,需要实现和定义 ,如下所示:RequestLoadBalancerFeignRequestTransformerSpring中文文档

@Bean
public LoadBalancerFeignRequestTransformer transformer() {
	return new LoadBalancerFeignRequestTransformer() {

		@Override
		public Request transformRequest(Request request, ServiceInstance instance) {
			Map<String, Collection<String>> headers = new HashMap<>(request.headers());
			headers.put("X-ServiceId", Collections.singletonList(instance.getServiceId()));
			headers.put("X-InstanceId", Collections.singletonList(instance.getInstanceId()));
			return Request.create(request.httpMethod(), request.url(), headers, request.body(), request.charset(),
					request.requestTemplate());
		}
	};
}

如果定义了多个转换器,则按照定义 Bean 的顺序应用它们。 或者,您可以使用来指定顺序。LoadBalancerFeignRequestTransformer.DEFAULT_ORDERSpring中文文档

X-Forwarded Headers 支持

X-Forwarded-Host可以通过设置以下标志来启用支持:X-Forwarded-ProtoSpring中文文档

spring.cloud.loadbalancer.x-forwarded.enabled=true

支持向假装客户端提供 URL 的方法

您可以通过以下任一方式向 Feign 客户端提供 URL:Spring中文文档

URL 在注释中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient", url="http://localhost:8081")Spring中文文档

URL 是从注释的属性解析的,没有负载平衡。urlSpring中文文档

URL 在注释和 配置属性。@FeignClientSpring中文文档

@FeignClient(name="testClient", url="http://localhost:8081")以及定义为application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081Spring中文文档

URL 是从注释的属性解析的,没有负载平衡。 配置属性中提供的 URL 保持未使用状态。urlSpring中文文档

URL 未在批注中提供,但在配置属性中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient")以及定义为application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081Spring中文文档

URL 从配置属性解析,不进行负载均衡。如果 ,则可以按照 Spring RefreshScope 支持中所述刷新配置属性中定义的 URL。spring.cloud.openfeign.client.refresh-enabled=trueSpring中文文档

URL 既不在批注中提供,也不在配置属性中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient")Spring中文文档

URL 从注解的属性解析,并具有负载均衡功能。nameSpring中文文档

AOT 和原生映像支持

Spring Cloud OpenFeign 支持 Spring AOT 转换和本机映像,但仅禁用刷新模式、禁用 Feign 客户端刷新(默认设置)和禁用性@FeignClient属性解析(默认设置)。Spring中文文档

如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保设置为 。spring.cloud.refresh.enabledfalse
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保未设置为 。spring.cloud.openfeign.client.refresh-enabledtrue
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保未设置为 。spring.cloud.openfeign.lazy-attributes-resolutiontrue
但是,如果通过属性设置该值,则可以通过运行带有标志的图像来覆盖该值。为了启用覆盖,还必须在构建时通过属性而不是属性设置值。url@FeignClienturl-Dspring.cloud.openfeign.client.config.[clientId].url=[url]url@FeignClient
要在 -annotated-classes 上使用注解,请确保指定客户端所在的位置,例如: 或显式列出它们:@EnableFeignClients@Configuration@EnableFeignClients(basePackages = "com.example.clients")@EnableFeignClients(clients = InventoryServiceFeignClient.class)
对于 Spring Cloud Contract 测试集成,应使用延迟属性解析。
FooConfiguration不需要用 进行注释。但是,如果是,请注意将其排除在包含此配置的任何内容之外,因为它将在指定时成为 、 、 等的默认源。这可以通过将其放在与任何 or 分开的、不重叠的包中来避免,也可以将其显式排除在 中。@Configuration@ComponentScanfeign.Decoderfeign.Encoderfeign.Contract@ComponentScan@SpringBootApplication@ComponentScan
使用注释的属性,以及更改名称 ensemble,它将覆盖客户端名称的别名 它将用作为该客户机创建的配置 Bean 名称的一部分。contextId@FeignClientApplicationContext
以前,使用该属性不需要该属性。现在需要使用。urlnamename
spring-cloud-starter-openfeign支持。但是,由于这是一个可选的依赖项,如果要使用它,则需要确保它已添加到项目中。spring-cloud-starter-loadbalancer
从 Spring Cloud OpenFeign 4 开始,不再支持 Feign Apache HttpClient 4。我们建议改用 Apache HttpClient 5。
默认情况下,Feign 客户端不对斜杠字符进行编码。您可以通过将 的值设置为 来更改此行为。/spring.cloud.openfeign.client.decodeSlashfalse
如果服务器未运行或不可用,则数据包会导致连接被拒绝。通信以错误消息或回退结束。如果它设置得很低,这可能会之前发生。执行查找和接收此类数据包所花费的时间是造成此延迟的很大一部分原因。它可能会根据涉及 DNS 查找的远程主机而更改。connectTimeout
在上面的示例中是默认配置 由 Spring Cloud OpenFeign 提供。FeignClientsConfiguration.class
PROD-SVC是客户将向其发出请求的服务的名称。
Feign 对象定义哪些注释和值在接口上有效。这 autowired bean 提供对 SpringMVC 注解的支持,而不是 默认的 Feign 本机注解。ContractContract
自 2020.0.2 起,断路器名称模式已从 更改为 。 使用 2020.0.4 中引入的断路器名称可以保留旧模式。<feignClientName>_<calledMethod>CircuitBreakerNameResolver
如果要切换回 Spring Cloud 之前使用的断路器名称 2022.0.0 您可以设置为 .spring.cloud.openfeign.circuitbreaker.alphanumeric-ids.enabledfalse
@FeignClient不应在服务器和客户端之间共享接口,并且不再支持使用 ON 类级别注释接口。@FeignClient@RequestMapping
由于 OkHttpClient 使用“透明”压缩(如果存在 or 标头,则禁用该压缩),因此当类路径上存在并设置为 时,我们不会启用压缩。content-encodingaccept-encodingfeign.okhttp.OkHttpClientspring.cloud.openfeign.okhttp.enabledtrue
如果您的应用程序已使用 Micrometer,则启用此功能就像放入类路径一样简单。feign-micrometer
spring.cloud.openfeign.micrometer.enabled=false禁用对所有 Feign 客户端的 Micrometer 支持,而不考虑客户端级标志的值:。 如果要启用或禁用每个客户端的 Micrometer 支持,请不要设置和使用 。spring.cloud.openfeign.client.config.feignName.micrometer.enabledspring.cloud.openfeign.micrometer.enabledspring.cloud.openfeign.client.config.feignName.micrometer.enabled
不要用注释对界面进行注释。@FeignClient@RefreshScope

URL 在注释中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient", url="http://localhost:8081")Spring中文文档

URL 是从注释的属性解析的,没有负载平衡。urlSpring中文文档

URL 在注释和 配置属性。@FeignClientSpring中文文档

@FeignClient(name="testClient", url="http://localhost:8081")以及定义为application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081Spring中文文档

URL 是从注释的属性解析的,没有负载平衡。 配置属性中提供的 URL 保持未使用状态。urlSpring中文文档

URL 未在批注中提供,但在配置属性中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient")以及定义为application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081Spring中文文档

URL 从配置属性解析,不进行负载均衡。如果 ,则可以按照 Spring RefreshScope 支持中所述刷新配置属性中定义的 URL。spring.cloud.openfeign.client.refresh-enabled=trueSpring中文文档

URL 既不在批注中提供,也不在配置属性中提供。@FeignClientSpring中文文档

@FeignClient(name="testClient")Spring中文文档

URL 从注解的属性解析,并具有负载均衡功能。nameSpring中文文档

如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保设置为 。spring.cloud.refresh.enabledfalse
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保未设置为 。spring.cloud.openfeign.client.refresh-enabledtrue
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保未设置为 。spring.cloud.openfeign.lazy-attributes-resolutiontrue
但是,如果通过属性设置该值,则可以通过运行带有标志的图像来覆盖该值。为了启用覆盖,还必须在构建时通过属性而不是属性设置值。url@FeignClienturl-Dspring.cloud.openfeign.client.config.[clientId].url=[url]url@FeignClient

配置属性

要查看所有 Spring Cloud OpenFeign 相关配置属性的列表,请查看附录页面Spring中文文档