当前位置:网站首页>Feign远程调用fallback回调失败,无效果
Feign远程调用fallback回调失败,无效果
2022-06-28 01:09:00 【云梦归遥】
Feign远程调用fallback回调失败,无效果
1.fallback
1.1 fallback介绍
- fallback 是用来为 Feign 远程调用失败的时候设置的回调方法,如果调用远程微服务失败,就会调用并返回服务消费端默认保留的 fallback 回调方法的内容
1.2 fallback用法
首先需要在服务消费端编写和服务提供端提供的服务相同的接口,有点类似于 Dubbo
- 这是服务提供者的controller方法
/** * @author 云梦归遥 * @date 2022/6/22 10:13 * @description */
@RestController
@RequestMapping("/products")
public class ProductsController {
@Autowired
private ProductDao productDao;
@RequestMapping("/all")
public List<Products> findAll() {
List<Products> productsList = productDao.selectList(null);
return productsList;
}
@RequestMapping("/{id}")
public Products findById(@PathVariable("id") Integer id) {
Products products = productDao.selectById(id);
return products;
}
// 注入配置文件中的 IP 和 Port
@Value("${server.port}")
private String port;
@Value("${spring.cloud.client.ip-address}")
private String address;
// 通过 Ribbon 负载均衡来验证是否实现负载均衡
@RequestMapping("/info")
public String getServerInfo(){
try {
Thread.sleep(5000);
} catch (Exception e){
System.out.println("/info 方法出现异常");
}
return address + ":" + port;
}
}
- 这是服务消费端编写的接口
public interface PageFeign {
@RequestMapping("/products/all")
public List<Products> findAll();
@RequestMapping("/products/{id}")
public Products findById(@PathVariable("id") Integer id);
@RequestMapping("/products/info")
public String getServerInfo();
}
- 我们需要在服务消费端的接口上添加注解,name 对应的是服务提供者微服务的名称
@FeignClient(name = "ProductDemo")
- 然后我们编写接口的实现类
package com.lagou.controller;
import com.lagou.entity.Products;
import org.springframework.stereotype.Component;
import java.util.List;
/** * @author 云梦归遥 * @date 2022/6/25 10:05 * @description */
@Component // 交给 Spring 进行管理
public class PageFallback implements PageFeign {
@Override
public List<Products> findAll() {
return null;
}
@Override
public Products findById(Integer id) {
return null;
}
@Override
public String getServerInfo() {
return "/page/info 接口Feign远程调用响应超时,自动开启回退";
}
}
- 此时我们的 fallback 回调类和对应的回调方法已经编写完成,需要再次修改我们的接口上的注解,完整的接口代码如下
package com.lagou.controller;
import com.lagou.entity.Products;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/** * @author 云梦归遥 * @date 2022/6/25 9:56 * @description */
@Repository
@FeignClient(name = "ProductDemo", fallback = PageFallback.class)
public interface PageFeign {
@RequestMapping("/products/all")
public List<Products> findAll();
@RequestMapping("/products/{id}")
public Products findById(@PathVariable("id") Integer id);
@RequestMapping("/products/info")
public String getServerInfo();
}
2.请求测试
2.1 第一次浏览器测试
- 请求地址:http://127.0.0.1:9100/page/info
- 请求之后发现请求失效,明明已经超出 Hystrix 超时时间,但是没有回调到 fallback的内容
2.2 处理方法,修改配置文件,要开启 Feign对 Hystrix的支持
# 配置 Feign 远程调用
feign:
hystrix:
# 为 Feign 开启 Hystrix熔断机制,就可以使用回调
enabled: true
2.3 第二次浏览器测试
- 请求地址:http://127.0.0.1:9100/page/info
- 请求效果:

3.总结
- 首先编写接口和对应的回调方法
- 第二步要在配置文件中开启 Feign 对 Hystrix的支持,否则远程调用不可用时无法执行熔断相关的操作,就会导致无法fallback回调
边栏推荐
- Win11新建不了文本文档?Win11右键无法新建文本文档的解决方法
- 【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世
- Stm32f1 and stm32subeide programming example - metal touch sensor driver
- 【历史上的今天】6 月 6 日:世界 IPv6 启动纪念日;《俄罗斯方块》发布;小红书成立
- 无心剑汉英双语诗004.《剑》
- 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
- 面试:Bitmap像素内存分配在堆内存还是在native中
- Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法
- Prometheus 2.27.0 new features
- 为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
猜你喜欢

【历史上的今天】6 月 16 日:甲骨文成立;微软 MSX 诞生;快速傅里叶变换发明者出生

Basic flask: template rendering + template filtering + control statement

【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
![[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out](/img/1b/31b5adbec5182207c371a23e41baa3.png)
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out
![[today in history] May 29: the pioneer of sharing software was born; Chromebox launched; VoodooPC founder was born](/img/ba/aa5db22e1391886a4e263d10e5f9f7.png)
[today in history] May 29: the pioneer of sharing software was born; Chromebox launched; VoodooPC founder was born

Cloud native (30) | kubernetes' app store Helm
![[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born](/img/4f/67e1598b523058a8fb6f3148136902.png)
[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born

Character interception triplets of data warehouse: substrb, substr, substring

Get 5 offers after being notified of layoffs

【历史上的今天】6 月 1 日:Napster 成立;MS-DOS 原作者出生;谷歌出售 Google SketchUp
随机推荐
如何判断线程池已经执行完所有任务了?
为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
[today in history] June 1: Napster was founded; MS-DOS original author was born; Google sells Google SketchUp
Shuttle uses custompaint to paint basic shapes
榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10
Win11 cannot create a new text document? Solution to win11 right click failure to create a new text document
The first place on the list - the carrying rate of front-end equipment is up to 10%, and the top 10 suppliers of digital key solutions
How does win11 add printers and scanners? Win11 add printer and scanner settings
第一次使用gcc和makefile编写c程序
isEmpty 和 isBlank 的用法區別
CMU提出NLP新范式—重构预训练,高考英语交出134高分
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release
What if win11 cannot use dynamic wallpaper? Solution of win11 without dynamic wallpaper
面试:Bitmap像素内存分配在堆内存还是在native中
Basic flask: template rendering + template filtering + control statement
树莓派-环境设置和交叉编译
Initial linear regression
【Kotlin】在Android官方文档中对其语法的基本介绍和理解
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power