一、 sentinel是什么?

1.概念:

分布式服务架构的流量治理组件。

2.sentinel有什么作用?

2.1 流控:QPS、线程数

2.2 熔断降级:降级-->熔断策略、时长、请求数等

2.3 授权:黑白名单

2.4 系统自适应过载保护

提供保护机制让系统入口流量与负载达到平衡,使得系统在尽可能处理最多的请求

2.5 热点流量防护

二、 sentinel如何应用于项目中?

1.引入sentinel及持久化到nacos依赖

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel规则持久化至Nacos配置 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

2.配置

2.1 sentinel及nacos

spring:
cloud:
sentinel:
enabled: true
eager: true # 取消控制台懒加载,项目启动即连接Sentinel
transport:
client-ip: localhost
dashboard: localhost:8080
datasource:
ds:
nacos:
server-addr: http://cloud.lebao.site:8848
dataId: ams-admin-degrade-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow

2.2 在nacos创建限流规则配置文件:

ams-admin-degrade-rules

[
{
"resource": "/hello",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]

3.添加sentinel公共模块

3.1 创建网关过滤器PortalFilter

package com.ams.gateway.security;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; /**
* @description:请求头中添加当前服务名称
*/
@Component
@Slf4j
@RequiredArgsConstructor
public class PortalFilter implements GlobalFilter, Ordered { @Value("${spring.application.name}")
private String applicationName; @SneakyThrows
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest().mutate()
.header("serviceName", applicationName)
.build();
exchange = exchange.mutate().request(request).build();
return chain.filter(exchange);
} @Override
public int getOrder() {
return -1;
}
}

3.2 创建sentinel来源解析器RequestOriginParserDefinition

package com.ams.common.web.sentinel;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest;
/**
* @description:通过HttpServletRequest获取服务名
*/
@Component
public class RequestOriginParserDefinition implements RequestOriginParser {
// 获取调用方标识信息并返回
@Override
public String parseOrigin(HttpServletRequest request) {
String serviceName = request.getHeader("serviceName");
StringBuffer url = request.getRequestURL();
if (url.toString().endsWith("favicon.ico")) {
// 浏览器会向后台请求favicon.ico图标
return serviceName;
} if (StringUtils.isEmpty(serviceName)) {
throw new IllegalArgumentException("serviceName must not be null");
} return serviceName;
}
}

3.3 创建BlockExceptionHandler 阻塞异常处理器


package com.ams.common.web.sentinel; import cn.hutool.http.HttpStatus;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.ams.common.result.R;
import com.ams.common.result.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @description:捕获流控、降级、未授权异常
*/
@Component
public class DefaultBlockExceptionHandler implements BlockExceptionHandler { @Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
response.setStatus(HttpStatus.HTTP_OK);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8"); ObjectMapper objectMapper = new ObjectMapper(); // 流控
if (e instanceof FlowException) {
objectMapper.writeValue(response.getWriter(), R.failed(ResultCode.FLOW_LIMITING));
// 降级
} else if (e instanceof DegradeException) {
objectMapper.writeValue(response.getWriter(), R.failed(ResultCode.DEGRADATION));
// 未授权
} else if (e instanceof AuthorityException) {
objectMapper.writeValue(response.getWriter(), R.failed(ResultCode.SERVICE_NO_AUTHORITY));
}
}
}

3.4 下载安装sentinel面板添加设置流程、降级、授权规则等

3.4.1 流控



配置规则

3.4.2 降级



新建规则

3.4.2 授权



设置规则

参考链接

三、Sentinel插槽工作原理

关于插槽详细介绍:https://zhuanlan.zhihu.com/p/64786381


随心所往,看见未来。Follow your heart,see night!

欢迎点赞、关注、留言,收藏及转发,一起学习、交流!

SpringCloud之Sentinel的更多相关文章

  1. SpringCloud Alibaba Sentinel 限流详解

    点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 sentinel,即可免费获取源码 熔断规则 在上一篇文章中我们讲解了 ...

  2. SpringCloud Alibaba整合Sentinel

    SpringCloud Alibaba整合Sentinel Sentinel 控制台 1. 概述 Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理.监控(单机和集群),规则 ...

  3. Sentinel 流程分析

    最近公司开始做新的项目.新项目准备用点新的技术.之前我们采用的是spring cloud的那一套.之前几个月看到阿里开始拥抱springcloud,推出好几个组件无缝兼容现有springcloud.我 ...

  4. Sentinel使用

    Sentinel控制台的功能主要包括:流量控制.降级控制.热点配置.系统规则和授权规则等 # 安装sentinel的控制台 ## 下载地址 Sentinel控制台下载地址: https://githu ...

  5. 肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!

    写在前面 不少小伙伴让我整理下有关SpringCloud和SpringCloudAlibaba的知识点,经过3天的收集和整理,冰河整理出这份4万字的SpringCloud与SpringCloudAli ...

  6. 十一. SpringCloud Alibaba

    1. SpringCloud Alibaba简介 1.1 为什么会出现SpringCloud Alibaba Spring Cloud Netflix项目进入到维护模式 什么是维护模式?=> 将 ...

  7. Sentinel高级

    Sentinel高级 sentinel和springCloud整合 减少开发的复杂度,对大部分的主流框架,例如:Web Servlet.Dubbo.Spring Cloud.gRPC.Spring W ...

  8. 《吃透微服务》 - 服务容错之Sentinel

    大家好,我是小菜. 一个希望能够成为 吹着牛X谈架构 的男人!如果你也想成为我想成为的人,不然点个关注做个伴,让小菜不再孤单! 本文主要介绍 SpringCloud中Sentinel 如有需要,可以参 ...

  9. SpringCloude简记_part3

    18. SpringCloud Alibaba Sentinel实现熔断与限流 18.1 Sentiel 官网 https://github.com/alibaba/Sentinel 中文 https ...

随机推荐

  1. 在vue中路径中的@

    1.在Vue的路径中@等于src 2.在css的路径中~@等于src

  2. Python 微博搜索爬虫

    微博搜索爬虫 网页分析 由于网页端反爬虫机制比较完善所以才去移动端进行爬虫. url地址:https://m.weibo.cn/ 搜索框,输入关键词进行搜索 对网页进行抓包,找到相关数据 查看数据是否 ...

  3. LightGBM原理与实践简记

    写在前面: LightGBM 用了很久了,但是一直没有对其进行总结,本文从 LightGBM 的使用.原理及参数调优三个方面进行简要梳理. 目录 开箱即用 quickstart sklearn 接口 ...

  4. 技术分享 | 想做App测试就一定要了解的App结构

    本文节选自霍格沃兹测试开发学社内部教材 app 的结构包含了 APK 结构和 app 页面结构两个部分 APK结构 APK 是 Android Package 的缩写,其实就是 Android 的安装 ...

  5. C# 使用SpecFlow创建BDD测试用例

    将自然语言编写的测试用例转换为可执行的测试,可以大大降低需求与开发之间的沟通成本,这是BDD(行为驱动开发)希望达到的效果.SpecFlow是.Net平台的BDD工具,可以帮助我们创建面向BDD的测试 ...

  6. 一款超级好用的3Dmax模型插件 支持模型多格式批量转换

    对于模型设计师来说模型格式转换是最常见的事,但是每一款建模软件所支持的格式各有不同,模型互导操作太麻烦 为了解决这个难题,老子云平台研发了一款基于3dmax软件的模型格式转换插件,支持多种模型格式想换 ...

  7. node线上项目连接mysql出现 504 Gateway Time-Out

    var connection = mysql.createConnection({host : 'localhost',user : 'root',password : '123456',port: ...

  8. 10.5 详解Android Studio项目结构

    Android项目的结构很复杂,并不像HTML项目,最简单的直接一个HTML文件就行了,相信学完上一节的同学就明白,哪怕是一个HelloWorld这样一个项目的文件可能都有几十个,所以我们需要搞清楚, ...

  9. 贝壳自动化测试平台sosotest 学习记录

    手工测试VS自动化测试 用例执行: 手动执行 自动执行 是否需要些脚本: 需要 不需要 测试报告生成: 手动 自动 常见的测试技术 关键字驱动的测试框架 RobotFRamework 单元测试框架 自 ...

  10. go 编程规范

    如果没有编程规范会有什么问题? 哪些地方可以需要指定规范? 非编码类规范:编码规范 非编码规范 开源规范 http://www.ruanyifeng.com/blog/2011/05/how_to_c ...