当前位置:网站首页>Feign远程调用
Feign远程调用
2022-06-26 18:25:00 【cc_南柯一梦】
1、简介
Feign是Netflix开发的⼀个轻量级RESTful的HTTP服务客户端(⽤它来发起请求, 远程调⽤的),是以Java接⼝注解的⽅式调⽤Http请求,⽽不⽤像Java中通过封装 HTTP请求报⽂的⽅式直接调⽤,Feign被⼴泛应⽤在Spring Cloud 的解决⽅案中。
类似于Dubbo,服务消费者拿到服务提供者的接⼝,然后像调⽤本地接⼝⽅法⼀样 去调⽤,实际发出的是远程的请求
Feign可帮助我们更加便捷,优雅的调⽤HTTP API:不需要我们去拼接url然后 呢调⽤restTemplate的api,在SpringCloud中,使⽤Feign⾮常简单,创建⼀个 接⼝(在消费者--服务调⽤⽅这⼀端),并在接⼝上添加⼀些注解,代码就完成 了
SpringCloud对Feign进⾏了增强,使Feign⽀持了SpringMVC注解 (OpenFeign)
本质:封装了Http调⽤流程,更符合⾯向接⼝化的编程习惯,类似于Dubbo的服务 调⽤
2、配置
【1】:maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>【2】:启动类使⽤注解@EnableFeignClients添加 Feign⽀持

【3】:创建Feign接⼝
package com.cc.gateway.feign;
import com.cc.common.dto.UserDto;
import com.cc.common.vo.ReturnVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author cc
* @data 2021年06月30日 18:01
*/
// "project-oauth" 服务名称
@FeignClient(name = "project-oauth", fallback = OauthFallback.class)
public interface OauthFeign {
/**
* token验证
* @author cc
* @date 2021/6/30 22:59
* @param dto
* @return com.cc.common.vo.ReturnVo
*/
// "/oauth/checkToken" 调用路径
@RequestMapping("/oauth/checkToken")
public ReturnVo<Boolean> checkToken(@RequestBody UserDto dto);
}
3、负载均衡
Feign 本身已经集成了Ribbon依赖和⾃动配置,因此我们不需要额外引⼊依赖,可 以通过 ribbon.xx 来进 ⾏全局配置,也可以通过服务名.ribbon.xx 来对指定服务进⾏ 细节配置配置
ribbon:
#请求连接超时时间
ConnectTimeout: 3000
#请求处理超时时间
ReadTimeout: 15000
#对所有操作都进⾏重试
OkToRetryOnAllOperations: false
####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数由MaxAutoRetries配置),
####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数由MaxAutoRetriesNextServer配置),
####如果依然不⾏,返回失败信息。
MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
4、熔断器的支持
【1】:在Feign客户端⼯程配置⽂件(application.yml)中开启Feign对熔断器的⽀持
# 开启Feign的熔断功能
feign:
hystrix:
enabled: true
Hystrix超时设置
hystrix:
command:
default:
execution:
isolation:
thread:
##########################################Hystrix的超时时⻓设置
timeoutInMilliseconds: 15000
注意:
开启Hystrix之后,Feign中的⽅法都会被进⾏⼀个管理了,⼀旦出现问题就进⼊ 对应的回退逻辑处理
针对超时这⼀点,当前有两个超时时间设置(Feign/hystrix),熔断的时候是根 据这两个时间的最⼩值来进⾏的,即处理时⻓超过最短的那个超时时间了就熔断进 ⼊回退降级逻辑
⾃定义FallBack处理类
package com.cc.gateway.feign;
import com.cc.common.dto.UserDto;
import com.cc.common.utils.ReturnVoUtil;
import com.cc.common.vo.ReturnVo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author cc
* @data 2021年06月30日 18:02
*/
@Component
public class OauthFallback implements OauthFeign {
@Override
public ReturnVo<Boolean> checkToken(@RequestBody UserDto dto) {
return ReturnVoUtil.success("请重新登录", false);
}
}
5、Feign对请求压缩和响应压缩的⽀持
Feign ⽀持对请求和响应进⾏GZIP压缩,以减少通信过程中的性能损耗。通过下⾯ 的参数 即可开启请求与响应的压缩功能:
feign:
compression:
request:
enabled: true # 开启请求压缩
mime-types: text/html,application/xml,application/json # 设置压缩的数据类型,此处也是默认值
min-request-size: 2048 # 设置触发压缩的⼤⼩下限,此处也是默认值
response:
enabled: true # 开启响应压缩边栏推荐
猜你喜欢

Clion breakpoint single step debugging

Numpy之matplotlib

Connected to surface test questions

Ethereum技术架构介绍

(multi threading knowledge points that must be mastered) understand threads, create threads, common methods and properties of using threads, and the meaning of thread state and state transition

Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template

Do you know how to compare two objects

Image binarization

Boyun, standing at the forefront of China's container industry

Logstash安装及使用
随机推荐
Deep learning: numpy
Reading notes: process consulting III
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
Temporarily turn off MySQL cache
Map and filter methods for processing scarce arrays
微信小程序 自定义 弹框组件
PC端录制扫515地机器人/scan数据
JS common regular expressions
Usage and difference between ros:: spinonce() and ros:: spin()
Map和List<Map>转相应的对象
Request method 'POST' not supported
为什么我不推荐去SAP培训机构参加培训?
Leetcode 128 longest continuous sequence
Connected to surface test questions
LeetCode 面试题29 顺时针打印矩阵
Résumé des points de connaissance
50 lines of code to crawl TOP500 books and import TXT documents
Idea collection code, quickly open favorites collection window
System table SQLite of SQLite database_ master
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)