对于最新的稳定版本,请使用 Spring Cloud OpenFeign 4.3.0spring-doc.cadn.net.cn

Spring Cloud OpenFeign 功能

声明式 REST 客户端:假装

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

如何包含佯装

要将 Feign 包含在您的项目中,请使用带有组的Startersorg.springframework.cloud和工件 IDspring-cloud-starter-openfeign.有关使用当前 Spring Cloud Release Train 设置构建系统的详细信息,请参阅 Spring Cloud 项目页面spring-doc.cadn.net.cn

示例 spring boot 应用程序spring-doc.cadn.net.cn

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

@FeignClient注释 String 值(上面的“stores”)是一个任意客户端名称,用于创建 Spring Cloud LoadBalancer 客户端。 您还可以使用url属性 (绝对值或只是主机名)。中 bean 的名称 应用程序上下文是接口的完全限定名称。 要指定自己的别名值,可以使用qualifiers价值 的@FeignClient注解。spring-doc.cadn.net.cn

上面的负载均衡器客户端将希望发现物理地址 用于“商店”服务。如果您的应用程序是 Eureka 客户端,则 它将解析 Eureka 服务注册表中的服务。如果你 不想使用 Eureka,可以配置服务器列表 在外部配置中使用SimpleDiscoveryClient.spring-doc.cadn.net.cn

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

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

为了在多模块设置中加载 Spring Feign 客户端 bean,您需要直接指定包。spring-doc.cadn.net.cn

因为FactoryBean对象可以在初始上下文刷新发生之前实例化,并且 Spring Cloud OpenFeign 客户端的实例化会触发上下文刷新,它们不应在FactoryBean类。

属性解析模式

在创建时Feignclient bean,我们解析通过@FeignClient注解。截至4.x,这些价值观正在急切地得到解决。对于大多数用例来说,这是一个很好的解决方案,并且它还允许 AOT 支持。spring-doc.cadn.net.cn

如果您需要延迟解析属性,请将spring.cloud.openfeign.lazy-attributes-resolution属性值设置为true.spring-doc.cadn.net.cn

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

覆盖假缺值

Spring Cloud 的 Feign 支持中的一个核心概念是命名客户端。每个 Feign 客户端都是组件集合的一部分,这些组件协同工作以按需联系远程服务器,并且集合具有一个名称,您作为应用程序开发人员使用@FeignClient注解。Spring Cloud 创建一个新的集成作为ApplicationContext按需为每个命名客户端使用FeignClientsConfiguration.这包含(除其他外)一个feign.Decoder一个feign.Encoder和一个feign.Contract. 可以使用contextId属性的@FeignClient注解。spring-doc.cadn.net.cn

Spring Cloud 允许您通过声明其他配置(在FeignClientsConfiguration) 使用@FeignClient.例:spring-doc.cadn.net.cn

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

在这种情况下,客户端由已有的组件组成FeignClientsConfiguration与任何FooConfiguration(后者将覆盖前者)。spring-doc.cadn.net.cn

FooConfiguration不需要用@Configuration.但是,如果是,请注意将其从任何@ComponentScan否则将包括此配置,因为它将成为feign.Decoder,feign.Encoder,feign.Contract,等,如果指定。这可以通过将其放在一个单独的、不重叠的包中来避免@ComponentScan@SpringBootApplication,或者可以在@ComponentScan.
contextId属性的@FeignClient注释,除了更改 这ApplicationContextensemble,它将覆盖客户端名称的别名 它将用作为该客户机创建的配置 Bean 名称的一部分。
以前,使用url属性,不需要name属性。用name现在是必需的。

占位符在nameurl属性。spring-doc.cadn.net.cn

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

Spring Cloud OpenFeign 默认为 feign (BeanTypebean名称:ClassName):spring-doc.cadn.net.cn

  • Decoder假装解码器:ResponseEntityDecoder(它将SpringDecoder)spring-doc.cadn.net.cn

  • EncoderfeignEncoder:SpringEncoderspring-doc.cadn.net.cn

  • LoggerfeignLogger:Slf4jLoggerspring-doc.cadn.net.cn

  • MicrometerObservationCapabilitymicrometerObservationCapability:如果feign-micrometer在类路径上,并且ObservationRegistry可用spring-doc.cadn.net.cn

  • MicrometerCapability千分尺能力:如果feign-micrometer在类路径上,MeterRegistry可用,并且ObservationRegistry不可用spring-doc.cadn.net.cn

  • CachingCapability缓存能力:如果@EnableCaching注释。可以通过spring.cloud.openfeign.cache.enabled.spring-doc.cadn.net.cn

  • ContractfeignContract:SpringMvcContractspring-doc.cadn.net.cn

  • Feign.BuilderfeignBuilder 的:FeignCircuitBreaker.Builderspring-doc.cadn.net.cn

  • ClientfeignClient:如果 Spring Cloud LoadBalancer 在类路径上,FeignBlockingLoadBalancerClient被使用。 如果它们都不在类路径上,则使用默认的 Feign 客户端。spring-doc.cadn.net.cn

spring-cloud-starter-openfeign支持spring-cloud-starter-loadbalancer.但是,作为可选依赖项,如果您想使用它,您需要确保它已添加到您的项目中。

要使用 OkHttpClient 支持的 Feign 客户端和 Http2Client Feign 客户端,请确保要使用的客户端位于类路径上,并将spring.cloud.openfeign.okhttp.enabledspring.cloud.openfeign.http2client.enabledtrue分别。spring-doc.cadn.net.cn

当涉及到 Apache HttpClient 5 支持的 Feign 客户端时,确保 HttpClient 5 在类路径上就足够了,但您仍然可以通过设置spring.cloud.openfeign.httpclient.hc5.enabledfalse. 您可以通过提供 bean 来自定义使用的 HTTP 客户端org.apache.hc.client5.http.impl.classic.CloseableHttpClient使用 Apache HC5 时。spring-doc.cadn.net.cn

您可以通过在spring.cloud.openfeign.httpclient.xxx性能。那些前缀为httpclient适用于所有客户端,前缀为httpclient.hc5到 Apache HttpClient 5,前缀为httpclient.okhttp设置为 OkHttpClient 和前缀为httpclient.http2到 Http2Client。您可以在附录中找到可以自定义的属性的完整列表。 如果您无法使用属性配置 Apache HttpClient 5,则有一个HttpClient5FeignConfiguration.HttpClientBuilderCustomizer用于编程配置的接口。spring-doc.cadn.net.cn

Apache HTTP 组件5.4已更改 HttpClient 中与 HTTP/1.1 TLS 升级相关的默认值。大多数代理服务器处理升级都没有问题,但是,您可能会遇到 Envoy 或 Istio 的问题。如果您需要恢复以前的行为,您可以使用HttpClient5FeignConfiguration.HttpClientBuilderCustomizer执行此作,如下例所示。
@Configuration
public class FooConfiguration {

	@Bean
	public HttpClient5FeignConfiguration.HttpClientBuilderCustomizer httpClientBuilder() {
		return (httpClientBuilder) -> {
			RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
			requestConfigBuilder.setProtocolUpgradeEnabled(false);
			httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
		};
	}
}
从 Spring Cloud OpenFeign 4 开始,不再支持 Feign Apache HttpClient 4。我们建议改用 Apache HttpClient 5。

Spring Cloud OpenFeign 默认情况下为 feign 提供以下 bean,但仍会从应用程序上下文中查找这些类型的 bean 以创建 Feign 客户端:spring-doc.cadn.net.cn

一颗豆子Retryer.NEVER_RETRY与类型Retryer默认情况下创建,这将禁用重试。 请注意,此重试行为与假装默认行为不同,后者将自动重试 IOExceptions, 将它们视为与网络相关的暂时性异常,以及从 ErrorDecoder 引发的任何 RetryableException。spring-doc.cadn.net.cn

创建其中一种类型的 bean 并将其放置在@FeignClient配置(例如FooConfigurationabove) 允许您覆盖所描述的每个 bean。例:spring-doc.cadn.net.cn

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

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

这将替换SpringMvcContractfeign.Contract.Default并添加一个RequestInterceptor到收藏RequestInterceptor.spring-doc.cadn.net.cn

@FeignClient也可以使用配置属性进行配置。spring-doc.cadn.net.cn

application.ymlspring-doc.cadn.net.cn

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在此示例中,引用@FeignClient value,也以@FeignClient name@FeignClient contextId.在负载平衡方案中,它还对应于serviceId将用于检索实例的服务器应用程序。解码器,重试器和其他类的指定类必须在Spring上下文中具有bean或具有默认构造函数。spring-doc.cadn.net.cn

默认配置可以在@EnableFeignClients属性defaultConfiguration以与上述类似的方式。不同之处在于,此配置将适用于所有 Feign 客户端。spring-doc.cadn.net.cn

如果您更喜欢使用配置属性来配置所有@FeignClient,您可以使用default假名。spring-doc.cadn.net.cn

您可以使用spring.cloud.openfeign.client.config.feignName.defaultQueryParametersspring.cloud.openfeign.client.config.feignName.defaultRequestHeaders指定将与名为feignName.spring-doc.cadn.net.cn

application.ymlspring-doc.cadn.net.cn

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

如果我们同时创建@Configurationbean 和配置属性,配置属性将获胜。 它将覆盖@Configuration值。但是,如果要将优先级更改为@Configuration, 您可以更改spring.cloud.openfeign.client.default-to-propertiesfalse.spring-doc.cadn.net.cn

如果我们想创建多个具有相同名称或 url 的 Feign 客户端 这样它们就会指向同一台服务器,但每个服务器都有不同的自定义配置 我们必须使用contextId属性的@FeignClient为了避免名字 这些配置 bean 的冲突。spring-doc.cadn.net.cn

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

也可以将 FeignClient 配置为不从父上下文继承 bean。 您可以通过覆盖inheritParentConfiguration()FeignClientConfigurer要返回的豆子false:spring-doc.cadn.net.cn

@Configuration
public class CustomConfiguration {
	@Bean
	public FeignClientConfigurer feignClientConfigurer() {
		return new FeignClientConfigurer() {
			@Override
			public boolean inheritParentConfiguration() {
				 return false;
			}
		};
	}
}
默认情况下,伪装客户端不对斜杠字符进行编码。您可以通过将/spring.cloud.openfeign.client.decode-slashfalse.
默认情况下,伪装客户端不会从请求路径中删除尾随斜杠字符。 您可以通过将/spring.cloud.openfeign.client.remove-trailing-slashtrue. 从请求路径中删除尾随斜杠将在下一个主要版本中成为默认行为。

SpringEncoder配置

SpringEncoder我们提供,我们设置null二进制内容类型的字符集和UTF-8对于所有其他的。spring-doc.cadn.net.cn

您可以修改此行为以从Content-Typeheader charset 而不是通过设置spring.cloud.openfeign.encoder.charset-from-content-typetrue.spring-doc.cadn.net.cn

超时处理

我们可以在默认客户端和命名客户端上配置超时。OpenFeign 使用两个超时参数:spring-doc.cadn.net.cn

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

手动创建伪造客户端

在某些情况下,可能需要以一种不使用上述方法可以。在这种情况下,您可以使用 Feign Builder API 创建客户端。下面是一个例子它创建了两个具有相同接口的 Feign 客户端,但每个客户端都配置为一个单独的请求拦截器。spring-doc.cadn.net.cn

@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");
	}
}
在上面的示例中FeignClientsConfiguration.class是默认配置由 Spring Cloud OpenFeign 提供。
PROD-SVC是客户端将向其发出请求的服务的名称。
佯装Contract对象定义了哪些注释和值在接口上有效。 这 自动接线Contractbean 提供了对 SpringMVC 注解的支持,而不是默认的 Feign 原生注解。不建议将 Spring MVC 注解和原生 Feign 注解混合在一起。

您还可以使用Builder`to configure FeignClient not to inherit beans from the parent context. You can do this by overriding calling `inheritParentContext(false)Builder.spring-doc.cadn.net.cn

假装 Spring Cloud CircuitBreaker 支持

如果 Spring Cloud CircuitBreaker 位于类路径上,并且spring.cloud.openfeign.circuitbreaker.enabled=true,Feign 将用断路器包裹所有方法。spring-doc.cadn.net.cn

要在每个客户端的基础上禁用 Spring Cloud CircuitBreaker 支持,请创建一个 vanillaFeign.Builder使用“原型”范围,例如:spring-doc.cadn.net.cn

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

断路器名称遵循此模式<feignClientClassName>#<calledMethod>(<parameterTypes>).调用@FeignClientFooClient接口,并且没有参数的被调用接口方法是bar那么断路器名称将是FooClient#bar().spring-doc.cadn.net.cn

从 2020.0.2 开始,断路器名称模式已从<feignClientName>_<calledMethod>. 用CircuitBreakerNameResolver在 2020.0.4 中引入的断路器名称可以保留旧模式。

提供 beanCircuitBreakerNameResolver,您可以更改断路器名称模式。spring-doc.cadn.net.cn

@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.enabled属性设置为true(默认情况下false).spring-doc.cadn.net.cn

使用配置属性配置断路器

您可以通过配置属性配置断路器。spring-doc.cadn.net.cn

例如,如果你有这个 Feign 客户端spring-doc.cadn.net.cn

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

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

您可以通过执行以下作使用配置属性来配置它spring-doc.cadn.net.cn

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 支持回退的概念:当电路打开或出现错误时执行的默认代码路径。为给定的@FeignClientfallback属性添加到实现回退的类名。您还需要将您的实现声明为 Spring bean。spring-doc.cadn.net.cn

@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属性@FeignClient.spring-doc.cadn.net.cn

@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 回退一起使用时,在ApplicationContext相同类型的。这将导致@Autowired无法工作,因为没有一个 bean,或者一个标记为主 bean。为了解决这个问题,Spring Cloud OpenFeign 将所有 Feign 实例标记为@Primary,因此 Spring Framework 将知道要注入哪个 bean。在某些情况下,这可能是不可取的。要关闭此行为,请将primary属性@FeignClient设置为 false。spring-doc.cadn.net.cn

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

假继承支持

Feign 通过单继承接口支持样板 API。 这允许将常见作分组到方便的基本接口中。spring-doc.cadn.net.cn

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接口不应在服务器和客户端之间共享,并注释@FeignClient接口@RequestMapping不再支持类级别。

假请求/响应压缩

您可以考虑为您的 假装请求。您可以通过启用以下属性之一来执行此作:spring-doc.cadn.net.cn

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

伪造请求压缩为您提供的设置类似于您可能为 Web 服务器设置的设置:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

当请求与spring.cloud.openfeign.compression.request.mime-types和尺寸设置在spring.cloud.openfeign.compression.request.min-request-size,spring.cloud.openfeign.compression.request.enabled=true导致将压缩标头添加到请求中。 标头的功能是向服务器发出信号,表明客户端需要压缩的正文。服务器端应用程序负责根据客户端提供的标头提供压缩正文。spring-doc.cadn.net.cn

由于 OkHttpClient 使用“透明”压缩,因此如果content-encodingaccept-encoding标头存在,当feign.okhttp.OkHttpClient存在于类路径上,并且spring.cloud.openfeign.okhttp.enabled设置为true.

假日志记录

为创建的每个 Feign 客户端创建一个记录器。默认情况下,记录器的名称是用于创建 Feign 客户端的接口的完整类名。Feign 日志记录仅响应DEBUG水平。spring-doc.cadn.net.cn

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

Logger.Level您可以为每个客户端配置的对象,告诉 Feign 要记录多少。选择是:spring-doc.cadn.net.cn

例如,以下命令将设置Logger.LevelFULL:spring-doc.cadn.net.cn

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

假装能力支持

Feign 功能公开核心 Feign 组件,以便可以修改这些组件。例如,这些功能可以采用Client装饰它,并将装饰后的实例交还给 Feign。对 Micrometer 的支持就是一个很好的现实示例。请参阅 Micrometer 支持spring-doc.cadn.net.cn

创建一个或多个Capability豆子并将它们放在@FeignClient配置允许您注册它们并修改相关客户端的行为。spring-doc.cadn.net.cn

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

千分尺支架

如果满足以下所有条件,则MicrometerObservationCapabilitybean 被创建和注册,以便 Micrometer 可以观察到您的 Feign 客户端:spring-doc.cadn.net.cn

如果您的应用程序已经使用 Micrometer,则启用此功能就像将feign-micrometer到你的类路径上。

您还可以通过以下任一方式禁用该功能:spring-doc.cadn.net.cn

spring.cloud.openfeign.micrometer.enabled=false禁用对所有 Feign 客户端的 Micrometer 支持,而不管客户端级标志的值如何:spring.cloud.openfeign.client.config.feignName.micrometer.enabled. 如果要为每个客户端启用或禁用 Micrometer 支持,请不要将spring.cloud.openfeign.micrometer.enabled并使用spring.cloud.openfeign.client.config.feignName.micrometer.enabled.

您还可以自定义MicrometerObservationCapability通过注册您自己的 Bean:spring-doc.cadn.net.cn

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

仍然可以使用MicrometerCapability使用 Feign(仅支持指标)时,您需要禁用千分尺支持(spring.cloud.openfeign.micrometer.enabled=false)并创建一个MicrometerCapability豆:spring-doc.cadn.net.cn

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

假缓存

如果@EnableCaching注释,则CachingCapabilitybean 被创建和注册,以便您的 Feign 客户端识别@Cache*其界面上的注释:spring-doc.cadn.net.cn

public interface DemoClient {

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

您还可以通过属性禁用该功能spring.cloud.openfeign.cache.enabled=false.spring-doc.cadn.net.cn

Spring @RequestMapping支持

Spring Cloud OpenFeign 为 Spring 提供了支持@RequestMapping注释及其派生注释(例如@GetMapping,@PostMapping等)支持。 上的属性@RequestMapping注释(包括value,method,params,headers,consumesproduces)由SpringMvcContract作为请求的内容。spring-doc.cadn.net.cn

请考虑以下示例:spring-doc.cadn.net.cn

使用params属性。spring-doc.cadn.net.cn

@FeignClient("demo")
public interface DemoTemplate {

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

在上面的示例中,请求 url 被解析为/stores/storeId?mode=upsert.
params 属性还支持使用多个
key=value或只有一个key:
spring-doc.cadn.net.cn

  • 什么时候params = { "key1=v1", "key2=v2" },则请求 URL 被解析为/stores/storeId?key1=v1&key2=v2.spring-doc.cadn.net.cn

  • 什么时候params = "key",则请求 URL 被解析为/stores/storeId?key.spring-doc.cadn.net.cn

假装@QueryMap支持

Spring Cloud OpenFeign 提供了等效的@SpringQueryMap注释,其中 用于将 POJO 或 Map 参数作为查询参数映射进行注释。spring-doc.cadn.net.cn

例如,Params类定义参数param1param2:spring-doc.cadn.net.cn

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

	// [Getters and setters omitted for brevity]
}

以下 Feign 客户端使用Params类,使用@SpringQueryMap注解:spring-doc.cadn.net.cn

@FeignClient("demo")
public interface DemoTemplate {

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

如果您需要对生成的查询参数映射进行更多控制,您可以实现自定义QueryMapEncoder豆。spring-doc.cadn.net.cn

HATEOAS 支持

Spring提供了一些API来创建遵循HATEOAS原则的REST表示,Spring HateoasSpring Data REST。spring-doc.cadn.net.cn

如果您的项目使用org.springframework.boot:spring-boot-starter-hateoas起动机 或org.springframework.boot:spring-boot-starter-data-reststarter,默认启用假装 HATEOAS 支持。spring-doc.cadn.net.cn

启用 HATEOAS 支持后,允许 Feign 客户端序列化 并反序列化 HATEOAS 表示模型:EntityModelCollectionModelPagedModelspring-doc.cadn.net.cn

@FeignClient("demo")
public interface DemoTemplate {

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

Spring @MatrixVariable支持

Spring Cloud OpenFeign 为 Spring 提供了支持@MatrixVariable注解。spring-doc.cadn.net.cn

如果将映射作为方法参数传递,则@MatrixVariable路径段是通过将映射中的键值对与 .=spring-doc.cadn.net.cn

如果传递了不同的对象,则name@MatrixVariable注释(如果已定义)或注释的变量名称为 使用 与提供的方法参数连接。=spring-doc.cadn.net.cn

重要

尽管在服务器端,Spring 不要求用户将路径段占位符命名为与矩阵变量名称相同的名称,但由于在客户端会过于模糊,Spring Cloud OpenFeign 要求您添加一个路径段占位符,其名称与name@MatrixVariable注释(如果已定义)或带注释的变量名称。spring-doc.cadn.net.cn

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

请注意,变量名称和路径段占位符都调用matrixVars.spring-doc.cadn.net.cn

@FeignClient("demo")
public interface DemoTemplate {

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

CollectionFormat支持

我们支持feign.CollectionFormat通过提供@CollectionFormat注解。 您可以通过传递所需的feign.CollectionFormat作为注释值。spring-doc.cadn.net.cn

在以下示例中,CSVformat 而不是默认的EXPLODED处理方法。spring-doc.cadn.net.cn

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

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

}

响应式支持

在 Spring Cloud OpenFeign 积极开发时,OpenFeign 项目不支持响应式客户端,例如 Spring WebClient,此类支持也无法添加到 Spring Cloud OpenFeign 中。spring-doc.cadn.net.cn

由于 Spring Cloud OpenFeign 项目现在被认为是功能完整的,因此即使它在上游项目中可用,我们也不打算添加支持。我们建议迁移到 Spring Interface Clients。那里支持阻塞和响应式堆栈。spring-doc.cadn.net.cn

早期初始化错误

我们不鼓励在应用程序生命周期的早期阶段使用 Feign 客户端,同时处理配置和初始化 Bean。不支持在 Bean 初始化期间使用客户端。spring-doc.cadn.net.cn

同样,根据您使用 Feign 客户端的方式,您可能会在启动应用程序时看到初始化错误。要解决此问题,您可以使用ObjectProvider自动接线客户端时。spring-doc.cadn.net.cn

@Autowired
ObjectProvider<TestFeignClient> testFeignClient;

Spring 数据支持

如果 Jackson Databind 和 Spring Data Commons 在类路径上,则org.springframework.data.domain.Pageorg.springframework.data.domain.Sort将自动添加。spring-doc.cadn.net.cn

要禁用此行为,请设置spring-doc.cadn.net.cn

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

org.springframework.cloud.openfeign.FeignAutoConfiguration.FeignJacksonConfiguration了解详情。spring-doc.cadn.net.cn

Spring@RefreshScope支持

如果启用了 Feign 客户端刷新,则每个 Feign 客户端都使用:spring-doc.cadn.net.cn

  • feign.Request.Options作为刷新作用域的 bean。这意味着connectTimeoutreadTimeout可以针对任何 Feign 客户端实例刷新。spring-doc.cadn.net.cn

  • 包装在org.springframework.cloud.openfeign.RefreshableUrl. 这意味着 Feign 客户端的 URL(如果已定义) 跟spring.cloud.openfeign.client.config.{feignName}.url属性,可以针对任何 Feign 客户端实例刷新。spring-doc.cadn.net.cn

您可以通过以下方式刷新这些属性POST /actuator/refresh.spring-doc.cadn.net.cn

默认情况下,伪装客户端中的刷新行为处于禁用状态。使用以下属性启用刷新行为:spring-doc.cadn.net.cn

spring.cloud.openfeign.client.refresh-enabled=true
不要对@FeignClient接口与@RefreshScope注解。

OAuth2 支持

可以通过添加spring-boot-starter-oauth2-client依赖项目并设置以下标志:spring-doc.cadn.net.cn

spring.cloud.openfeign.oauth2.enabled=true

当标志设置为 true 并且存在 oauth2 客户端上下文资源详细信息时,类为OAuth2AccessTokenInterceptor被创建。在每次请求之前,拦截器都会解析所需的访问Tokens并将其作为标头包含在内。OAuth2AccessTokenInterceptor使用OAuth2AuthorizedClientManager获取OAuth2AuthorizedClientOAuth2AccessToken.如果用户指定了 OAuth2clientRegistrationId使用spring.cloud.openfeign.oauth2.clientRegistrationId属性,它将用于检索Tokens。如果未检索Tokens或clientRegistrationId尚未指定,则serviceIdurl主机段。spring-doc.cadn.net.cn

提示

使用serviceId由于 OAuth2 客户端 registrationId 对于负载均衡的 Feign 客户端来说很方便。对于非负载平衡的,基于属性的clientRegistrationId是一个合适的方法。spring-doc.cadn.net.cn

提示

如果您不想使用OAuth2AuthorizedClientManager,您可以在配置中实例化这种类型的 bean。spring-doc.cadn.net.cn

转换负载均衡的 HTTP 请求

您可以使用所选的ServiceInstance转换负载平衡的 HTTP 请求。spring-doc.cadn.net.cn

Request,您需要实现和定义LoadBalancerFeignRequestTransformer如下:spring-doc.cadn.net.cn

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

如果定义了多个 transformer,则按照定义 bean 的顺序应用它们。 或者,您可以使用LoadBalancerFeignRequestTransformer.DEFAULT_ORDER以指定顺序。spring-doc.cadn.net.cn

X-Forwarded 标头支持

X-Forwarded-HostX-Forwarded-Proto可以通过设置以下标志来启用支持:spring-doc.cadn.net.cn

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

向伪造客户端提供 URL 的支持方式

您可以通过以下任一方式向 Feign 客户端提供 URL:spring-doc.cadn.net.cn

示例

URL 在@FeignClient注解。spring-doc.cadn.net.cn

@FeignClient(name="testClient", url="http://localhost:8081")spring-doc.cadn.net.cn

URL 是从url属性,无需负载平衡。spring-doc.cadn.net.cn

URL 在@FeignClient注释和配置属性。spring-doc.cadn.net.cn

@FeignClient(name="testClient", url="http://localhost:8081")以及application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081spring-doc.cadn.net.cn

URL 是从url属性,无需负载平衡。 配置属性中提供的 URL 仍未使用。spring-doc.cadn.net.cn

URL 未在@FeignClient注释,但在配置属性中提供。spring-doc.cadn.net.cn

@FeignClient(name="testClient")以及application.ymlspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081spring-doc.cadn.net.cn

URL 是从配置属性解析的,没有负载平衡。 如果spring.cloud.openfeign.client.refresh-enabled=true,则可以按照 Spring RefreshScope Support 中所述刷新配置属性中定义的 URL。spring-doc.cadn.net.cn

URL 中均未提供@FeignClient注释,也不在配置属性中。spring-doc.cadn.net.cn

@FeignClient(name="testClient")spring-doc.cadn.net.cn

URL 从name属性,具有负载平衡功能。spring-doc.cadn.net.cn

AOT 和原生映像支持

Spring Cloud OpenFeign 支持 Spring AOT 转换和本机映像,但是,仅禁用刷新模式,禁用 Feign 客户端刷新(默认设置)和懒惰@FeignClient属性解析disabled(默认设置)。spring-doc.cadn.net.cn

如果要在 AOT 或原生镜像模式下运行 Spring Cloud OpenFeign 客户端,请确保将spring.cloud.refresh.enabledfalse.
如果要在AOT或原生镜像模式下运行Spring Cloud OpenFeign客户端,请确保spring.cloud.openfeign.client.refresh-enabled尚未设置为true.
如果要在AOT或原生镜像模式下运行Spring Cloud OpenFeign客户端,请确保spring.cloud.openfeign.lazy-attributes-resolution尚未设置为true.
但是,如果您将url值,则可以覆盖@FeignClient url值,通过使用-Dspring.cloud.openfeign.client.config.[clientId].url=[url]旗。 为了启用覆盖,一个urlvalue 也必须通过属性设置,而不是@FeignClient属性。

配置属性

要查看所有 Spring Cloud OpenFeign 相关配置属性的列表,请查看附录页面spring-doc.cadn.net.cn