Feign是一个声明式的Web服务客户端,它使编写Web服务客户端变得更容易,要使用Feign,请建立一个接口并对其进行注解,它具备可插拔的注解支持,包括Feign注解和JAX-RS注解,Feign还支持可插拔编码器和解码器。Spring Cloud增长了对Spring MVC注解的支持,并使用了Spring Web中默认使用的相同HttpMessageConverters
,Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。java
要在项目中包含Feign,请使用包含组名为org.springframework.cloud
和工件名为spring-cloud-starter-openfeign
的启动器。git
spring boot应用示例github
@SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
StoreClient.javaspring
@FeignClient("stores") public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json") Store update(@PathVariable("storeId") Long storeId, Store store); }
在@FeignClient
注解中,String值(上面的“stores”)是一个任意客户端名称,用于建立Ribbon负载均衡器,你还可使用url
属性指定URL(绝对值或仅指定主机名),应用程序上下文中bean的名称是接口的彻底限定名称,要指定本身的别名值,可使用@FeignClient
注解的qualifier
值。json
上面的Ribbon客户端将想要发现“stores”服务的物理地址,若是你的应用程序是Eureka客户端,那么它将解析Eureka服务注册表中的服务,若是你不想使用Eureka,只需在外部配置中配置服务器列表。api
Spring Cloud的Feign支持的核心概念是命名客户端,每一个feign客户端都是一个组件集成的一部分,这些组件协同工做以按需联系远程服务器,而且集成有一个名称,做为使用@FeignClient
注解的应用程序开发人员可使用这个名称。Spring Cloud使用FeignClientsConfiguration
按需为每一个命名客户端建立一个新的集成做为ApplicationContext
,这包含(除其余外)feign.Decoder
、feign.Encoder
和feign.Contract
,可使用@FeignClient
注解的contextId
属性覆盖该集成的名称。服务器
Spring Cloud容许你经过使用@FeignClient
声明其余配置(在FeignClientsConfiguration
之上)来彻底控制feign客户端,例如:app
@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. }
在这种状况下,客户端由FeignClientsConfiguration
中已有的组件以及FooConfiguration
中的任何组件组成(后者将覆盖前者)。负载均衡
FooConfiguration
不须要使用@Configuration
注解,可是,若是是,则注意将其从任何包含此配置的@ComponentScan
中排除,由于它将成为feign.Decoder
、feign.Encoder
、feign.Contract
等的默认源。这能够经过将其放在任何@ComponentScan
或@SpringBootApplication
的单独的非重叠包中来避免,也能够在@ComponentScan
中明确排除。
如今不推荐使用serviceId
属性,而是使用name
属性。
使用@FeignClient
注解的contextId
属性除了更改ApplicationContext
集成的名称,它将覆盖客户端名称的别名,它将用做为该客户端建立的配置bean名称的一部分。
之前,使用url
属性不须要name
属性,如今须要使用name
。
name
和url
属性支持占位符。异步
@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient { //.. }
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.enabled
或feign.httpclient.enabled
分别设置为true
,并将它们放在类路径上来使用OkHttpClient
和ApacheHttpClient
feign客户端,你能够经过在使用Apache时提供ClosableHttpClient
或在使用OK HTTP时提供OkHttpClient
的bean来定制使用的HTTP客户端。
Spring Cloud Netflix默认状况下不为feign提供如下bean,但仍然从应用程序上下文中查找这些类型的bean以建立feign客户端:
Logger.Level
Retryer
ErrorDecoder
Request.Options
Collection<RequestInterceptor>
SetterFactory
建立其中一种类型的bean并将其放在@FeignClient
配置中(如上面的FooConfiguration
)容许你覆盖所描述的每一个bean,例如:
@Configuration public class FooConfiguration { @Bean public Contract feignContract() { return new feign.Contract.Default(); } @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "password"); } }
这将使用feign.Contract.Default
替换SpringMvcContract
,并将RequestInterceptor
添加到RequestInterceptor
的集合中。
@FeignClient
也可使用配置属性进行配置。
application.yml
feign: client: config: feignName: connectTimeout: 5000 readTimeout: 5000 loggerLevel: full errorDecoder: com.example.SimpleErrorDecoder retryer: com.example.SimpleRetryer requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor decode404: false encoder: com.example.SimpleEncoder decoder: com.example.SimpleDecoder contract: com.example.SimpleContract
能够以与上述相似的方式在@EnableFeignClients
属性defaultConfiguration
中指定默认配置,不一样之处在于此配置将适用于全部feign客户端。
若是你更喜欢使用配置属性来配置全部@FeignClient
,则可使用default
feign名称建立配置属性。
application.yml
feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic
若是咱们同时建立@Configuration
bean和配置属性,配置属性将获胜,它将覆盖@Configuration
值,可是,若是要将优先级更改成@Configuration
,则能够将feign.client.default-to-properties
更改成false
。
若是须要在RequestInterceptor
中使用ThreadLocal
绑定变量,则须要将Hystrix的线程隔离策略设置为“SEMAPHORE”或在Feign中禁用Hystrix。
application.yml
# To disable Hystrix in Feign feign: hystrix: enabled: false # To set thread isolation to SEMAPHORE hystrix: command: default: execution: isolation: strategy: SEMAPHORE
若是咱们想要建立具备相同名称或URL的多个feign客户端,以便它们指向同一服务器但每一个都具备不一样的自定义配置,那么咱们必须使用@FeignClient
的contextId
属性,以免这些配置bean的名称冲突。
@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class) public interface FooClient { //.. }
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class) public interface BarClient { //.. }
在某些状况下,可能须要以使用上述方法没法实现的方式自定义Feign客户端,在这种状况下,你可使用Feign Builder API建立客户端。下面是一个示例,它建立两个具备相同接口的Feign客户端,但使用单独的请求拦截器配置每一个客户端。
@Import(FeignClientsConfiguration.class) class FooController { private FooClient fooClient; private FooClient adminClient; @Autowired public FooController(Decoder decoder, Encoder encoder, Client client, Contract contract) { this.fooClient = Feign.builder().client(client) .encoder(encoder) .decoder(decoder) .contract(contract) .requestInterceptor(new BasicAuthRequestInterceptor("user", "user")) .target(FooClient.class, "http://PROD-SVC"); this.adminClient = Feign.builder().client(client) .encoder(encoder) .decoder(decoder) .contract(contract) .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin")) .target(FooClient.class, "http://PROD-SVC"); } }
在上面的示例中,
FeignClientsConfiguration.class
是Spring Cloud Netflix提供的默认配置。
PROD-SVC
是客户端将向其发出请求的服务的名称。
FeignContract
对象定义了哪些注解和值在接口上是有效的,自动装配的Contract
bean提供对SpringMVC注解的支持,而不是默认的Feign原生注解。
若是Hystrix位于类路径而且feign.hystrix.enabled=true
,则Feign将使用断路器包装全部方法,返回com.netflix.hystrix.HystrixCommand
也可用,这容许你使用反应模式(经过调用.toObservable()
或.observe()
或异步使用(经过调用.queue()
)。
要在每一个客户端的基础上禁用Hystrix支持,请建立一个带有“prototype”范围的vanilla Feign.Builder
,例如:
@Configuration public class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }
在Spring Cloud Dalston发布以前,若是Hystrix在类路径上,Feign会默认将全部方法包装在断路器中,Spring Cloud Dalston中更改了此默认行为,转而采用了选择加入方法。
Hystrix支持回退的概念:在电路打开或出现错误时执行的默认代码路径,要为给定的@FeignClient
启用回退,请将fallback
属性设置为实现回退的类名,你还须要将实现声明为Spring bean。
@FeignClient(name = "hello", fallback = HystrixClientFallback.class) protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes(); } static class HystrixClientFallback implements HystrixClient { @Override public Hello iFailSometimes() { return new Hello("fallback"); } }
若是须要访问产生回退触发器的缘由,可使用@FeignClient
中的fallbackFactory
属性。
@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class) protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes(); } @Component static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> { @Override public HystrixClient create(Throwable cause) { return new HystrixClient() { @Override public Hello iFailSometimes() { return new Hello("fallback; reason was: " + cause.getMessage()); } }; } }
在Feign中实现回退以及Hystrix回退如何工做都有必定的限制,返回com.netflix.hystrix.HystrixCommand
和rx.Observable
的方法目前不支持回退。
@Primary
当与Hystrix回退一块儿使用Feign时,ApplicationContext
中有相同类型的多个bean,这将致使@Autowired
没法工做,由于没有一个明确的bean或一个标记为primary
的bean。为了解决这个问题,Spring Cloud Netflix将全部Feign实例标记为@Primary
,所以Spring Framework将知道要注入哪一个bean,在某些状况下,这可能并不理想,要关闭此行为,请将@FeignClient
的primary
属性设置为false
。
@FeignClient(name = "hello", primary = false) public interface HelloClient { // methods here }
Feign经过单继承接口支持样板api,这容许将通用操做分组为方便的基本接口。
UserService.java
public interface UserService { @RequestMapping(method = RequestMethod.GET, value ="/users/{id}") User getUser(@PathVariable("id") long id); }
UserResource.java
@RestController public class UserResource implements UserService { }
UserClient.java
package project.user; @FeignClient("users") public interface UserClient extends UserService { }
一般不建议在服务器和客户端之间共享接口,它引入了紧耦合,而且实际上也不能以其当前形式使用Spring MVC(方法参数映射不会被继承)。
你能够考虑为你的Feign请求启用请求或响应GZIP压缩,你能够经过启用如下属性之一来执行此操做:
feign.compression.request.enabled=true feign.compression.response.enabled=true
Feign请求压缩为你提供相似于你为Web服务器设置的设置:
feign.compression.request.enabled=true feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048
经过这些属性,你能够选择压缩介质类型和最小请求阈值长度。
为每一个建立的Feign客户端建立一个记录器,默认状况下,记录器的名称是用于建立Feign客户端的接口的完整类名,Feign日志记录仅响应DEBUG
级别。
application.yml
logging.level.project.user.UserClient: DEBUG
你能够为每一个客户端配置Logger.Level
对象,告诉Feign要记录多少,选择是:
NONE
,没有记录(DEFAULT)。BASIC
,仅记录请求方法和URL以及响应状态代码和执行时间。HEADERS
,记录基本信息以及请求和响应headers。FULL
,记录请求和响应的headers、body和元数据。例如,如下内容将Logger.Level
设置为FULL
:
@Configuration public class FooConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
OpenFeign @QueryMap
注解为POJO提供了支持,可用做GET参数映射,不幸的是,默认的OpenFeign QueryMap注解与Spring不兼容,由于它缺乏value
属性。
Spring Cloud OpenFeign提供等效的@SpringQueryMap
注解,用于将POJO或Map参数注解为查询参数映射。
例如,Params
类定义参数param1
和param2
:
// Params.java public class Params { private String param1; private String param2; // [Getters and setters omitted for brevity] }
如下feign客户端使用@SpringQueryMap
注解使用Params
类:
@FeignClient("demo") public class DemoTemplate { @GetMapping(path = "/demo") String demoEndpoint(@SpringQueryMap Params params); }