由于hystrix的停止更新,以及阿里Sentinel在历年双十一的贡献。项目中使用了Sentinel,今天我们来讲讲Sentinel的入门教程,本文使用1.6.3版本进行讲解

本文通过Sentinel_dashBoard进行讲解,当然不引入监控看板也能实现限流熔断降级功能,但是监控看板能够直观的看到请求的QPS,成功率等等,同时可以实时的进行降级限流策略的修改与新建。

1.sentinel_dashboard的引入

  https://github.com/alibaba/Sentinel/releases,下载sentinel-dashboard-1.6.3.jar

由于dashboard是springboot的项目,在CMD模式下使用命令

  java -Dserver.port=8080

    -Dcsp.sentinel.dashboard.server=localhost:8080

    -Dproject.name=sentinel-dashboard

    -jar sentinel-dashboard-1.6.3.jar

进行控制看板服务的启动。

  其中,-Dserver.port=8080 代表看板项目的端口号,-Dcsp.sentinel.dashboard.server=localhost:8080代表本看板服务将会注册到自己的看板上,-Dproject.name=sentinel-dashboard代表本看板服务的项目名称。

访问localhost:8080;输入用户名,密码,均是sentinel,如果要自定义用户名和密码,在启动命令加上-Dsentinel.dashboard.auth.username=sentinel,
-Dsentinel.dashboard.auth.password=123456即可。

我们可以看到控制台自身的服务已经注册到了控制台上。

2. 接下来,引入需要接入sentinel功能的项目。

3.maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>spring.sentinel</groupId>
<artifactId>spring-sentinel</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.6.3</version>
</dependency> <dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
</project>

4. 编写启动类

package sentile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SentileApp { public static void main(String[] args) {
SpringApplication.run(SentileApp.class, args);
}
}

5. 编写配置类

package sentile.config;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List; @Configuration
public class SentileConfig { @Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
} @PostConstruct
private void initRules() throws Exception {
FlowRule rule1 = new FlowRule();
rule1.setResource("test.hello");
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule1.setCount(1); // 每秒调用最大次数为 1 次 List<FlowRule> rules = new ArrayList<>();
rules.add(rule1); // 将控制规则载入到 Sentinel
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
}
}

6. 编写测试入口

package sentile.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@ResponseBody
public class TestController { @GetMapping("hello")
@SentinelResource(value = "test.hello", fallback = "helloError")
public String hello(String name){
return "hello,"+name;
} public String helloError(String name, Throwable e){
return "error,"+name;
} }

7. 添加启动参数,启动服务

-Dproject.name=app1
-Dcsp.sentinel.dashboard.server=localhost:8080
-Dserver.port=9090

(由于8080已经被控制台服务占据,我们修改客户端服务端口为9090)

由于注册是懒加载的,所以我们先访问一下服务,再去看控制台看板

服务正常访问

通过同

通过观察控制台,我们发现我们的项目名为app1的项目已经注册到了控制台

接下来,我们就可以在卒簇族链路进行规则设置啦

以上的方法适合非springboot,springcloud的项目实现,如果项目本身是springboot,springcloud项目,可以直接引入

spring-cloud-starter-alibaba-sentinel

添加配置文件application.yml:
spring:
application:
name: baobanserver
cloud:
sentinel:
transport:
dashboard: localhost:9999
#eager: true


往后甚至不用写@SentinelResource注解,直接按照正常的springboot写法即可实现。然后再Sentinel的控制台进行链路规则设置即可!

springBoot整合Sentinel实现降级限流熔断的更多相关文章

  1. Hystrix介绍以及服务的降级限流熔断

    (dubbo熔断,Hystrix问的少) 无论是缓存层还是存储层都会有出错的概率,可以将它们视同为资源.作为并发量较大的系统,假如有一个资源不可用,可能会造成线程全部 hang (挂起)在这个资源上, ...

  2. Spring Cloud alibaba网关 sentinel zuul 四 限流熔断

    spring cloud alibaba 集成了 他内部开源的 Sentinel 熔断限流框架 Sentinel 介绍 官方网址 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentine ...

  3. springcloud3(六) 服务降级限流熔断组件Resilience4j

    代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-gateway/src/test/java/com/ ...

  4. spring cloud gateway整合sentinel作网关限流

    说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...

  5. Spring Cloud Alibaba基础教程:使用Sentinel实现接口限流

    最近管点闲事浪费了不少时间,感谢网友libinwalan的留言提醒.及时纠正路线,继续跟大家一起学习Spring Cloud Alibaba. Nacos作为注册中心和配置中心的基础教程,到这里先告一 ...

  6. Spring Cloud Alibaba 使用Sentinel实现接口限流

    Sentinel是什么 Sentinel的官方标题是:分布式系统的流量防卫兵.从名字上来看,很容易就能猜到它是用来作服务稳定性保障的.对于服务稳定性保障组件,如果熟悉Spring Cloud的用户,第 ...

  7. 快速体验 Sentinel 集群限流功能,只需简单几步

    ️ Pic by Alibaba Tech on Facebook 集群限流 可以限制某个资源调用在集群内的总 QPS,并且可以解决单机流量不均导致总的流控效果不佳的问题,是保障服务稳定性的利器. S ...

  8. .net core使用ocelot---第四篇 限流熔断

    简介 .net core使用ocelot---第一篇 简单使用 .net core使用ocelot---第二篇 身份验证 .net core使用ocelot---第三篇 日志记录 前几篇文章我们陆续介 ...

  9. .Net微服务实践(四)[网关]:Ocelot限流熔断、缓存以及负载均衡

    目录 限流 熔断 缓存 Header转化 HTTP方法转换 负载均衡 注入/重写中间件 后台管理 最后 在上篇.Net微服务实践(三)[网关]:Ocelot配置路由和请求聚合中我们介绍了Ocelot的 ...

随机推荐

  1. Goland 2020.2.x 激活码永久破解教程 (最新Goland激活码!2020.11.26亲测可用!)

    在2020.11.26 Goland的用户们又迎来了一次更新,这就导致很多软件打开时候就提示Goland激活码已经失效,码小辫第一时间给各位分享了关于最新Goland激活破解教程! goland已经更 ...

  2. 推荐:国产etl调度工具Taskctl web应用版,0元永久授权

    写在前面 2020年疫情席卷全球,更是对整个市场经济造成了严重影响,年初疫情肆虐,西方世界单方面的科技.经济封锁,国际关系吃紧.....导致很多中小型企业业务链受阻,大型企业经费资金吃紧,轮班制导致公 ...

  3. [自学] MIT的EECS本科+研究生课程【持续更新中-2020.06.02】

    前言 我的本科是读的电子信息工程,研究生跟着老师做项目,参与到深度学习中来,毕业后做了算法工程师,工作之后愈发发现,不论从事什么岗位,基础都很重要,但现在也没有时间再读一遍本科了,自学的话也不知道从何 ...

  4. 老猿学5G:多量纲计费与QoS的QCI、5QI、ARP、GBR和MBR

    ☞ ░ 前往老猿Python博文目录 ░ 一.多量纲计费 多量纲计费是与传统的计费模式相区别的一种计费模式,传统的计费基本上都是通过使用量.使用时长或包固定时长等方式计费,而多量纲计费是指在考虑以上方 ...

  5. 老猿学5G扫盲贴:NEF、NRF、AF、UPF以及DN的功能

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 NEF:Network Exposure Function ,网络开放 ...

  6. Python中str类型的字符串写入二进制文件时报TypeError错的处理方式

    在用二进制模式打开文件情况下,写入一个str对象时报错:TypeError: a bytes-like object is required, not 'str' 出现该问题是因为Python严格区分 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中窗口对象的windowFilePath属性

    windowFilePath属性仅对窗口对象有效,用于关联一个窗口和对应的文件及路径. 当窗口没有设置标题属性的情况下,则窗口标题展示展示windowFilePath对应的文件名的信息(路径信息不展示 ...

  8. vue基础题

    一.对于MVVM的理解? MVVM 是 Model-View-ViewModel 的缩写. Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑. View 代表UI 组件,它负责 ...

  9. POJ3565

    题目大意: 给定\(n\)个蚂蚁和\(n\)颗苹果树的坐标,要求每个蚂蚁爬到一颗苹果树旁,使得每个蚂蚁路线不相交且路线总长度最小,求每个蚂蚁爬到哪个苹果树旁? 首先假设有两只蚂蚁路径相交,那么这两个蚂 ...

  10. BJOI2015 隐身术

    落谷. Description 给你两个串 \(A.B\).询问 \(B\) 中有多少个非空子串和 \(A\) 的编辑距离不超过 \(K\). Solution 发现 \(K \le 5\),考虑可以 ...