当前位置:网站首页>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回调
边栏推荐
- STM32的C语言与汇编语言混合编程
- MFC common current path
- PSM总结
- Opencv——霍夫变换以及遇到的一些问题
- 【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
- 【历史上的今天】5 月 29 日:共享软件先驱诞生;ChromeBox 推出;VoodooPC 创始人出生
- 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
- Win11 ne peut pas faire glisser l'image sur le logiciel de la barre des tâches
- 【插件-statistic】统计代码行数和相关数据
- 2-5基础配置-Win2003增加攻击面
猜你喜欢

第一次使用gcc和makefile编写c程序
![[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world](/img/f7/b3239802d19d00f760bb3174649a89.jpg)
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world

Moving Tencent to the cloud: half of the evolution history of cloud server CVM
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

Writing based on stm32

3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力

How does win11 close recently opened projects? Win11 method to close recently opened projects

2021年软件测试工具总结——模糊测试工具

Win11不能拖拽圖片到任務欄軟件上快速打開怎麼辦

【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
随机推荐
Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法
Win11不能拖拽圖片到任務欄軟件上快速打開怎麼辦
Initial linear regression
Win11 cannot create a new text document? Solution to win11 right click failure to create a new text document
【模糊神经网络】基于matlab的模糊神经网络仿真
[cloud native] - docker installation and deployment of distributed database oceanbase
[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter
Usage differences between isempty and isblank
转载文章:数字经济催生强劲算力需求 英特尔发布多项创新技术挖掘算力潜能
面试:Bitmap像素内存分配在堆内存还是在native中
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
把腾讯搬上云:云服务器 CVM 的半部进化史
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release
【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
StaticLayout的使用详解
CRF+BiLSTM代码分步骤解读
Interview: how do lists duplicate objects according to their attributes?