声明式 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 客户端。HttpMessageConverters
如何包括假装
要将 Feign 包含在项目中,请使用带有 group 和 artifact id 的 starter。有关使用当前 Spring Cloud 发布系列设置构建系统的详细信息,请参阅 Spring Cloud 项目页面。org.springframework.cloud
spring-cloud-starter-openfeign
示例 Spring Boot 应用
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@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 的名称
应用程序上下文是接口的完全限定名称。
若要指定自己的别名值,可以使用该值
的注释。@FeignClient
url
qualifiers
@FeignClient
上面的负载均衡器客户端将希望发现物理地址
对于“商店”服务。如果你的应用程序是 Eureka 客户端,那么
它将解析 Eureka 服务注册表中的服务。如果你
不想使用 Eureka,您可以配置服务器列表
在外部配置中使用 SimpleDiscoveryClient
。
Spring Cloud OpenFeign 支持 Spring Cloud LoadBalancer 阻塞模式的所有可用功能。您可以在项目文档中阅读有关它们的更多信息。
要在 -annotated-classes 上使用注解,请确保指定客户端所在的位置,例如: 或显式列出它们:@EnableFeignClients @Configuration @EnableFeignClients(basePackages = "com.example.clients") @EnableFeignClients(clients = InventoryServiceFeignClient.class)
|
覆盖假装默认值
Spring Cloud 的 Feign 支持的一个核心概念是命名客户端。每个假装客户端都是组件集合的一部分,这些组件协同工作以按需联系远程服务器,并且该集合具有您作为应用程序开发人员使用注释为其指定的名称。Spring Cloud 使用 .它包含(除其他外)an , a 和 a 。
可以使用注释的属性覆盖该集成的名称。@FeignClient
ApplicationContext
FeignClientsConfiguration
feign.Decoder
feign.Encoder
feign.Contract
contextId
@FeignClient
Spring Cloud 允许您通过声明其他配置(在 之上)来完全控制假装客户端。例:FeignClientsConfiguration
@FeignClient
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
在这种情况下,客户端由已经包含的组件和任何 in 组成(后者将覆盖前者)。FeignClientsConfiguration
FooConfiguration
FooConfiguration 不需要用 进行注释。但是,如果是,请注意将其排除在包含此配置的任何内容之外,因为它将在指定时成为 、 、 等的默认源。这可以通过将其放在与任何 or 分开的、不重叠的包中来避免,也可以将其显式排除在 中。@Configuration @ComponentScan feign.Decoder feign.Encoder feign.Contract @ComponentScan @SpringBootApplication @ComponentScan |
使用注释的属性,以及更改名称
ensemble,它将覆盖客户端名称的别名
它将用作为该客户机创建的配置 Bean 名称的一部分。contextId @FeignClient ApplicationContext |
以前,使用该属性不需要该属性。现在需要使用。url name name |
和 属性支持占位符。name
url
@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {
//..
}
Spring Cloud OpenFeign 默认为 feign ( beanName: ) 提供以下 bean :BeanType
ClassName
-
Decoder
feignDecoder:(包装一个ResponseEntityDecoder
SpringDecoder
) -
Encoder
feignEncoder:SpringEncoder
-
Logger
feignLogger:Slf4jLogger
-
MicrometerObservationCapability
micrometerObservationCapability:如果在类路径上并且可用feign-micrometer
ObservationRegistry
-
MicrometerCapability
micrometerCapability:如果在类路径上,则可用和不可用feign-micrometer
MeterRegistry
ObservationRegistry
-
CachingCapability
cachingCapability:如果使用了注释。可以通过 禁用。@EnableCaching
spring.cloud.openfeign.cache.enabled
-
Contract
假装合同:SpringMvcContract
-
Feign.Builder
feignBuilder:FeignCircuitBreaker.Builder
-
Client
feignClient:如果 Spring Cloud LoadBalancer 在类路径上,则使用 feignClient。 如果它们都不在类路径上,则使用默认的假装客户端。FeignBlockingLoadBalancerClient
spring-cloud-starter-openfeign 支持。但是,由于这是一个可选的依赖项,如果要使用它,则需要确保它已添加到项目中。spring-cloud-starter-loadbalancer |
若要使用 OkHttpClient 支持的 Feign 客户端和 Http2Client Feign 客户端,请确保要使用的客户端位于类路径上,并分别设置为 或 to。spring.cloud.openfeign.okhttp.enabled
spring.cloud.openfeign.http2client.enabled
true
对于支持 Apache HttpClient 5 的 Feign 客户端,只需确保 HttpClient 5 位于类路径上就足够了,但您仍然可以通过设置为 来禁用其对 Feign 客户端的使用。
您可以通过在使用 Apache HC5 时提供其中任何一个的 Bean 来自定义所使用的 HTTP 客户端。spring.cloud.openfeign.httpclient.hc5.enabled
false
org.apache.hc.client5.http.impl.classic.CloseableHttpClient
您可以通过在属性中设置值来进一步自定义 http 客户端。仅以 为前缀的客户端适用于所有客户端,以 Apache HttpClient 5 为前缀的客户端,以 OkHttpClient 为前缀的客户端,以 Http2Client 为前缀的客户端。您可以在附录中找到可以自定义的属性的完整列表。
如果无法使用属性配置 Apache HttpClient 5,则有一个用于编程配置的接口。spring.cloud.openfeign.httpclient.xxx
httpclient
httpclient.hc5
httpclient.okhttp
httpclient.http2
HttpClientBuilderCustomizer
从 Spring Cloud OpenFeign 4 开始,不再支持 Feign Apache HttpClient 4。我们建议改用 Apache HttpClient 5。 |
Spring Cloud OpenFeign 默认不为 feign 提供以下 bean,但仍然从应用程序上下文中查找这些类型的 bean 以创建 feign 客户端:
-
Logger.Level
-
Retryer
-
ErrorDecoder
-
Request.Options
-
Collection<RequestInterceptor>
-
SetterFactory
-
QueryMapEncoder
-
Capability
(MicrometerObservationCapability
并且默认提供)CachingCapability
缺省情况下,将创建具有该类型的 Bean,这将禁用重试。
请注意,此重试行为与 Feign 默认行为不同,在 Feign 默认行为中,它将自动重试 IOExceptions,
将它们视为暂时性网络相关异常,以及从 ErrorDecoder 引发的任何 RetryableException。Retryer.NEVER_RETRY
Retryer
创建其中一种类型的 Bean 并将其放置在配置中(如上所示)允许您覆盖所描述的每个 Bean。例:@FeignClient
FooConfiguration
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
}
这将替换 with 并将 a 添加到 的集合中。SpringMvcContract
feign.Contract.Default
RequestInterceptor
RequestInterceptor
@FeignClient
也可以使用配置属性进行配置。
application.yml
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 或具有默认构造函数。@FeignClient
value
@FeignClient
name
@FeignClient
contextId
serviceId
默认配置可以在属性中以与上述类似的方式指定。不同之处在于,此配置将应用于所有假装客户端。@EnableFeignClients
defaultConfiguration
如果您更喜欢使用配置属性来配置所有 ,则可以使用假名创建配置属性。@FeignClient
default
您可以使用 和 指定将与名为 的客户端的每个请求一起发送的查询参数和标头。spring.cloud.openfeign.client.config.feignName.defaultQueryParameters
spring.cloud.openfeign.client.config.feignName.defaultRequestHeaders
feignName
application.yml
spring:
cloud:
openfeign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
如果我们同时创建 Bean 和配置属性,则配置属性将获胜。
它将覆盖值。但是如果要将优先级更改为 ,
您可以更改为 .@Configuration
@Configuration
@Configuration
spring.cloud.openfeign.client.default-to-properties
false
如果我们想创建多个具有相同名称或 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 {
//..
}
也可以将 FeignClient 配置为不从父上下文继承 bean。
为此,您可以重写 in a bean 以返回:inheritParentConfiguration()
FeignClientConfigurer
false
@Configuration
public class CustomConfiguration {
@Bean
public FeignClientConfigurer feignClientConfigurer() {
return new FeignClientConfigurer() {
@Override
public boolean inheritParentConfiguration() {
return false;
}
};
}
}
默认情况下,Feign 客户端不对斜杠字符进行编码。您可以通过将 的值设置为 来更改此行为。/ spring.cloud.openfeign.client.decodeSlash false |
超时处理
我们可以在默认客户端和命名客户端上配置超时。OpenFeign 使用两个超时参数:
-
connectTimeout
防止由于服务器处理时间过长而阻止调用方。 -
readTimeout
从建立连接时开始应用,并在返回响应时间过长时触发。
如果服务器未运行或不可用,则数据包会导致连接被拒绝。通信以错误消息或回退结束。如果它设置得很低,这可能会在之前发生。执行查找和接收此类数据包所花费的时间是造成此延迟的很大一部分原因。它可能会根据涉及 DNS 查找的远程主机而更改。connectTimeout |
手动创建假装客户端
在某些情况下,可能需要以一种不 可以使用上述方法。在这种情况下,您可以使用 Feign Builder API 创建客户端。下面是一个示例 它创建了两个具有相同接口的 Feign 客户端,但每个客户端都使用 一个单独的请求拦截器。
@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 本机注解。Contract Contract |
您也可以使用 on .Builder`to configure FeignClient not to inherit beans from the parent context.
You can do this by overriding calling `inheritParentContext(false)
Builder
Feign Spring Cloud CircuitBreaker 支持
如果 Spring Cloud CircuitBreaker 在类路径上,则 Feign 会用断路器包装所有方法。spring.cloud.openfeign.circuitbreaker.enabled=true
要基于每个客户端禁用 Spring Cloud CircuitBreaker 支持,请创建一个具有“原型”范围的香草,例如:Feign.Builder
@Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
断路器名称遵循此模式。当调用 with 接口时,调用的接口方法没有参数,则断路器名称将为 。<feignClientClassName>#<calledMethod>(<parameterTypes>)
@FeignClient
FooClient
bar
FooClient#bar()
自 2020.0.2 起,断路器名称模式已从 更改为 。
使用 2020.0.4 中引入的断路器名称可以保留旧模式。<feignClientName>_<calledMethod> CircuitBreakerNameResolver |
提供 的 bean,可以更改断路器名称模式。CircuitBreakerNameResolver
@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
使用配置属性配置断路器
您可以通过配置属性配置断路器。
例如,如果您有这个 Feign 客户端
@FeignClient(url = "http://localhost:8080")
public interface DemoClient {
@GetMapping("demo")
String getDemo();
}
您可以通过执行以下操作来使用配置属性对其进行配置
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.enabled false |
假装 Spring Cloud CircuitBreaker 回退
Spring Cloud CircuitBreaker 支持回退的概念:当电路打开或出现错误时执行的默认代码路径。若要为给定对象启用回退,请将属性设置为实现回退的类名。您还需要将您的实现声明为 Spring Bean。@FeignClient
fallback
@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
@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
@Primary
primary
@FeignClient
@FeignClient(name = "hello", primary = false)
public interface HelloClient {
// methods here
}
假装继承支持
Feign 通过单继承接口支持样板 API。 这允许将常见操作分组到方便的基本接口中。
public interface UserService {
@GetMapping("/users/{id}")
User getUser(@PathVariable("id") long id);
}
@RestController
public class UserResource implements UserService {
}
@FeignClient("users")
public interface UserClient extends UserService {
}
@FeignClient 不应在服务器和客户端之间共享接口,并且不再支持使用 ON 类级别注释接口。@FeignClient @RequestMapping |
[[假装请求/响应压缩]] === 假装请求/响应压缩
您可以考虑为您的 假装请求。为此,您可以启用以下属性之一:
spring.cloud.openfeign.compression.request.enabled=true
spring.cloud.openfeign.compression.response.enabled=true
假装请求压缩为您提供的设置类似于您可能为 Web 服务器设置的设置:
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
通过这些属性,您可以选择压缩媒体类型和最小请求阈值长度。
由于 OkHttpClient 使用“透明”压缩(如果存在 or 标头,则禁用该压缩),因此当类路径上存在并设置为 时,我们不会启用压缩。content-encoding accept-encoding feign.okhttp.OkHttpClient spring.cloud.openfeign.okhttp.enabled true |
假装记录
为创建的每个 Feign 客户端创建一个记录器。默认情况下,记录器的名称是用于创建 Feign 客户端的接口的完整类名。假装日志记录仅响应级别。DEBUG
logging.level.project.user.UserClient: DEBUG
您可以为每个客户端配置的对象告诉 Feign 要记录多少。选项包括:Logger.Level
-
NONE
,无日志记录 (DEFAULT)。 -
BASIC
,仅记录请求方式和 URL 以及响应状态代码和执行时间。 -
HEADERS
,记录基本信息以及请求和响应标头。 -
FULL
,记录请求和响应的标头、正文和元数据。
例如,以下命令会将 设置为:Logger.Level
FULL
@Configuration
public class FooConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
Feign Capability 支持
Feign 功能公开核心 Feign 组件,以便可以修改这些组件。例如,这些功能可以获取 、装饰它,并将装饰后的实例返回给 Feign。
对 Micrometer 的支持就是一个很好的现实生活中的例子。请参阅千分尺支持。Client
通过创建一个或多个 Bean 并将它们放置在配置中,您可以注册它们并修改所涉及的客户机的行为。Capability
@FeignClient
@Configuration
public class FooConfiguration {
@Bean
Capability customCapability() {
return new CustomCapability();
}
}
千分尺支持
如果满足以下所有条件,则创建并注册一个 Bean,以便 Micrometer 可以观察到 Feign 客户端:MicrometerObservationCapability
-
feign-micrometer
位于类路径上 -
豆子可用
ObservationRegistry
-
假千分尺属性设置为(默认)
true
-
spring.cloud.openfeign.micrometer.enabled=true
(适用于所有客户) -
spring.cloud.openfeign.client.config.feignName.micrometer.enabled=true
(适用于单个客户端)
-
如果您的应用程序已使用 Micrometer,则启用此功能就像放入类路径一样简单。feign-micrometer |
您还可以通过以下任一方式禁用该功能:
-
从类路径中排除
feign-micrometer
-
将其中一个假千分尺属性设置为
false
-
spring.cloud.openfeign.micrometer.enabled=false
-
spring.cloud.openfeign.client.config.feignName.micrometer.enabled=false
-
spring.cloud.openfeign.micrometer.enabled=false 禁用对所有 Feign 客户端的 Micrometer 支持,而不考虑客户端级标志的值:。
如果要启用或禁用每个客户端的 Micrometer 支持,请不要设置和使用 。spring.cloud.openfeign.client.config.feignName.micrometer.enabled spring.cloud.openfeign.micrometer.enabled spring.cloud.openfeign.client.config.feignName.micrometer.enabled |
您还可以通过注册自己的 Bean 来自定义:MicrometerObservationCapability
@Configuration
public class FooConfiguration {
@Bean
public MicrometerObservationCapability micrometerObservationCapability(ObservationRegistry registry) {
return new MicrometerObservationCapability(registry);
}
}
仍然可以与 Feign(仅指标支持)一起使用,您需要禁用 Micrometer 支持 () 并创建一个 bean:MicrometerCapability
spring.cloud.openfeign.micrometer.enabled=false
MicrometerCapability
@Configuration
public class FooConfiguration {
@Bean
public MicrometerCapability micrometerCapability(MeterRegistry meterRegistry) {
return new MicrometerCapability(meterRegistry);
}
}
假装缓存
如果使用注解,则会创建并注册一个 bean,以便您的 Feign 客户端识别其接口上的注解:@EnableCaching
CachingCapability
@Cache*
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 @RequestMapping 支持
Spring Cloud OpenFeign 支持 Spring 注解及其派生注解(如、 等)。
注释上的属性(包括 、 、 、 、 和 )被解析为请求的内容。@RequestMapping
@GetMapping
@PostMapping
@RequestMapping
value
method
params
headers
consumes
produces
SpringMvcContract
请看以下示例:
使用属性定义接口。params
@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=upsert
key=value
key
-
当 时,请求 url 解析为 。
params = { "key1=v1", "key2=v2" }
/stores/storeId?key1=v1&key2=v2
-
当 时,请求 url 解析为 。
params = "key"
/stores/storeId?key
假装@QueryMap支持
Spring Cloud OpenFeign 提供了一个等效的注解,它
用于将 POJO 或 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
HATEOAS 支持
Spring 提供了一些 API 来创建遵循 HATEOAS 原则的 REST 表示,Spring Hateoas 和 Spring Data REST。
如果您的项目使用启动器
或启动器,默认情况下启用 Feign HATEOAS 支持。org.springframework.boot:spring-boot-starter-hateoas
org.springframework.boot:spring-boot-starter-data-rest
启用 HATEOAS 支持后,允许 Feign 客户端序列化 并反序列化 HATEOAS 表示模型:EntityModel、CollectionModel 和 PagedModel。
@FeignClient("demo")
public interface DemoTemplate {
@GetMapping(path = "/stores")
CollectionModel<Store> getStores();
}
Spring @MatrixVariable 支持
Spring Cloud OpenFeign 提供对 Spring 注解的支持。@MatrixVariable
如果将映射作为方法参数传递,则通过将映射中的键值对与 .@MatrixVariable
=
如果传递了其他对象,则注释中提供的(如果已定义)或带注释的变量名称为
使用 . 与提供的方法参数联接。name
@MatrixVariable
=
- 重要
-
尽管在服务器端,Spring 不要求用户将路径段占位符命名为与矩阵变量名称相同的占位符,因为它在客户端上过于模糊,但 Spring 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();
}
佯装支持CollectionFormat
我们通过提供注释来支持。
您可以通过传递所需的注释值来注释 Feign 客户端方法(或整个类以影响所有方法)。feign.CollectionFormat
@CollectionFormat
feign.CollectionFormat
在下面的示例中,使用格式而不是默认格式来处理方法。CSV
EXPLODED
@FeignClient(name = "demo")
protected interface DemoFeignClient {
@CollectionFormat(feign.CollectionFormat.CSV)
@GetMapping(path = "/test")
ResponseEntity performRequest(String test);
}
反应式支持
由于 OpenFeign 项目目前不支持响应式客户端,例如 Spring WebClient,因此 Spring Cloud OpenFeign 也不支持。
由于 Spring Cloud OpenFeign 项目现在被认为是功能完整的,因此即使它在上游项目中可用,我们也不打算添加支持。我们建议迁移到 Spring Interface 客户端。那里支持阻塞堆栈和反应式堆栈。
在此之前,我们建议使用 feign-reactive 来支持 Spring WebClient。
Spring Data 支持
如果 Jackson Databind 和 Spring Data Commons 在类路径上,则 和 的转换器将自动添加。org.springframework.data.domain.Page
org.springframework.data.domain.Sort
禁用此行为集
spring.cloud.openfeign.autoconfiguration.jackson.enabled=false
有关详细信息,请参阅。org.springframework.cloud.openfeign.FeignAutoConfiguration.FeignJacksonConfiguration
Spring 支持@RefreshScope
如果启用了 Feign 客户端刷新,则使用以下命令创建每个 Feign 客户端:
-
feign.Request.Options
作为刷新范围的 Bean。这意味着可以针对任何 Feign 客户端实例刷新 和 等属性。connectTimeout
readTimeout
-
包装在 . 下的 url这意味着 Feign 客户端的 URL(如果已定义) with 属性,可以针对任何 Feign 客户端实例刷新。
org.springframework.cloud.openfeign.RefreshableUrl
spring.cloud.openfeign.client.config.{feignName}.url
您可以通过 刷新这些属性。POST /actuator/refresh
默认情况下,Feign 客户端中的刷新行为处于禁用状态。使用以下属性启用刷新行为:
spring.cloud.openfeign.client.refresh-enabled=true
不要用注释对界面进行注释。@FeignClient @RefreshScope |
OAuth2 支持
可以通过将依赖项添加到项目并设置以下标志来启用 OAuth2 支持:spring-boot-starter-oauth2-client
spring.cloud.openfeign.oauth2.enabled=true
当标志设置为 true 并且存在 oauth2 客户机上下文资源详细信息时,将创建一个 class 的 bean。在每次请求之前,拦截器都会解析所需的访问令牌,并将其作为标头包含在内。 使用 to 获取 保存 .如果用户已使用该属性指定了 OAuth2,则该属性将用于检索令牌。如果未检索令牌或未指定令牌,则将使用从主机段检索到的令牌。OAuth2AccessTokenInterceptor
OAuth2AccessTokenInterceptor
OAuth2AuthorizedClientManager
OAuth2AuthorizedClient
OAuth2AccessToken
clientRegistrationId
spring.cloud.openfeign.oauth2.clientRegistrationId
clientRegistrationId
serviceId
url
- 提示
-
使用 as OAuth2 客户端 registrationId 对于负载均衡的 Feign 客户端来说非常方便。对于非负载均衡的,基于属性是一种合适的方法。
serviceId
clientRegistrationId
- 提示
-
如果您不想使用 的默认设置,则可以在配置中实例化此类型的 bean。
OAuth2AuthorizedClientManager
转换负载均衡的 HTTP 请求
您可以使用所选内容来转换负载平衡的 HTTP 请求。ServiceInstance
对于 ,需要实现和定义 ,如下所示:Request
LoadBalancerFeignRequestTransformer
@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_ORDER
X-Forwarded Headers 支持
X-Forwarded-Host
可以通过设置以下标志来启用支持:X-Forwarded-Proto
spring.cloud.loadbalancer.x-forwarded.enabled=true
支持向假装客户端提供 URL 的方法
您可以通过以下任一方式向 Feign 客户端提供 URL:
箱 | 例 | 详 |
---|---|---|
URL 在注释中提供。 |
|
URL 是从注释的属性解析的,没有负载平衡。 |
URL 在注释和
配置属性。 |
|
URL 是从注释的属性解析的,没有负载平衡。
配置属性中提供的 URL 保持未使用状态。 |
URL 未在批注中提供,但在配置属性中提供。 |
|
URL 从配置属性解析,不进行负载均衡。如果 ,则可以按照 Spring RefreshScope 支持中所述刷新配置属性中定义的 URL。 |
URL 既不在批注中提供,也不在配置属性中提供。 |
|
URL 从注解的属性解析,并具有负载均衡功能。 |
AOT 和原生映像支持
Spring Cloud OpenFeign 支持 Spring AOT 转换和本机映像,但仅禁用刷新模式、禁用 Feign 客户端刷新(默认设置)和禁用惰性@FeignClient
属性解析(默认设置)。
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保设置为 。spring.cloud.refresh.enabled false |
如果要在 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] url @FeignClient |
要在 -annotated-classes 上使用注解,请确保指定客户端所在的位置,例如: 或显式列出它们:@EnableFeignClients @Configuration @EnableFeignClients(basePackages = "com.example.clients") @EnableFeignClients(clients = InventoryServiceFeignClient.class)
|
对于 Spring Cloud Contract 测试集成,应使用延迟属性解析。 |
FooConfiguration 不需要用 进行注释。但是,如果是,请注意将其排除在包含此配置的任何内容之外,因为它将在指定时成为 、 、 等的默认源。这可以通过将其放在与任何 or 分开的、不重叠的包中来避免,也可以将其显式排除在 中。@Configuration @ComponentScan feign.Decoder feign.Encoder feign.Contract @ComponentScan @SpringBootApplication @ComponentScan |
使用注释的属性,以及更改名称
ensemble,它将覆盖客户端名称的别名
它将用作为该客户机创建的配置 Bean 名称的一部分。contextId @FeignClient ApplicationContext |
以前,使用该属性不需要该属性。现在需要使用。url name name |
spring-cloud-starter-openfeign 支持。但是,由于这是一个可选的依赖项,如果要使用它,则需要确保它已添加到项目中。spring-cloud-starter-loadbalancer |
从 Spring Cloud OpenFeign 4 开始,不再支持 Feign Apache HttpClient 4。我们建议改用 Apache HttpClient 5。 |
默认情况下,Feign 客户端不对斜杠字符进行编码。您可以通过将 的值设置为 来更改此行为。/ spring.cloud.openfeign.client.decodeSlash false |
如果服务器未运行或不可用,则数据包会导致连接被拒绝。通信以错误消息或回退结束。如果它设置得很低,这可能会在之前发生。执行查找和接收此类数据包所花费的时间是造成此延迟的很大一部分原因。它可能会根据涉及 DNS 查找的远程主机而更改。connectTimeout |
在上面的示例中是默认配置
由 Spring Cloud OpenFeign 提供。FeignClientsConfiguration.class |
PROD-SVC 是客户将向其发出请求的服务的名称。 |
Feign 对象定义哪些注释和值在接口上有效。这
autowired bean 提供对 SpringMVC 注解的支持,而不是
默认的 Feign 本机注解。Contract Contract |
自 2020.0.2 起,断路器名称模式已从 更改为 。
使用 2020.0.4 中引入的断路器名称可以保留旧模式。<feignClientName>_<calledMethod> CircuitBreakerNameResolver |
如果要切换回 Spring Cloud 之前使用的断路器名称
2022.0.0 您可以设置为 .spring.cloud.openfeign.circuitbreaker.alphanumeric-ids.enabled false |
@FeignClient 不应在服务器和客户端之间共享接口,并且不再支持使用 ON 类级别注释接口。@FeignClient @RequestMapping |
由于 OkHttpClient 使用“透明”压缩(如果存在 or 标头,则禁用该压缩),因此当类路径上存在并设置为 时,我们不会启用压缩。content-encoding accept-encoding feign.okhttp.OkHttpClient spring.cloud.openfeign.okhttp.enabled true |
如果您的应用程序已使用 Micrometer,则启用此功能就像放入类路径一样简单。feign-micrometer |
spring.cloud.openfeign.micrometer.enabled=false 禁用对所有 Feign 客户端的 Micrometer 支持,而不考虑客户端级标志的值:。
如果要启用或禁用每个客户端的 Micrometer 支持,请不要设置和使用 。spring.cloud.openfeign.client.config.feignName.micrometer.enabled spring.cloud.openfeign.micrometer.enabled spring.cloud.openfeign.client.config.feignName.micrometer.enabled |
不要用注释对界面进行注释。@FeignClient @RefreshScope |
箱 | 例 | 详 |
---|---|---|
URL 在注释中提供。 |
|
URL 是从注释的属性解析的,没有负载平衡。 |
URL 在注释和
配置属性。 |
|
URL 是从注释的属性解析的,没有负载平衡。
配置属性中提供的 URL 保持未使用状态。 |
URL 未在批注中提供,但在配置属性中提供。 |
|
URL 从配置属性解析,不进行负载均衡。如果 ,则可以按照 Spring RefreshScope 支持中所述刷新配置属性中定义的 URL。 |
URL 既不在批注中提供,也不在配置属性中提供。 |
|
URL 从注解的属性解析,并具有负载均衡功能。 |
如果要在 AOT 或本机映像模式下运行 Spring Cloud OpenFeign 客户端,请确保设置为 。spring.cloud.refresh.enabled false |
如果要在 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] url @FeignClient |
配置属性
要查看所有 Spring Cloud OpenFeign 相关配置属性的列表,请查看附录页面。