Spring Cloud Feign


Feign概述

Feign是一种声明式、模块化的HTTP客户端。在Spring Cloud项目使用Feign,使其HTTP访问远程服务,就像调用本地的方法一样。可以无感知的调用远程服务。

  1. 可插拔的注解支持,包括Feign注解和JAX-RS注解。
  2. 支持可插拔的HTTP编码器和解码器
  3. 支持Hystrix和它的Fallback。
  4. 支持Ribbon的负载均衡,结合注册中心一起使用。
  5. 支持HTTP请求和响应的压缩。
  6. 支持替换不同HTTP组件。okhttp3,

所有配置查看类

spring-cloud-openfeign-core-2.2.9.RELEASE.jar下/META-INF/spring-configuration-metadata.json

简单示例

引入库

<dependencies>
    <!--服务注册中心客户端-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
     <!--远程HTTP调用组件-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

示例代码

启动类

@SpringBootApplication
@EnableDiscoveryClient
#用于扫描当前类包下,所有FeignClient
@EnableFeignClients
public class ShopApp {
    public static void main(String... args){
        new SpringApplication(ShopApp.class).run(args);
    }
}

编写Feign

FeignClient参数说明
在调用:fallback的时候需要开启feign.hystrix.enabled=true否则无效,不会开启熔断回调。

  1. name: 指定FeignClient名称,如果采用了服务注册于发现,则是微服务名称
  2. url: 指定的地址
  3. decode404: 当发生404异常时,如果为ture,会调用decoder进行解码,否则抛出FeignException
  4. configuration: Feign配置类
  5. fallback: 当调用发生异常时候,对调用对应类中的方法
  6. fallbackFactory:工厂类,通用异常错误处理逻辑。
  7. path:定义路径的统一前缀
@FeignClient(name = "customer", path = "/customer/",fallback = CustomerFallbackService.class, primary = false)
public interface CustomerService {
    @RequestMapping(method = RequestMethod.GET, value = "/test")
    String test();
}
/**
* 需要开启:feign.hystrix.enabled=true
* 这是在回调失败时候,会自动调用下列方法
* 其中不包含负载均衡失败的调用
*/
class CustomerFallbackService implements CustomerService{
   public String test(){
       return "error test";
   }
}

Controller调用

@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private CustomerService customerService;
    @GetMapping("test")
    public String test(){
        return "shop test";
    }
    @GetMapping("customer")
    public String customer(){
        return customerService.test();
    }
}

替换为Okhttp

okhttp功能和特性

  1. 支持SPDY,可以合并多个到同一个主机的请求。
  2. 使用连接池技术减少请求的延迟(如果SPDY是可用的)。
  3. 使用GZIP压缩减少传输的数据量
  4. 缓存响应避免重复的网络请求。

引入库

<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-okhttp</artifactId>
</dependency>

配置yml

feign:
  okhttp:
    enabled: true #开启okhttp,作为请求组件
  httpclient:
    enabled: false  #httpclient默认是开启的,所以需要关闭

替换为httpclient5

引入库

 <dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.1.3</version>
</dependency>

配置yml

feign:
  httpclient:
    enabled: false #httpclient默认是开启的,所以需要关闭
    hc5:
      enabled: true #开启http5,作为请求组件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值