当前位置:网站首页>【微服务|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);
}
边栏推荐
- Graduation summary
- laravel admin里百度编辑器自定义路径和文件名
- Mindmanager2022 efficient and easy to use office mind map MindManager
- 【读书会第十三期】多媒体处理工具 FFmpeg 工具集
- Katalon中控件的参数化
- Why is the probability of pod increasing after IPtable
- MySQL one master multiple slaves + linear replication
- 图解网络:什么是热备份路由器协议HSRP?
- Calculate the odd sum of 1~n (1~100 as an example)
- Illustrated network: what is the hot backup router protocol HSRP?
猜你喜欢
Two commonly used graphics can easily realize data display
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
My opinion on how to effectively telecommute | community essay solicitation
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
Rhcsa-- day one
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Confession code collection, who says program apes don't understand romance
三年进账35.31亿,这个江西老表要IPO了
laravel admin里百度编辑器自定义路径和文件名
Illustrated network: what is the hot backup router protocol HSRP?
随机推荐
【webrtc】m98 ninja 构建和编译指令
How was my life in 2021
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
Pytest multi process / multi thread execution test case
My opinion on how to effectively telecommute | community essay solicitation
Katalon framework tests web (XXI) to obtain element attribute assertions
Typical applications of minimum spanning tree
Global exposure and roller shutter exposure of industrial cameras
Flink学习6:编程模型
【罗技】m720
postgresql 用户不能自己创建表格配置
Sales management system of lightweight enterprises based on PHP
Storage of MySQL database
Class summation, shortest row
如何有效远程办公之我见 | 社区征文
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
Objective-C member variable permissions
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
Which product is better if you want to go abroad to insure Xinguan?
XSS prevention