CodeAshen's blog CodeAshen's blog
首页
  • Spring Framework

    • 《剖析Spring5核心原理》
    • 《Spring源码轻松学》
  • Spring Boot

    • Spring Boot 2.0深度实践
  • Spring Cloud

    • Spring Cloud
    • Spring Cloud Alibaba
  • RabbitMQ
  • RocketMQ
  • Kafka
  • MySQL8.0详解
  • Redis从入门到高可用
  • Elastic Stack
  • 操作系统
  • 计算机网络
  • 数据结构与算法
  • 云原生
  • Devops
  • 前端
  • 实用工具
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • Reference
GitHub (opens new window)

CodeAshen

后端界的小学生
首页
  • Spring Framework

    • 《剖析Spring5核心原理》
    • 《Spring源码轻松学》
  • Spring Boot

    • Spring Boot 2.0深度实践
  • Spring Cloud

    • Spring Cloud
    • Spring Cloud Alibaba
  • RabbitMQ
  • RocketMQ
  • Kafka
  • MySQL8.0详解
  • Redis从入门到高可用
  • Elastic Stack
  • 操作系统
  • 计算机网络
  • 数据结构与算法
  • 云原生
  • Devops
  • 前端
  • 实用工具
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • Reference
GitHub (opens new window)
  • 剖析Spring5核心原理

  • Spring源码轻松学

  • Spring Boot 2.0深度实践

  • Spring-Cloud

    • 第01章-注册中心Eureka
    • 第02章-负载均衡器Ribbon
    • 第03章-熔断器Hystrix
    • 第04章-HTTP客户端Feign
      • Feign简介
        • Feign主要作用
        • Feign特性
        • Feign依赖
        • Feign使用步骤
      • FeignClient注解详解
        • FeignClient基础参数
        • configuration自定义配置
      • Feign多组件集成
      • Feign优化点
        • Feign优化点之HTTP性能优化
        • Feign优化点之HTTP解压缩
      • Feign继承
    • 第05章-Zuul网关
    • 第06章-微服务安全
  • Spring-Cloud-Alibaba

  • Spring
  • Spring-Cloud
CodeAshen
2023-02-10
目录

第04章-HTTP客户端Feign

# Feign简介

  • Feign是一个非常好用的HTTP客户端
  • Feign很大程度上简化了HTTP调用方式
  • Feign很好的弥补了SpringCould的Http调用缺陷

# Feign主要作用

  • Feign包含了多种HTTP调用形式
  • Feign整合了Ribbon和Hystrix
  • Feign提供了多种HTTP底层支持

# Feign特性

  • Feign实现了可插拔注解支持,包括Feign和JAX-RS注解
  • Feign支持可插拔的HTTP编码器和解码器
  • Feign支持HTTP请求和响应压缩

# Feign依赖

<!-- Feign依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Feign使用步骤

  • 集成Feign环境
  • 添加FeignClient注解
  • 业务接口添加类似SpringMVC注解

# FeignClient注解详解

# FeignClient基础参数

参数 含义
name/value 给FeignClient指定名称
url 手动指定http调用地址
path 接口统一前缀
primary 多实现时指定优先级
configuration 自定义Feign配置
fallback和fallbackFactory 降级统一处理

# configuration自定义配置

image-20201202185052141

# Feign多组件集成

  • Feign可以集成Ribbon实现负载均衡
  • Feign可以集成Hystrix实现命令封装
  • Feign可以集成Hystrix实现业务降级

Feign

# Feign优化点

# Feign优化点之HTTP性能优化

  • Feign默认使用JDK自带的HTTP方式
  • Feign最大的优化点就是更换HTTP底层实现
  • 目前Apache HttpClient是一个非常好的选择

更换步骤

  1. 加入依赖
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
    </dependency>
    
    1
    2
    3
    4
  2. 开启配置 feign.httpclient.enabled=true

# Feign优化点之HTTP解压缩

  • HTTP常见的优化项就是数据压缩
  • Feign可以支持GZip的请求解压缩
  • 注意:解压缩是把双刃剑,一定要谨慎使用
# 开启请求压缩
feign.compression.request.enabled=true
# 压缩对象
feign.compression.request.mime-types=text/xml,application/xml,application/json
# 触发压缩的最小阈值
feign.compression.request.min-request-size=2048

# 开启响应压缩
feign.compression.response.enabled=true
1
2
3
4
5
6
7
8
9

# Feign继承

  • 微服务的目标是大量复用,Feign会导致重复工作量
  • Feign提供了继承特性帮助我们解决这个问题
  • 接口复用最多只能由一层,切忌多继承
编辑 (opens new window)
上次更新: 2023/06/04, 12:34:19
第03章-熔断器Hystrix
第05章-Zuul网关

← 第03章-熔断器Hystrix 第05章-Zuul网关→

最近更新
01
第01章-RabbitMQ导学
02-10
02
第02章-入门RabbitMQ核心概念
02-10
03
第03章-RabbitMQ高级特性
02-10
更多文章>
Theme by Vdoing | Copyright © 2020-2023 CodeAshen | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式