当前位置: 首页 > news >正文

Sentinel整合OpenFeign

1、配置文件

feign:sentinel:enabled: true

2、 编写一个工厂类

import com.cart.cartservice.client.ItemClient;
import com.cart.cartservice.entity.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.ResponseEntity;@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {@Overridepublic ItemClient create(Throwable cause) {return new ItemClient() {@Overridepublic ResponseEntity<Item> queryById(Integer id) {log.error("查询对象失败", cause);//返回一个空对象return ResponseEntity.ok(new Item());}};}
}

需要实现FallbackFactory接口,其中ItemClient为客户端接口。 

3、声明这个类

import com.cart.cartservice.factory.ItemClientFallbackFactory;
import org.springframework.context.annotation.Bean;public class DefaultFeignConfig {@Beanpublic ItemClientFallbackFactory getItemClientFallbackFactory(){return new ItemClientFallbackFactory();}
}

4、ItemClient上添加openFeign注解的属性fallbackFactory = ItemClientFallbackFactory.class)

import com.cart.cartservice.entity.Item;
import com.cart.cartservice.factory.ItemClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "item-service",fallbackFactory = ItemClientFallbackFactory.class)
public interface ItemClient {@GetMapping("item/{id}")ResponseEntity<Item> queryById(@PathVariable("id") Integer id);
}

这样就可以在Sentinel里面配置限流了。

http://www.lryc.cn/news/273151.html

相关文章:

  • PyTorch实战:基于Seq2seq模型处理机器翻译任务(模型预测)
  • stm32学习总结:5、Proteus8+STM32CubeMX+MDK仿真串口并使用串口打印日志(注意重定向printf到串口打印的问题)
  • SAFe大规模敏捷企业级实训
  • 中医电子处方系统,西医个体诊所门诊卫生室病历记录查询软件教程
  • 搞定ESD(八):静电放电之原理图设计
  • 微前端 Micro App
  • Java amr格式转mp3格式
  • Vue2面试题:说一下虚拟DOM的原理?
  • Spring对bean的管理
  • Character Controller Smooth
  • 企业内训系统源码开发实战:搭建实践与经验分享
  • 15.三数之和(双指针,C解答附详细分析)
  • SpringCloud微服务 【实用篇】| Dockerfile自定义镜像、DockerCompose
  • Vue3+TS+ElementPlus的安装和使用教程【详细讲解】
  • 浅析锂电池保护板(BMS)系统设计思路(四)SOC算法-扩展Kalman滤波算法
  • 构建异步高并发服务器:Netty与Spring Boot的完美结合
  • uniapp实现文字超出宽度自动滚动(在宽度范围之内不滚动、是否自动滚动、点击滚动暂停)
  • win11 电脑睡眠功能失效了如何修复 win11 禁止鼠标唤醒
  • 内坐标转换计算
  • vue中 components自动注册,不需要一个个引入注册方法
  • web自动化测试从入门到持续集成
  • python小工具之弱密码检测工具
  • 链接器--动态链接器--延迟绑定与动态链接器是什么?学习笔记二
  • JMeter CSV 参数文件的使用方法
  • how2heap-2.23-06-unsorted_bin_into_stack
  • (学习打卡2)重学Java设计模式之六大设计原则
  • 数据结构:第7章:查找(复习)
  • 编程语言的未来?
  • SpringBoot的测试
  • C++睡眠函数:Windows平台下的Sleep函数和Linux平台的usleep函数