栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud OpenFeign 使用介绍

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud OpenFeign 使用介绍

文章内容参考springcloud官方文档Spring Cloud OpenFeign,版本为3.0.4

介绍

Spring Cloud OpenFeign项目设计的初衷是将OpenFeign与Springboot项目进行整合,整合后使用springboot自动装配将其托管到spring容器中

声明式rest client: Feign

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 = "/stores")
    List getStores();

    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    Page getStores(Pageable pageable);

    @RequestMapping(method = RequestMethod.POST, value = "/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 =
“com.example.clients”)

覆盖Feign默认配置

Springcloud 根据 @FeignClient& FeignClientsConfiguration在容器中创建Feign,FeignClientsConfiguration包含feign.Decoder,feign.Encoder,feign.Contract.Feign针对每个FeignClient提供了自定义配置

@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.

总结

本文只截取官方文档一部分进行介绍,更多内容请参考官方文档

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736121.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号