文章内容参考springcloud官方文档Spring Cloud OpenFeign,版本为3.0.4介绍 Spring Cloud OpenFeign项目设计的初衷是将OpenFeign与Springboot项目进行整合,整合后使用springboot自动装配将其托管到spring容器中
声明式rest client: Feign 【spring|Spring Cloud OpenFeign 使用介绍】Feign设计为声明式web服务客户端简化了web服务间的请求,在没有使用过Feign前请求http接口需要大量的代码书写与各种配置设置,Feign通过接口+注解的方式进行声明,支持的注解:Feign annotations & JAX-RS annotations,Feign支持可插拔的编码器与解码器例如 Java bean -> Json & Json -> Java Bean,为了兼容Spring MVC模块中注解Spring Web 默认使用HttpMessageConverters,Spring Cloud LoadBalancer*为Feign提供了负载均衡功能
如何在项目中引入Feign 在springcloud 项目pom中引入相应的starter即可
org.springframework.cloud
spring-cloud-starter-openfeign
引入后在项目启动类上开启 @EnableFeignClients即可
@SpringBootApplication
@EnableFeignClients
public class Application {public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}}
通过接口+注解方式声明如下所示
StoreClient.java 代码来自springcloud 官网
@FeignClient("stores")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "https://www.it610.com/stores")
List getStores();
@RequestMapping(method = RequestMethod.GET, value = "https://www.it610.com/stores")
Page getStores(Pageable pageable);
@RequestMapping(method = RequestMethod.POST, value = "https://www.it610.com/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
}
- @FeignClient(“stores”) 中"stores"为FeignCient的value-代表应用名,如果是分布式微服务此应用名将在spring容器启动时创建为负载均衡的client,此处也可以直接使用url属性指定一个完整的url
- @FeignClient创建的bean在spring容器中的名字是该接口的全限定名,可以通过qualifiers属性自定义该对象在spring 容器中的名字
- 如果应用是用了Eureka或者Nacos,负载均衡的客户端会在注册中心中选择一个服务进行连接
- 例子中的请求都是阻塞方式
@EnableFeignClients可以扫描指定包下所有接口例如:@EnableFeignClients(basePackages =覆盖Feign默认配置 Springcloud 根据 @FeignClient& FeignClientsConfiguration在容器中创建Feign,FeignClientsConfiguration包含feign.Decoder,feign.Encoder,feign.Contract.Feign针对每个FeignClient提供了自定义配置
“com.example.clients”)
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
在上例中FeignClient的创建将会通过FeignClientsConfiguration和FooConfiguration,后者配置将会覆盖前者
FooConfiguration不需要使用@Configration注解,如果加了该注解需要在扫描包的注解中将此类排除不然此类就会成为默认配置Spring Cloud OpenFeign提供如下bean作为Feign默认配置
- Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
- Encoder feignEncoder: SpringEncoder
- Logger feignLogger: Slf4jLogger
- MicrometerCapability micrometerCapability: If feign-micrometer is on the classpath and MeterRegistry is available
- Contract feignContract: SpringMvcContract
- Feign.Builder feignBuilder: FeignCircuitBreaker.Builder
- Client feignClient: If Spring Cloud LoadBalancer is on the classpath, FeignBlockingLoadBalancerClient is used. If none of them is on the classpath, the default feign client is used.
推荐阅读
- Spring|Spring Cloud OpenFeign/Hystrix 超时配置
- java|SpringCloud OpenFeign的功能与使用
- SpringCloud|SpringCloud -Ribbon
- java|SpringCloud OpenFeign + Nacos
- MyBatis|mybatis-config.xml-配置文件详解
- springcloud|Spring Cloud OpenFeign(负载均衡器)
- Spring|Spring Cloud OpenFeign 详解
- Java|2017 java程序员面试宝典
- Java|Java高级笔试宝典覆盖近3年Java笔试中98%高频知识点