Spring Cloud Alibaba(四)简单接入Sentinel(fallback用法)

@SentinelResource 注解

接着 Spring Cloud Alibaba(三)简单接入Sentinel(Sentinel 控制台),来看一下 @SentinelResource 一些常用的属性:

value : 资源名称,必需项(不能为空)
fallback:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。fallback 函数签名和位置要求:
返回值类型必须与原函数返回值类型一致;
方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析

首先我们需要开启注解支持,SpringBootApplication 入口添加注解支持。

	// 注解支持的配置Bean
	@Bean
	public SentinelResourceAspect sentinelResourceAspect() {
		return new SentinelResourceAspect ();
	}

先测试一下 fallback,新增一个SentinelTest 方法并增加一个SentinelTestException方法:

@GetMapping ("/sentinel-test/{id}")
	@SentinelResource ( value = "SentinelTest",fallback = "SentinelTestException")
	public String SentinelTest(@PathVariable("id") long id){
		long defaultId = 10L;
		
		if (id < defaultId) {
			throw new RuntimeException ("id bad");
		}
		return "Sentinel test OKK!";
	}
	
	/**
	 * fallback
	 * @param id
	 */
	public String SentinelTestException (long id) {
		
		log.error ("id={}",id);
		
		return "Sentinel test Error!";
	}

启动应用,在浏览器直接访问 http://localhost:1889/sentinel-test/1 和 http://localhost:1889/sentinel-test/11 来看结果。可以看到当id = 1 时,抛出异常,然后通过fallback 属性执行了 SentinelTestException 方法,最后返回的是 "Sentinel test Error!" ,而id = 11 时,程序正常执行完成 返回 "Sentinel test OKK!";
这里 SentinelTestException 还可以增加一个Throwable 类型的参数,可以通过这个参数来实现捕获不同的异常,从而做对应的异常处理。
fallback 函数位置有要求,必须和原方法在同一个类中,实际需求中,我们需要放在其他地方。 通过fallbackClass 指定对应的类的 Class 对象,添加一个 static 函数,否则无法解析。

首先添加一个SentinelController

@RestController
@Slf4j
public class SentinelController {
	
	@Autowired
	private SentinelService sService;
	
	@GetMapping ("/sentinel-test2/{id}")
	public String SentinelTest(@PathVariable("id") long id){
		return sService.sentinelTest (id);
	}
	
}

创建一个SentinelService接口以及其实现类,实现类代码如下:

@Service ("sService")
@Slf4j
public class SentinelServiceImpl implements SentinelService{
	
	@Override
	@SentinelResource ( value = "SentinelTest2",
	fallback ="sentinelTestFallBackClass",
	fallbackClass = {FallBackService.class})
	public String sentinelTest ( long id ) {
		long defaultId = 10L;
		
		if (id < defaultId) { //抛出异常,触发熔断降级
			throw new RuntimeException ("id bad");
		}
		return id+" hello OKK!";
	}
}

创建一个FallBackService类,并定义一个static 函数

@Slf4j
public class FallBackService {

	public static String sentinelTestFallBackClass ( long id,Throwable e ) {
		log.error ("异常降级处理");
		
		//可以处理各种类型的异常,自定义异常
		if (e instanceof RuntimeException) {
			System.out.println ("异常类型");
		}
		return id+" Error";
	}
}

启动应用,在浏览器访问:http://localhost:1889/sentinel-test2/11
在这里插入图片描述
然后访问:http://localhost:1889/sentinel-test2/1
在这里插入图片描述
后台日志也打印了对应的信息:

ERROR 10712 --- [nio-1889-exec-8] c.f.n.c.service.impl.FallBackService     : 异常降级处理

接下来看一下 defaultFallback说明,用法很明显了。需要注意的是 :1.6.0 之前的版本 fallback 函数只针对降级异常(DegradeException)进行处理,不能针对业务异常进行处理。

defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。
若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。
defaultFallback 函数签名要求:
返回值类型必须与原函数返回值类型一致;
方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

刚入坑学习记录一下,如果错误不足,欢迎指出。

Spring Cloud Alibaba 系列学习笔记

Spring Cloud Alibaba(一) 简单实现服务注册与发现

Spring Cloud Alibaba(二) 简单使用nacos配置中心

Spring Cloud Alibaba(三)简单接入Sentinel(Sentinel 控制台)

Spring Cloud Alibaba(四)简单接入Sentinel(fallback用法)

Spring Cloud Alibaba(五)简单接入Sentinel(blockHandler 用法)

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Alibaba Sentinel是一个基于Java的开源框架,提供了熔断、降级、限流、系统负载保护等功能,可以帮助开发者实现微服务架构中的高可用性和稳定性。下面是一个使用Spring Cloud Alibaba Sentinel实现熔断与限流的项目介绍。 1. 创建Spring Boot项目 首先,需要创建一个Spring Boot项目,并添加Spring Cloud Alibaba Sentinel的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency> ``` 2. 配置Sentinel Dashboard Sentinel Dashboard是Sentinel的可视化管理平台,可以通过它来查看应用程序的运行状况、配置规则等。需要在项目中添加Sentinel Dashboard的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel-datasource-nacos</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-dashboard</artifactId> <version>2.1.1.RELEASE</version> </dependency> ``` 同时,在application.properties文件中添加以下配置: ```properties # Sentinel Dashboard配置 spring.cloud.sentinel.transport.dashboard=localhost:8080 # Nacos配置 spring.cloud.nacos.discovery.server-addr=localhost:8848 spring.cloud.nacos.discovery.namespace= spring.cloud.nacos.discovery.username= spring.cloud.nacos.discovery.password= ``` 启动项目后,访问http://localhost:8080即可进入Sentinel Dashboard界面。 3. 实现熔断与限流 在项目中可以通过注解方式实现熔断与限流功能。例如,在Controller类中添加以下代码: ```java @RestController public class HelloController { @GetMapping("/hello") @SentinelResource(value = "hello", fallback = "fallback") public String hello(@RequestParam(required = false) String name) { if(StringUtils.isEmpty(name)) { throw new IllegalArgumentException("name is empty"); } return "Hello, " + name; } public String fallback(String name) { return "fallback " + name; } } ``` @SentinelResource注解指定了资源名称为hello,同时指定了fallback方法用于处理熔断降级。可以通过Sentinel Dashboard配置熔断规则和流量控制规则。 以上就是使用Spring Cloud Alibaba Sentinel实现熔断与限流的项目介绍。使用Sentinel可以帮助我们更好地保障应用程序的稳定性和可用性,避免因为异常情况导致系统崩溃。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值