Spring Cloud gateway 七 Sentinel 注解方式使用
Sentinel 注解支持
@SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:
- value:资源名称,必需项(不能为空)
- entryType:entry 类型,可选项(默认为 EntryType.OUT)
- blockHandler / blockHandlerClass: blockHandler 对应处理 BlockException 的函数名称,可选项。blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- fallback:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 - exceptionsToIgnore 里面排除掉的异常类型)进行处理。fallback 函数签名和位置要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。
注:1.6.0 之前的版本 fallback 函数只针对降级异常(DegradeException)进行处理,不能针对业务异常进行处理。
特别地,若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。
使用注意点采坑日记
@SentinelResource 注解不单单用于controller的接口流控。同时也可以用于方法上面。如果看过实现方式代码。可以知道他底层是基于cglib动态代理实现的。进行切面处理。注意点:
- 不能修饰在接口上面。只能修饰在实现类的方法上
- 不能修饰在静态的方法上面。
- 同一个bean方法A调用方法B,假设方法A和B都进行了注解。B方法注解失效,请参考@Transactional 失效。
- @Transactional 加于private方法, 无效
- @Transactional 加于未加入接口的public方法, 再通过普通接口方法调用, 无效
- @Transactional 加于接口方法, 无论下面调用的是private或public方法, 都有效
- @Transactional 加于接口方法后, 被本类普通接口方法直接调用, 无效
- @Transactional 加于接口方法后, 被本类普通接口方法通过接口调用, 有效
- @Transactional 加于接口方法后, 被它类的接口方法调用, 有效
- @Transactional 加于接口方法后, 被它类的私有方法调用后, 有效
blockHandler 和 blockHandlerClass 的使用
blockHandler 是可选的。如果使用blockHandlerClass,必须搭配blockHandler使用, blockHandler指定blockHandlerClass类中对应的方法名称。方法名称、参数、返回值、static 必须按照上述文档描述一样。官方文档没有强调要必须要搭配使用。
同理 fallback 和 fallbackClass也是上面讲述的注意点。
改造client 服务
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
bootstrap.yml 配置文件
spring:
cloud:
sentinel:
filter:
# sentienl 默认生效,本地调试false
enabled: true
transport:
dashboard: localhost:8890
port: 8719
# 饥饿加载
eager: true
datasource:
# Sentinel基于nacos存储获取配置信息
na:
nacos:
server-addr: 47.99.209.72:8848
groupId: DEFAULT_GROUP
dataId: ${spring.application.name}-${spring.profiles.active}-sentinel
# 类型
# FLOW("flow", FlowRule.class),
# DEGRADE("degrade", DegradeRule.class),
# PARAM_FLOW("param-flow", ParamFlowRule.class),
# SYSTEM("system", SystemRule.class),
# AUTHORITY("authority", AuthorityRule.class),
# GW_FLOW("gw-flow", "com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"),
# GW_API_GROUP("gw-api-group", "com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition");
rule-type: flow
nacos 创建 cloud-discovery-client-dev-sentinel 配置文件
[
{
"resource": "client:log:save",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "client:fegin:test",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "user:service:saveTx",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "user:service:save:test",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
创建 BackHandlerClass DiscoveryClientControllerBackHandler
package com.xian.cloud.common.handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xian.cloud.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
/**
* 对应处理 BlockException 的函数名称 服务限流
* @Author: xlr
* @Date: Created in 9:08 PM 2019/11/16
*/
@Slf4j
public class DiscoveryClientControllerBackHandler {
public static String defaultMessage(BlockException e){
log.warn( "DiscoveryClientControllerBackHandler defaultMessage BlockException : {}",e );
return "defaultMessage 服务限流,请稍后尝试";
}
public static String saveTx(UserEntity entity,BlockException e) {
log.warn( "DiscoveryClientControllerBackHandler saveTx BlockException : {}",e );
return "saveTx 服务限流,请稍后尝试";
}
}
创建 FallBackHandlerClass
package com.xian.cloud.common.handler;
import com.xian.cloud.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
/**
* 仅针对降级功能生效(DegradeException)
* @Author: xlr
* @Date: Created in 9:13 PM 2019/11/16
*/
@Slf4j
public class DiscoveryClientControllerFallBackHandler {
public static String defaultMessage(Throwable t){
log.warn( "DiscoveryClientControllerFallBackHandler defaultMessage Throwable : {}",t );
return "defaultMessage 服务降级,请稍后尝试";
}
public static String saveTx(UserEntity entity,Throwable t) {
log.warn( "DiscoveryClientControllerFallBackHandler saveTx Throwable : {}",t );
return "saveTx 服务降级,请稍后尝试";
}
}
对外接口DiscoveryClientController 添加接口
@SentinelResource(
value = "client:fegin:test",
blockHandler = "defaultMessage",
fallback = "defaultMessage",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
@RequestMapping(value = "fegin/test",method = RequestMethod.GET)
public String feginTest() {
String result = serverService.hello( "fegin" );
return " 返回 : " + result;
}
@GetMapping("/log/save")
@SentinelResource(
value = "client/log/save",
blockHandler = "defaultMessage",
fallback = "defaultMessage",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
public String save(){
UserEntity entity = new UserEntity();
entity.setUsername("tom");
entity.setPassWord("1232131");
entity.setEmail("222@qq.com");
userService.saveTx(entity);
return "success";
}
@GetMapping("user/service/save")
public String userServiceSaveTx(){
UserEntity entity = new UserEntity();
String result = userService.saveTx( entity );
return result;
}
UserServiceImpl 方法
@Override
@Transactional
@SentinelResource(
value = "user:service:saveTx",
blockHandler = "saveTx",
fallback = "saveTx",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
public String saveTx(UserEntity entity) {
return "success";
}
以上就配置完毕。然后进行测试在页面疯狂刷新
http://localhost:9011/client/user/service/save

http://localhost:9011/client/fegin/test

停止 server服务 再次调用 fegin、test
服务降级和服务限流来回切换提示在前端页面。blockHandlerClass、fallbackClass。
如何喜欢可以关注分享本公众号。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。转载请附带公众号二维码
Spring Cloud gateway 七 Sentinel 注解方式使用的更多相关文章
- Spring Cloud gateway 五 Sentinel整合
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- Spring Cloud gateway 六 Sentinel nacos存储动态刷新
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- spring cloud gateway整合sentinel作网关限流
说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第八篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...
- 跟我学SpringCloud | 第十三篇:Spring Cloud Gateway服务化和过滤器
SpringCloud系列教程 | 第十三篇:Spring Cloud Gateway服务化和过滤器 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich. ...
- 网关服务Spring Cloud Gateway(二)
上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...
- 阿里Sentinel支持Spring Cloud Gateway啦
1. 前言 4月25号,Sentinel 1.6.0 正式发布,带来 Spring Cloud Gateway 支持.控制台登录功能.改进的热点限流和注解 fallback 等多项新特性,该出手时就出 ...
- curl使用post方式访问Spring Cloud gateway报time out错误
公司老的项目使用是php,要进行重构.其他团队使用php curl函数使用post方式调用Spring Cloud gateway 报time out错误. 但是使用postman测试是没有任何问题, ...
- Spring Cloud Gateway 之获取请求体(Request Body)的几种方式
Spring Cloud Gateway 获取请求体 一.直接在全局拦截器中获取,伪代码如下 private String resolveBodyFromRequest(ServerHttpReque ...
随机推荐
- Celery的使用完成异步任务与定时任务
0917自我总结 Celery的使用 一.官方文档 Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryp ...
- RF中的if判断
关键字run keyword if 格式如下: Run Keyword If 判断条件 其他关键字 ... ELSE IF 判断条件 其他关键字 ... ELSE ...
- PowerSploit-CodeExecution(代码执行)脚本渗透实战
首先介绍一下国外大牛制作的Powershell渗透工具PowerSploit,上面有很多powershell攻击脚本,它们主要被用来渗透中的信息侦察.权限提升.权限维持. 项目地址:https://g ...
- python-函数相关
一.函数: 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print(). 但你也可以自 ...
- C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形
上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...
- LeetCode122——Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Mybatis使用自定义类型转换Postgresql
Mybatis使用自定义类型转换Postgresql 主要目的 为了解决从数据库取出来之后再手动转换为javaBean的问题. 主要用mybatis提供的Handler来把处理前置 添加转换类 imp ...
- next day
#_*_coding:utf_*_#!/usr/bin/env python now_day=input('当前某一天:').format('%Y.%m.%d')%%输入日期(年.月.日) year= ...
- 使用Apache common 的csv工具包处理csv文件
1.向csv文件中追加数据 //向文件中追加数据 BufferedWriter csvBufferedWriter = new BufferedWriter(new OutputStreamWrite ...
- day05整理
目录 一.上节课回顾 (一)数据类型 (1)数字类型 (2)字符串类型str (3)列表类型list (4)字典类型dict (二)jieba模块 (三)wordcloud模块 二.文本处理 (一)什 ...