本文对Spring Cloud OpenFeign进行学习,参考文章 Spring Cloud Reference .
Feign为声明式的web service client,使得编写web service client更加简单。使用Feign,只需要创建interface并使用annotation注释。Feign支持pluggable annotation以及pluggable encoders/decoders。当使用Feign时, Spring cloud整合Ribbon和Eureka来提供load balanced http client。
使用Feign需要引入依赖:
implementation('org.springframework.cloud:spring-cloud-starter-openfeign')
然后,在启动程序中使用@EnableFeignClients来enable Feign。
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建service interface并使用@FeignClient声明绑定的service client,用来创建Ribbon load balancer。
@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);
}
上面的interface将寻找"stores" service的physical addresses。若application为Eureka client,将从Eureka service registry中获取server list。若不使用Eureka,需要在external configuration中简单配置a list of servers。
stores:
ribbon:
listOfServers: example.com,google.com
Spring Cloud使用FeignClientsConfiguration来为每个named client创建新的ApplicationContext, 包含feign.Decoder, feign.Encoder和feign.Contract。Spring Cloud允许我们使用@FeignClient来在FeignClientsConfiguration的基础上声明额外的configuration,从而完全控制feign client,如下:
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
上例中,client包含FeignClientsConfiguration以及FooConfiguration中的配置,后者会覆盖前者。
注意:FooConfiguration中不能使用@Configuration,否则,应该放在@ComponentScan或@SpringBootApplication扫描范围外的地方。不然,该configuration将作为feign.Decoder, feign.Encoder和feign.Contract等的默认来源。
Spring Cloud Netflix为feign提供了默认的beans(BeanType beanName: ClassName):
![9eacc746301bce1488689bdff4b58d59.png](https://i-blog.csdnimg.cn/blog_migrate/fef8e6ccda4fd06181b90f4ae0220f80.jpeg)
若想配置上面的bean,需要在@FeignClient声明的configuration中创建相应的bean type,如上面的FooConfiguration。
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
}
同时,也可以使用configuration properties来配置@FeignClient.
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属性中使用上面的方法来设置default配置,区别是属性会作用于所有的feign clients。若想使用configuration properties来配置所有的@FeignClient,可以使用default feign name来创建configuration properties.
application.yml
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
若同时使用@Configuratiion bean和configuration properties, property方式会覆盖bean方式。若想改变覆盖方向,需要设置属性feign.client.default-to-properties
为false。
可以使用Feign.builder来定制化Feign Client,下面的例子使用提供的encoder,decoder,contract等信息,'http://PROD-SVC'为要访问的service。
@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");
}
}
Feign Hystrix support
若Hystrix在classpath中且feign.hystrix.enabled=true,Feign将会用断路器来封装所有的方法。Spring Cloud Dalston版本之前,若Hystrix在classpath中,Feign默认会用断路器封装所有method。
Hystrix支持fallback,当断路器打开或出现错误时,会默认调用fallback。若开启fallback功能,需要在@FeignClient中指定fallback属性为实现fallback的类名,同时声明fallback类为spring bean。
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
@Component
static class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}
若想要知道导致fallback被trigger的原因,可以使用@FeignClient的fallbackFactory属性。同样需要将实现fallbackFactory的类声明为spring bean。
@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 fallbacks时,ApplicationContext中存在多个相同类型的bean(FeignClient和各种callback),这样会导致@Autowired不知道加载哪个bean而失败。为了解决这个问题,Spring Cloud Netflix会将所有的Feign instances使用@Primary来标注,这样Spring就知道insert哪个bean。但有时,我们不需要这样,可以设置@FeignClient的primary属性为false来disable这种行为。
@FeignClient(name = "hello", primary = false)
public interface HelloClient {
// methods here
}