SpringCloud之Sentinel
一、 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的更多相关文章
- SpringCloud Alibaba Sentinel 限流详解
点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 sentinel,即可免费获取源码 熔断规则 在上一篇文章中我们讲解了 ...
- SpringCloud Alibaba整合Sentinel
SpringCloud Alibaba整合Sentinel Sentinel 控制台 1. 概述 Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理.监控(单机和集群),规则 ...
- Sentinel 流程分析
最近公司开始做新的项目.新项目准备用点新的技术.之前我们采用的是spring cloud的那一套.之前几个月看到阿里开始拥抱springcloud,推出好几个组件无缝兼容现有springcloud.我 ...
- Sentinel使用
Sentinel控制台的功能主要包括:流量控制.降级控制.热点配置.系统规则和授权规则等 # 安装sentinel的控制台 ## 下载地址 Sentinel控制台下载地址: https://githu ...
- 肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!
写在前面 不少小伙伴让我整理下有关SpringCloud和SpringCloudAlibaba的知识点,经过3天的收集和整理,冰河整理出这份4万字的SpringCloud与SpringCloudAli ...
- 十一. SpringCloud Alibaba
1. SpringCloud Alibaba简介 1.1 为什么会出现SpringCloud Alibaba Spring Cloud Netflix项目进入到维护模式 什么是维护模式?=> 将 ...
- Sentinel高级
Sentinel高级 sentinel和springCloud整合 减少开发的复杂度,对大部分的主流框架,例如:Web Servlet.Dubbo.Spring Cloud.gRPC.Spring W ...
- 《吃透微服务》 - 服务容错之Sentinel
大家好,我是小菜. 一个希望能够成为 吹着牛X谈架构 的男人!如果你也想成为我想成为的人,不然点个关注做个伴,让小菜不再孤单! 本文主要介绍 SpringCloud中Sentinel 如有需要,可以参 ...
- SpringCloude简记_part3
18. SpringCloud Alibaba Sentinel实现熔断与限流 18.1 Sentiel 官网 https://github.com/alibaba/Sentinel 中文 https ...
随机推荐
- AMS 新闻视频广告的云原生容器化之路
作者 卓晓光,腾讯广告高级开发工程师,负责新闻视频广告整体后台架构设计,有十余年高性能高可用海量后台服务开发和实践经验.目前正带领团队完成云原生技术栈的全面转型. 吴文祺,腾讯广告开发工程师,负责新闻 ...
- Win10 LTSC 2021 安装及相关bug解决
Win10 LTSC 2021 安装及相关bug解决 目录 Win10 LTSC 2021 安装及相关bug解决 准备文件 系统安装 系统激活 修复CPU占用高和输入法显示bug 安装微软应用商店 推 ...
- 关于一些lrzsz的知识
问题:如何从windows轻松上传文件到Linux? 方法:容器里面:apt-get update && apt-get install lrzsz 有yum的情况:yum -y in ...
- c++ 树状数组
关于树状数组 树状数组,即 Binary Indexed Tree ,主要用于维护查询前缀和 属于 log 型数据结构 和线段树比较 都是 log 级别 树状数组常数.耗费的空间.代码量都比线段树小 ...
- Python数据分析--Numpy常用函数介绍(9)--Numpy中几中常见的图形
在NumPy中,所有的标准三角函数如sin.cos.tan等均有对应的通用函数. 一.利萨茹曲线 (Lissajous curve)利萨茹曲线是一种很有趣的使用三角函数的方式(示波器上显示出利萨茹曲线 ...
- Node.js精进(1)——模块化
模块化是一种将软件功能抽离成独立.可交互的软件设计技术,能促进大型应用程序和系统的构建. Node.js内置了两种模块系统,分别是默认的CommonJS模块和浏览器所支持的ECMAScript模块. ...
- go程序添加远程调用tcpdump功能
最近开发的telemetry采集系统上线了.听起来高大上,简单来说就是一个grpc/udp服务端,用户的机器(路由器.交换机)将它们的各种统计数据上报采集.整理后交后端的各类AI分析系统分析.目前华为 ...
- OpenCloudOS使用snap安装.NET 6
开源操作系统社区 OpenCloudOS 由腾讯与合作伙伴共同倡议发起,是完全中立.全面开放.安全稳定.高性能的操作系统及生态.OpenCloudOS 沉淀了多家厂商在软件和开源生态的优势,继承了腾讯 ...
- 几种常见的DoS攻击
DoS为Denial of Service的简称,意思是拒绝服务.DoS攻击是一种使被攻击者无法正常提供服务的攻击.常见的攻击方式有以下几种类型: LAND Local Area Network ...
- fiddle如何使用代理
前言 Fiddle作为抓包工具深受程序员的喜爱,可能在代理方面有些会感觉到迷惑的,可以通过本文的学习来掌握一些基本的知识. Fiddle介绍 Fiddler是位于客户端和服务器端的HTTP代理,也是目 ...