1.Feign是什么?
Feign是一个声明式的web服务客户端。它使编写web服务变得更容易。
要使用Feign创建一个接口interface和注解(annotate)便可以实现。
它具有可插拔的的注解(annotation)支持,包括feitn自身的注解和JAX-RS注解。
Feign还支持可插拔的编码器(encoders)和解码器(decoders)。
Spring Cloud增加了对Spring MVC注释的支持,并支持使用在Spring Web中默认使用的相同HttpMessageConverters。Spring Cloud集成Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。
github地址:https://github.com/OpenFeign/feign
2.如何使用Feign?
2.1 使用默认配置
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
Example spring boot app:
@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();
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
}
2.2 覆写Feign的默认配置(Overriding Feign Defaults):
官档如下描述: