NACOS中应用SENTINEL 熔断限流降级框架

SENTINEL 熔断限流降级框架

在学习sentinel之前我已经玩过hystrix了,看到nacos真的就感觉不需要这边引用一个框架!这边又换一个!对于初学者算友好!就是一些配置。

首先如果我们只是想,先搭建一个sentinel看看样!我导入了

			<dependency>
	            <groupId>com.alibaba.csp</groupId>
	            <artifactId>sentinel-core</artifactId>
	            <version>1.7.0</version>
	        </dependency>

然后你就可以导入

		<dependency>
	        <groupId>com.alibaba.csp</groupId>
	        <artifactId>sentinel-annotation-aspectj</artifactId>
	        <version>1.7.1</version>
	    </dependency>

来注解 @SentinelResource(value=“rule”,blockhandler=“method”) 记得写个method来降级回调。

			/**
			* 定义限流规则
			*/
			@PostConstruct
			private void initFlowRules(){
			    List<FlowRule> rules = new ArrayList<>();
			    FlowRule rule = new FlowRule();
			    rule.setResource("HelloWorld");
			    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
			    // 设置QPS为1
			    rule.setCount(1);
			    rules.add(rule);
			    FlowRuleManager.loadRules(rules);
			}

还得定义sentinel的限流规则。之后可以在sentinel dashboard中直接鼠标操作。

参考:

    @RequestMapping("/getOrder1")
    @ResponseBody
    public String queryOrder2(@RequestParam("orderId") String orderId) {

        Entry entry = null;
        // 资源名 也就是规则名
        String resourceName = "KEY";
        try {
            // entry可以理解成入口登记
            entry = SphU.entry(resourceName);
            // 被保护的逻辑, 这里为订单查询接口
            return orderQueryService.queryOrderInfo(orderId);
        } catch (BlockException blockException) {
            // 接口被限流的时候, 会进入到这里
            LOG.warn("---getOrder1接口被限流了---, exception: ", blockException);
            return "接口限流, 返回空";
        } finally {
            // SphU.entry(xxx) 需要与 entry.exit() 成对出现,否则会导致调用链记录异常
            if (entry != null) {
                entry.exit();
            }
        }
    }

然后限流就差不多了。

补充一句,在.yml中 写入eager这个便可以启动项目在dashboard中
具体看此博客NACOS:SENTINEL DASHBOARD 的配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值