当前位置:网站首页>【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
2022-07-04 03:51:00 【步尔斯特】
上文我们对@FeignClient做了详细的介绍,接下来,我们来一起feign的两种降级方式。
fallbackFactory 推荐:可以捕获异常信息并返回默认降级结果。可以打印堆栈信息。
Fallback
容错类
import com.ossa.feign.api.producer.ProducerFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/** * @author issavior */
@Slf4j
@Service
public class ProducerFeignFallback implements ProducerFeign {
@Override
public String producerById(String id) {
String msg = "fallback限流方法";
return id + msg;
}
}
FeignClient
import com.ossa.feign.config.FeignClientConfig;
import com.ossa.feign.fallback.ProducerFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/** * @author issavior */
@FeignClient(value = "ossa-service-producer", configuration = FeignClientConfig.class,fallback = ProducerFeignFallback.class)
public interface ProducerFeign {
/** * 根据ID查询商品 * * @param id 商品的主键ID * @return 相关商品的信息 */
@GetMapping(value = "/producer/{id}")
String producerById(@PathVariable("id") String id);
}
FallbackFactory
容错类
import com.ossa.feign.api.producer.ProducerFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/** * @author issavior */
@Slf4j
@Service
public class ProducerFeignFallback implements FallbackFactory<ProducerFeign> {
@Override
public ProducerFeign create(Throwable throwable) {
return id -> {
log.info("错误是:{}", throwable.getMessage());
String msg = "fallback限流方法";
return id + msg;
};
}
}
FeignClient
import com.ossa.feign.config.FeignClientConfig;
import com.ossa.feign.fallback.ProducerFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/** * @author issavior */
@FeignClient(value = "ossa-service-producer", configuration = FeignClientConfig.class,fallbackFactory = ProducerFeignFallback.class)
public interface ProducerFeign {
/** * 根据ID查询商品 * * @param id 商品的主键ID * @return 相关商品的信息 */
@GetMapping(value = "/producer/{id}")
String producerById(@PathVariable("id") String id);
}
边栏推荐
- JVM family -- heap analysis
- XSS prevention
- Value transfer communication between components (parent to child, child to parent, brother component to value)
- 深度优先搜索简要讲解(附带基础题)
- The new data center helps speed up the construction of a digital economy with data as a key element
- Future源碼一觀-JUC系列
- Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
- Katalon framework test web (XXVI) automatic email
- [paddleseg source code reading] normalize operation of paddleseg transform
- Flink学习7:应用程序结构
猜你喜欢

Flink学习7:应用程序结构

JSON string conversion in unity

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO

量子力学习题

Penetration practice - sqlserver empowerment

idea修改主体颜色

JVM family -- monitoring tools

选择排序与冒泡排序模板

Getting started with the go language is simple: go implements the Caesar password

JVM family -- heap analysis
随机推荐
还原窗口位置的微妙之处
Flink学习8:数据的一致性
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Global exposure and roller shutter exposure of industrial cameras
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Pytest multi process / multi thread execution test case
idea修改主体颜色
Katalon框架测试web(二十六)自动发邮件
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
ctf-pikachu-CSRF
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
LevelDB源码解读-SkipList
vim映射命令
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
【读书会第十三期】多媒体处理工具 FFmpeg 工具集
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
疫情来袭--远程办公之思考|社区征文
pytest多进程/多线程执行测试用例
vue多级路由嵌套怎么动态缓存组件
Objective C attribute keyword