Sentinel整合OpenFegin

news/2024/10/23 5:33:42/

之前学习了openFeign的使用,我是超链接
现在学习通过Sentinel来进行整合OpenFegin。

引入OpenFegin

我们需要在当前的8084项目中引入对应的依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

激活Sentinel对OpenFeign的支持,所以配置yml

# 激活Sentinel对OpenFeign的支持
feign:sentinel:enabled: true

主启动类要添加@EnableFeignClients注解

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {@Bean@LoadBalanced@SentinelRestTemplate(blockHandler = "handleException",blockHandlerClass= GlobalException.class,fallback = "fallback",fallbackClass = GlobalException.class)public RestTemplate getRestTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class);}
}

OpenFegin接口编写

这里我们的接口写法和之前保持一致,但是要注意,我们这里要多增加一个FeignClient的属性:

  • allback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口

  • 正常@FeignClient(value = “msb-user”)就可以发起请求

//当没有成功调用/user/{userId}接口时会走fallback属性标注的类型的处理方法 实现类方法
@Service
@FeignClient(value = "msb-user",fallback = UserFeignServiceImpl.class)
public interface UserFeignService {@GetMapping("/user/{userId}")String getUserName(@PathVariable Integer userId);
}
  • 实现类必须添加@Component注解,否则无法注入到容器中
@Component
public class UserFeignServiceImpl implements UserFeignService {@Overridepublic String getUserName(Integer userId) {return "服务降级";}
}

这里完成后我们来编写控制器

 @GetMapping("/order/user/{userId}")public String createOrder(@PathVariable Integer userId){if(userId > 3){throw new RuntimeException("没有该id");}String userName = userFeignService.getUserName(userId);System.out.println(userName);return userName;}

正常请求
在这里插入图片描述
正常抛异常,这个不好,应该统一处理一下
在这里插入图片描述
方法调用不通,终止服务提供者,则调用实现类的方法。
在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/930589.html

相关文章

【动手学深度学习】--10.卷积神经网络

文章目录 卷积神经网络1.从全连接层到卷积1.1全连接层1.2平移不变性1.3局部性1.4卷积 2.卷积层2.1互相关计算2.2卷积层2.3图像中目标的边缘检测2.4学习卷积核 3.填充和步幅3.1填充3.2步幅 4.多输入多输出通道4.1多输入通道4.2多输出通道4.3 11卷积层 5.池化层5.1池化层5.2填充和…

七款酷炫的 Mac 屏保

原文作者&#xff1a; Ankur Biswas 翻译&#xff1a;weakishLeanCloud 原链接可点击文末左下角「阅读原文」 分享我最喜欢的一些 MacBook 屏保&#xff0c;让电脑看起来美美哒&#xff5e; Padbury Clock 地址&#xff1a;https://url.leanapp.cn/aKGW0Fv Word Clock 地址&…

JWT的优缺点

JWT优点&#xff1a; 1.无需依赖数据库、Redis等中间件&#xff0c;token自带加密用户、权限信息等&#xff0c;后端直接解析即可获取&#xff1b; 2.token自带有效期&#xff0c;后端无需存储维护&#xff0c;只需校验&#xff1b; JWT缺点&#xff1a; 1.创建后后端无法销毁…

前缀、中缀、后缀表达式及简易运算实现总结

title: 前缀、中缀、后缀表达式及简易运算实现总结 date: 2023-06-30 10:25:50 tags: 表达式 categories:开发知识及其他 cover: https://cover.png feature: false 1. 概念 1.1 什么是前缀、中缀、后缀表达式&#xff1f; 前缀表达式&#xff1a;又称波兰式&#xff08;Pol…

防止记录丢失,保存QQ聊天记录文件的方法

转载于&#xff1a;http://www.fanww.com/html/QQkongjiangequ/2010/0208/2543.html qq聊天记录默认安装盘符是&#xff1a;C:/Program Files/Tencent/QQ/Users/你的qq号码 - 如果你更改了qq安装默认目录&#xff0c;请点击桌面腾讯qq右键属性查看安装目录。 qq2009版的qq聊天记…

QQ导出的txt聊天记录导入数据库方法

QQ导出的txt聊天记录导入数据库方法 1.把QQ聊天记录从QQ中导出为txt格式的文件 2.用word打开所导出的txt文件&#xff0c;然后进行查找替换&#xff0c;用^p^p替换为|分割符&#xff0c;用昵称替换为^p昵称&#xff0c;如果有多个昵称请替换多次便可&#xff0c;并保存 3.打开s…

[极客大挑战 2019]PHP(反序列化)

介绍说明&#xff0c;有备份的习惯&#xff0c;找常见的备份文件后缀名 使用dirsearch进行扫描 dirsearch -u http://f64378a5-a3e0-4dbb-83a3-990bb9e19901.node4.buuoj.cn:81/ -e php-e 指定网站语言 扫描出现&#xff0c;www.zip文件 查看index.php <?php include c…

【VulnHub】【2023年07月18日】最新全部靶场详情(下)

名称发布日期作者名称系列镜像大小下载地址描述1描述2MD5SHA1虚拟机格式操作系统DHCP服务IP地址The Pentesters: 64-Bit AppSec Primer (Beta)1 Jul 2016Austin WileThe Pentesters1.3 GBhttps://download.vulnhub.com/64bitprimer/64bitprimer.ovaHere at The Pentesters, we …