当前位置:网站首页>【微服务|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);
}
边栏推荐
- A review of reverse reinforcement learning at Virginia Tech (VT)
- I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
- SQL语句加强练习(MySQL8.0为例)
- [paddleseg source code reading] normalize operation of paddleseg transform
- vue多级路由嵌套怎么动态缓存组件
- 还原窗口位置的微妙之处
- ctf-pikachu-CSRF
- Mindmanager2022 efficient and easy to use office mind map MindManager
- 疫情来袭--远程办公之思考|社区征文
- Objective-C member variable permissions
猜你喜欢

Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption

Defensive programming skills

My opinion on how to effectively telecommute | community essay solicitation

JVM family -- monitoring tools

Flink学习8:数据的一致性

MySQL maxscale realizes read-write separation

JVM family -- heap analysis

Es network layer

Nbear introduction and use diagram

Go 语言入门很简单:Go 实现凯撒密码
随机推荐
三年进账35.31亿,这个江西老表要IPO了
Objective-C string class, array class
Redis cluster uses Lua script. Lua script can also be used for different slots
智慧地铁| 云计算为城市地铁交通注入智慧
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
The new data center helps speed up the construction of a digital economy with data as a key element
[Yugong series] go teaching course 002 go language environment installation in July 2022
Penetration practice - sqlserver empowerment
logistic regression
STM32外接DHT11显示温湿度
Flink学习8:数据的一致性
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
[PaddleSeg 源码阅读] PaddleSeg计算Dice
Perf simple process for multithreaded profile
Value transfer communication between components (parent to child, child to parent, brother component to value)
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.
支持首次触发的 Go Ticker
Support the first triggered go ticker
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装