Sentinel异步调用支持方式定义资源

完整目录清单页面(必看)

Sentinel 支持异步调用链路的统计。在异步调用中,需要通过 SphU.asyncEntry(xxx) 方法定义资源,并通常需要在异步的回调函数中调用 exit 方法。

第一步:启动类加注解@EnableAsync,让项目支持异步调用支持

@SpringBootApplication
@EnableAsync
public class SentinelHelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelHelloWorldApplication.class,args);
    }
}

第二步:创建AsyncService异步调用类以及方法

package com.java1234.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author java1234_小锋
 * @site www.java1234.com
 * @company Java知识分享网
 * @create 2021-05-27 13:18
 */
@Service
public class AsyncService {

    @Async
    public void doSomethingAsync(){
        System.out.println("async start...");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("async end...");
    }
}

第三步:创建Controller方法

@RequestMapping("helloWorld4")
public void helloWorld4(){
	AsyncEntry asyncEntry =null;
	try {
		asyncEntry = SphU.asyncEntry("helloWorld4");
		asyncService.doSomethingAsync();
	} catch (BlockException e) {
		System.out.println("系统繁忙,请稍后!");
	}finally {
		if(asyncEntry!=null){
			asyncEntry.exit();
		}
	}
}

第四步:Sentinel控制台新增流控规则

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SWkUoVmA-1622179693328)(image-20210527154900301.png)]

第五步:测试

浏览器请求:http://localhost/helloWorld4

正常访问控制台输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pY64Xf2r-1622179693335)(image-20210527155009805.png)]

频繁访问控制台输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CP1hy1Du-1622179693341)(image-20210527155020592.png)]

微信搜一搜【java1234】关注这个放荡不羁的程序员,关注后回复【资料】有我准备的一线大厂笔试面试资料以及简历模板。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值