资讯专栏INFORMATION COLUMN

Spring Cloud 参考文档(声明式REST客户端:Feign)

wqj97 / 2646人阅读

摘要:继承支持通过单继承接口支持样板,这允许将通用操作分组为方便的基本接口。,记录基本信息以及请求和响应。例如,类定义参数和以下客户端使用注解使用类

声明式REST客户端:Feign

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

如何包含Feign

要在项目中包含Feign,请使用包含组名为org.springframework.cloud和工件名为spring-cloud-starter-openfeign的启动器。

spring boot应用示例

</>复制代码

  1. @SpringBootApplication
  2. @EnableFeignClients
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

StoreClient.java

</>复制代码

  1. @FeignClient("stores")
  2. public interface StoreClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/stores")
  4. List getStores();
  5. @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
  6. Store update(@PathVariable("storeId") Long storeId, Store store);
  7. }

@FeignClient注解中,String值(上面的“stores”)是一个任意客户端名称,用于创建Ribbon负载均衡器,你还可以使用url属性指定URL(绝对值或仅指定主机名),应用程序上下文中bean的名称是接口的完全限定名称,要指定自己的别名值,可以使用@FeignClient注解的qualifier值。

上面的Ribbon客户端将想要发现“stores”服务的物理地址,如果你的应用程序是Eureka客户端,那么它将解析Eureka服务注册表中的服务,如果你不想使用Eureka,只需在外部配置中配置服务器列表。

覆盖Feign默认值

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

Spring Cloud允许你通过使用@FeignClient声明其他配置(在FeignClientsConfiguration之上)来完全控制feign客户端,例如:

</>复制代码

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

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

</>复制代码

  1. FooConfiguration不需要使用@Configuration注解,但是,如果是,则注意将其从任何包含此配置的@ComponentScan中排除,因为它将成为feign.Decoderfeign.Encoderfeign.Contract等的默认源。这可以通过将其放在任何@ComponentScan@SpringBootApplication的多带带的非重叠包中来避免,也可以在@ComponentScan中明确排除。

</>复制代码

  1. 现在不推荐使用serviceId属性,而是使用name属性。

</>复制代码

  1. 使用@FeignClient注解的contextId属性除了更改ApplicationContext集成的名称,它将覆盖客户端名称的别名,它将用作为该客户端创建的配置bean名称的一部分。

</>复制代码

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

nameurl属性支持占位符。

</>复制代码

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

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

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.enabledfeign.httpclient.enabled分别设置为true,并将它们放在类路径上来使用OkHttpClientApacheHttpClient feign客户端,你可以通过在使用Apache时提供ClosableHttpClient或在使用OK HTTP时提供OkHttpClient的bean来定制使用的HTTP客户端。

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

Logger.Level

Retryer

ErrorDecoder

Request.Options

Collection

SetterFactory

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

</>复制代码

  1. @Configuration
  2. public class FooConfiguration {
  3. @Bean
  4. public Contract feignContract() {
  5. return new feign.Contract.Default();
  6. }
  7. @Bean
  8. public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
  9. return new BasicAuthRequestInterceptor("user", "password");
  10. }
  11. }

这将使用feign.Contract.Default替换SpringMvcContract,并将RequestInterceptor添加到RequestInterceptor的集合中。

@FeignClient也可以使用配置属性进行配置。

application.yml

</>复制代码

  1. feign:
  2. client:
  3. config:
  4. feignName:
  5. connectTimeout: 5000
  6. readTimeout: 5000
  7. loggerLevel: full
  8. errorDecoder: com.example.SimpleErrorDecoder
  9. retryer: com.example.SimpleRetryer
  10. requestInterceptors:
  11. - com.example.FooRequestInterceptor
  12. - com.example.BarRequestInterceptor
  13. decode404: false
  14. encoder: com.example.SimpleEncoder
  15. decoder: com.example.SimpleDecoder
  16. contract: com.example.SimpleContract

可以以与上述类似的方式在@EnableFeignClients属性defaultConfiguration中指定默认配置,不同之处在于此配置将适用于所有feign客户端。

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

application.yml

</>复制代码

  1. feign:
  2. client:
  3. config:
  4. default:
  5. connectTimeout: 5000
  6. readTimeout: 5000
  7. loggerLevel: basic

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

</>复制代码

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

application.yml

</>复制代码

  1. # To disable Hystrix in Feign
  2. feign:
  3. hystrix:
  4. enabled: false
  5. # To set thread isolation to SEMAPHORE
  6. hystrix:
  7. command:
  8. default:
  9. execution:
  10. isolation:
  11. strategy: SEMAPHORE

如果我们想要创建具有相同名称或URL的多个feign客户端,以便它们指向同一服务器但每个都具有不同的自定义配置,那么我们必须使用@FeignClientcontextId属性,以避免这些配置bean的名称冲突。

</>复制代码

  1. @FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class)
  2. public interface FooClient {
  3. //..
  4. }

</>复制代码

  1. @FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class)
  2. public interface BarClient {
  3. //..
  4. }
手动创建Feign客户端

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

</>复制代码

  1. @Import(FeignClientsConfiguration.class)
  2. class FooController {
  3. private FooClient fooClient;
  4. private FooClient adminClient;
  5. @Autowired
  6. public FooController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
  7. this.fooClient = Feign.builder().client(client)
  8. .encoder(encoder)
  9. .decoder(decoder)
  10. .contract(contract)
  11. .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
  12. .target(FooClient.class, "http://PROD-SVC");
  13. this.adminClient = Feign.builder().client(client)
  14. .encoder(encoder)
  15. .decoder(decoder)
  16. .contract(contract)
  17. .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
  18. .target(FooClient.class, "http://PROD-SVC");
  19. }
  20. }

</>复制代码

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

</>复制代码

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

</>复制代码

  1. Feign Contract对象定义了哪些注解和值在接口上是有效的,自动装配的Contract bean提供对SpringMVC注解的支持,而不是默认的Feign原生注解。
Feign Hystrix支持

如果Hystrix位于类路径并且feign.hystrix.enabled=true,则Feign将使用断路器包装所有方法,返回com.netflix.hystrix.HystrixCommand也可用,这允许你使用反应模式(通过调用.toObservable().observe()或异步使用(通过调用.queue())。

要在每个客户端的基础上禁用Hystrix支持,请创建一个带有“prototype”范围的vanilla F​​eign.Builder,例如:

</>复制代码

  1. @Configuration
  2. public class FooConfiguration {
  3. @Bean
  4. @Scope("prototype")
  5. public Feign.Builder feignBuilder() {
  6. return Feign.builder();
  7. }
  8. }

在Spring Cloud Dalston发布之前,如果Hystrix在类路径上,Feign会默认将所有方法包装在断路器中,Spring Cloud Dalston中更改了此默认行为,转而采用了选择加入方法。

Feign Hystrix Fallback

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

</>复制代码

  1. @FeignClient(name = "hello", fallback = HystrixClientFallback.class)
  2. protected interface HystrixClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/hello")
  4. Hello iFailSometimes();
  5. }
  6. static class HystrixClientFallback implements HystrixClient {
  7. @Override
  8. public Hello iFailSometimes() {
  9. return new Hello("fallback");
  10. }
  11. }

如果需要访问产生回退触发器的原因,可以使用@FeignClient中的fallbackFactory属性。

</>复制代码

  1. @FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
  2. protected interface HystrixClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/hello")
  4. Hello iFailSometimes();
  5. }
  6. @Component
  7. static class HystrixClientFallbackFactory implements FallbackFactory {
  8. @Override
  9. public HystrixClient create(Throwable cause) {
  10. return new HystrixClient() {
  11. @Override
  12. public Hello iFailSometimes() {
  13. return new Hello("fallback; reason was: " + cause.getMessage());
  14. }
  15. };
  16. }
  17. }

</>复制代码

  1. Feign中实现回退以及Hystrix回退如何工作都有一定的限制,返回com.netflix.hystrix.HystrixCommandrx.Observable的方法目前不支持回退。
Feign和@Primary

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

</>复制代码

  1. @FeignClient(name = "hello", primary = false)
  2. public interface HelloClient {
  3. // methods here
  4. }
Feign继承支持

Feign通过单继承接口支持样板api,这允许将通用操作分组为方便的基本接口。

UserService.java

</>复制代码

  1. public interface UserService {
  2. @RequestMapping(method = RequestMethod.GET, value ="/users/{id}")
  3. User getUser(@PathVariable("id") long id);
  4. }

UserResource.java

</>复制代码

  1. @RestController
  2. public class UserResource implements UserService {
  3. }

UserClient.java

</>复制代码

  1. package project.user;
  2. @FeignClient("users")
  3. public interface UserClient extends UserService {
  4. }

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

Feign请求/响应压缩

你可以考虑为你的Feign请求启用请求或响应GZIP压缩,你可以通过启用以下属性之一来执行此操作:

</>复制代码

  1. feign.compression.request.enabled=true
  2. feign.compression.response.enabled=true

Feign请求压缩为你提供类似于你为Web服务器设置的设置:

</>复制代码

  1. feign.compression.request.enabled=true
  2. feign.compression.request.mime-types=text/xml,application/xml,application/json
  3. feign.compression.request.min-request-size=2048

通过这些属性,你可以选择压缩介质类型和最小请求阈值长度。

Feign记录日志

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

application.yml

</>复制代码

  1. logging.level.project.user.UserClient: DEBUG

你可以为每个客户端配置Logger.Level对象,告诉Feign要记录多少,选择是:

NONE,没有记录(DEFAULT)。

BASIC,仅记录请求方法和URL以及响应状态代码和执行时间。

HEADERS,记录基本信息以及请求和响应headers。

FULL,记录请求和响应的headers、body和元数据。

例如,以下内容将Logger.Level设置为FULL

</>复制代码

  1. @Configuration
  2. public class FooConfiguration {
  3. @Bean
  4. Logger.Level feignLoggerLevel() {
  5. return Logger.Level.FULL;
  6. }
  7. }
Feign @QueryMap支持

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

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

例如,Params类定义参数param1param2

</>复制代码

  1. // Params.java
  2. public class Params {
  3. private String param1;
  4. private String param2;
  5. // [Getters and setters omitted for brevity]
  6. }

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

</>复制代码

  1. @FeignClient("demo")
  2. public class DemoTemplate {
  3. @GetMapping(path = "/demo")
  4. String demoEndpoint(@SpringQueryMap Params params);
  5. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74163.html

相关文章

  • Spring Cloud中如何优雅的使用Feign调用接口

    摘要:对进行了封装,使其支持标准注解和。可以与和组合使用以支持负载均衡。中使用当我们搭建好注册中心之后,就是需要将自己的服务注册到中,然后别的服务可以直接调用。 JAVA 项目中接口调用怎么做 ? Httpclient Okhttp Httpurlconnection RestTemplate 上面是最常见的几种用法,我们今天要介绍的用法比上面的更简单,方便,它就是 Feign Feig...

    ChanceWong 评论0 收藏0
  • 外行人都能看懂的SpringCloud,错过了血亏!

    摘要:集群系统中的单个计算机通常称为节点,通常通过局域网连接,但也有其它的可能连接方式。这样就高兴了,可以专心写自己的,前端就专门交由小周负责了。于是,小周和就变成了协作开发。都是为了项目正常运行以及迭代。 一、前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringClou...

    沈建明 评论0 收藏0
  • 外行人都能看懂的SpringCloud,错过了血亏!

    摘要:集群系统中的单个计算机通常称为节点,通常通过局域网连接,但也有其它的可能连接方式。这样就高兴了,可以专心写自己的,前端就专门交由小周负责了。于是,小周和就变成了协作开发。都是为了项目正常运行以及迭代。 一、前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringClou...

    enda 评论0 收藏0

发表评论

0条评论

wqj97

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<