当前位置: 首页 > news >正文

SpringCloud-Sentinel

一、介绍

(1)提供界面配置配置服务限流、服务降级、服务熔断
(2)@SentinelResource的blockHandler只处理后台配置的异常,运行时异常fallBack处理,且资源名为value时才生效,走兜底方法

二、安装并启动sentinel

(1)官网
(2)运行java -jar sentinel-dashboard-1.8.6.jar
(3)访问http://localhost:8080/
在这里插入图片描述
注:sentinel是懒加载的,访问接口后才会显示

三、搭建项目

(1)编写pom.xml

<?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"><parent><artifactId>demo20220821</artifactId><groupId>com.wsh.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloudalibaba-sentinel-service</artifactId><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-nacos-config</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.wsh.springcloud</groupId><artifactId>cloud-api-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

(2)编写application.yml

server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080port: 8719management:endpoints:web:exposure:include: "*"

(3)编写启动类

package com.wsh.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @ClassName SentinelService8401* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelService8401 {public static void main(String[] args) {SpringApplication.run(SentinelService8401.class, args);}
}

(4)编写Controller

package com.wsh.springcloud.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName TestController* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@RestController
public class TestController {@GetMapping("/test")public String test(){return "test";}
}

四、服务限流

在这里插入图片描述

(1)打开界面
在这里插入图片描述
(2)阈值
QPS:每秒请求数
并发线程数:处理请求的线程数
在这里插入图片描述
(3)关联
在这里插入图片描述

/test炸了,/test1也炸

在这里插入图片描述

(4)预热,阈值先从阈值/3开始,在预热时长(单位:秒)内逐渐上升
在这里插入图片描述
(5)排队等待,阈值类型必须设置为QPS,超出阈值的请求将会排队,等待的超时时间设置为20秒
在这里插入图片描述

五、服务降级

(1)满足条件后,服务在规定时间内熔断
(2)慢调用比例
在这里插入图片描述
(3)异常比例
在这里插入图片描述
(4)异常数
在这里插入图片描述

六、热点参数限流

(1)要用@SentinelResource定义资源名和兜底方法

    @GetMapping("/test2")@SentinelResource(value = "test2", blockHandler = "test2_solve")public String test2(@RequestParam("name") String name) {log.info("test2");return "test2";}public String test2_solve(String name, BlockException blockException){return "block";}

(2)
在这里插入图片描述
(3)定义规则时指定参数的值
在这里插入图片描述

七、兜底方法编写方式优化

(1)

    @GetMapping("/test3")@SentinelResource(value = "test3",  blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve")public String test3() {log.info("test3");return "test3";}

(2)编写BlockHandler类

package com.wsh.springcloud.handler;import com.alibaba.csp.sentinel.slots.block.BlockException;/*** @ClassName BlockHandler* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
public class BlockHandler {public static String test3_solve(BlockException blockException){return "block";}
}

(3)
在这里插入图片描述

八、配置fallback、blockHandler

(1)exceptionsToIgnore 用于忽略异常,不走fallback

    @GetMapping("/test3")@SentinelResource(value = "test3", blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve",fallbackClass = FallBackHandler.class, fallback = "test3_solve1",exceptionsToIgnore = NullPointerException.class)public String test3(@RequestParam("name") String name) {if (name.equals("wsh")){throw new IllegalArgumentException();}return "test3";}

(2)

public class BlockHandler {public static String test3_solve(String name, BlockException blockException){return "block";}
}
public class FallBackHandler {public static String test3_solve1(String name, Throwable throwable){return "block1";}
}

九、配置openFeign

(1)编写pom.xml

<?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"><parent><artifactId>demo20220821</artifactId><groupId>com.wsh.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloudalibaba-consumer-order84</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.wsh.springcloud</groupId><artifactId>cloud-api-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

(2)编写application.yml

server:port: 84spring:application:name: cloudalibaba-consumer-ordercloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080port: 8719management:endpoints:web:exposure:include: "*"server-url: http://cloudalibaba-provider-paymentfeign:sentinel:enabled: true

(3)编写启动类

package com.wsh.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @ClassName ConsumerOrder84* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerOrder84 {public static void main(String[] args) {SpringApplication.run(ConsumerOrder84.class, args);}
}

(4)编写PaymentService

package com.wsh.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;/*** @ClassName PaymentService* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@FeignClient(value = "cloudalibaba-provider-payment", fallback = PaymentServiceHandler.class)
public interface PaymentService {@GetMapping("/payment/test")public String test();
}

(5)编写fallback类

package com.wsh.springcloud.service;import org.springframework.stereotype.Component;/*** @ClassName PaymentServiceHandler* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@Component
public class PaymentServiceHandler implements PaymentService{@Overridepublic String test() {return "fallback";}
}

(6)编写Controller

package com.wsh.springcloud.controller;import com.wsh.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName TestController* @Description: TODO* @Author wshaha* @Date 2023/10/19* @Version V1.0**/
@RestController
public class TestController {@Autowiredprivate PaymentService paymentService;@GetMapping("/consumer/test")public String test(){return paymentService.test();}
}

(7)编写服务提供者Controller

package com.wsh.springcloud.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName TestController* @Description: TODO* @Author wshaha* @Date 2023/10/18* @Version V1.0**/
@RestController
public class TestController {@Value("${server.port}")private String port;@GetMapping("/payment/test")public String test(){int i = 1 / 0;return "test: " + port;}
}

(8)运行
在这里插入图片描述

十、配置持久化

(1)将规则持久化到nacos保存,只能先在nacos里编写好才有效
(2)pom.xml增加依赖

        <dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency>

(3)修改application.yml

    sentinel:transport:dashboard: localhost:8080port: 8719datasource:dsl:nacos:server-addr: localhost:8848dataId: cloudalibaba-sentinel-servicegroupId: DEFAULT_GROUPdata-type: jsonrule-type: flow

(4)nacos里创建配置
在这里插入图片描述

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

在这里插入图片描述

相关文章:

SpringCloud-Sentinel

一、介绍 &#xff08;1&#xff09;提供界面配置配置服务限流、服务降级、服务熔断 &#xff08;2&#xff09;SentinelResource的blockHandler只处理后台配置的异常&#xff0c;运行时异常fallBack处理&#xff0c;且资源名为value时才生效&#xff0c;走兜底方法 二、安装…...

为什么索引要用B+树来实现呢,而不是B树

首先&#xff0c;常规的数据库存储引擎&#xff0c;一般都是采用 B 树或者 B树来实现索引的存储。 B树 因为 B 树是一种多路平衡树&#xff0c;用这种存储结构来存储大量数据&#xff0c;它的整个高度会相比二叉树来说&#xff0c;会矮很多。 而对于数据库来说&#xff0c;所有…...

使用vue3前端开发的一些知识点

Vue 3 是一种流行的 JavaScript 框架&#xff0c;用于构建用户界面。它是 Vue.js 框架的第三个主要版本&#xff0c;具有许多新特性和性能改进。下面是 Vue 3 的一些常用语法和概念的详细介绍&#xff1a; 创建 Vue 实例&#xff1a; 在 Vue 3 中&#xff0c;你可以通过创建一个…...

零基础Linux_20(进程信号)内核态和用户态+处理信号+不可重入函数+volatile

目录 1. 内核态和用户态 1.1 内核态和用户态概念 1.2 内核态和用户态转化 2. 处理信号 2.2 捕捉信号 2.2 系统调用sigaction 3. 不可重入函数 4. volatile关键字 5. SIGCHLD信号&#xff08;了解&#xff09; 6. 笔试选择题 答案及解析 本篇完。 1. 内核态和用户态…...

vite+vue3+elementPlus+less+router+pinia+axios

1.创建项目2.按需引入elementplus3.引入less安装vue-router安装 axios安装 piniapinia的持久化配置(用于把数据放在localStorage中)---另外增加的配置 1.创建项目 npm init vitelatest2.按需引入elementplus npm install element-plus --save//按需引入 npm install -D unpl…...

VMwarePlayer安装Ubuntu,切换中文并安装中文输入法

1.下载和安装 虚拟机使用的免费版官网链接&#xff1a;VMwarePlayer Ubuntu镜像下载官网链接&#xff1a;Ubuntu桌面版 自己学习使用&#xff0c;不需要考虑迁移之类的。选择单个磁盘IO性能会更高 安装过程中如果出现如下报错&#xff0c;则用系统管理员身份运行 右击VMwa…...

C# JSON转为实体类和List,以及结合使用

引用 using Newtonsoft.Json;using Newtonsoft.Json.Linq;JSON转实体类 public class Person {public string Name { get; set; }public int Age { get; set; }public string Gender { get; set; } }string jsonStr "{\"name\": \"Tom\", \"a…...

使用TensorRT-LLM进行高性能推理

LLM的火爆之后&#xff0c;英伟达(NVIDIA)也发布了其相关的推理加速引擎TensorRT-LLM。TensorRT是nvidia家的一款高性能深度学习推理SDK。此SDK包含深度学习推理优化器和运行环境,可为深度学习推理应用提供低延迟和高吞吐量。而TensorRT-LLM是在TensorRT基础上针对大模型进一步…...

怎么去别人的github工程下载

1、网络 确保网络能够顺利访问github&#xff0c;有的地方的公共网络不能访问github&#xff0c;我之前开过科学上网的会员&#xff0c;发现没必要特意开去访问它。可以直接开手机热点&#xff0c;一般是可以顺利访问的。 2、下载 以我的github开源笔记qq-hh/C_review (gith…...

【java基础-实战3】list遍历时删除元素的方法

插&#xff1a; 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。 坚持不懈&#xff0c;越努力越幸运&#xff0c;大家一起学习鸭~~~ 在实际的业务开发中&#xff0c;容器的遍历可以说是非…...

云计算与云服务

云计算与云服务 1、云计算与云服务概述2、云服务模式(IaaS、PaaS、SaaS、DaaS)3、公有云、私有云和混合云1、云计算与云服务概述 什么是云计算? “云”实质上就是一个网络,狭义上讲,云计算就是一种提供资源的网络,使用者可以随时获取“云”上的资源,按需求量使用,并且…...

Ubuntu20.4 设置代理

主要是涉及2个代理 涉及apt 可以在、/etc/apt/apt.conf 中进行修改 在系统全局可以在/etc/profile中进行修改...

RustDay06------Exercise[71-80]

71.box的使用 说实话这题没太看懂.敲了个模板跟着提示就过了 // box1.rs // // At compile time, Rust needs to know how much space a type takes up. This // becomes problematic for recursive types, where a value can have as part of // itself another value of th…...

Leetcode—2525.根据规则将箱子分类【简单】

2023每日刷题&#xff08;五&#xff09; Leetcode—2525.根据规则将箱子分类 实现代码 char * categorizeBox(int length, int width, int height, int mass){long long volume;long long len (long long)length;long long wid (long long)width;long long heig (long lo…...

RustDay05------Exercise[51-60]

51.使用?当作错误处理符 ? 是 Rust 中的错误处理操作符。通常用于尝试解析或执行可能失败的操作&#xff0c;并在出现错误时提前返回错误&#xff0c;以避免程序崩溃或出现未处理的错误。 具体来说&#xff0c;? 用于处理 Result 或 Option 类型的返回值。 // errors2.rs…...

hdlbits系列verilog解答(或非门)-07

文章目录 wire线网类型介绍一、问题描述二、verilog源码三、仿真结果 wire线网类型介绍 wire线网类型是verilog的一种数据类型&#xff0c;它是一种单向的物理连线。它可以是输入也可以是输出&#xff0c;它与reg寄存器数据类型不同&#xff0c;它不能存储数据&#xff0c;只能…...

Node学习笔记之path模块

path 模块提供了 操作路径 的功能&#xff0c;我们将介绍如下几个较为常用的几个 API&#xff1a; API 说明 path.resolve 拼接规范的绝对路径常用 path.sep 获取操作系统的路径分隔符 path.parse 解析路径并返回对象 path.basename 获取路径的基础名称 path.dirname…...

使用LangChain与chatGPT API开发故事推理游戏-海龟汤

项目概述 海龟汤简述: 主持人提出一个难以理解的事件,玩家通过提问来逐步还原事件,主持人仅能告知玩家:“是、不是、是也不是、不重要”。引入chatGPT API原因 想通过程序自动化主持人,可通过chatGPT来判断玩家推理正确与否。LangChain是什么 LangChain是一个强大的框架,…...

用ChatGPT编写Excel函数公式进行表格数据处理分析,so easy!

在用Excel进行数据处理分析时&#xff0c;经常需要编写不同的公式&#xff0c;需要了解大量的函数。有了ChatGPT&#xff0c;就很简单了&#xff0c;直接用自然语言描述自己的需求&#xff0c;然后让ChatGPT写出公式就好了。 例子1&#xff1a; Excel某个单元格的内容是&#…...

超全全国所有城市人力资本测算数据集(1990-2021年)

参考《管理世界》中詹新宇&#xff08;2020&#xff09;的做法&#xff0c;本文对地级市的人力资本水平进行测算&#xff0c;其中人力资本水平用地级市的普通高等学校在校学生数占该地区总人口比重来反映 一、数据介绍 数据名称&#xff1a;地级市-人力资本测算 数据年份&…...

基于uniapp+WebSocket实现聊天对话、消息监听、消息推送、聊天室等功能,多端兼容

基于 ​UniApp + WebSocket​实现多端兼容的实时通讯系统,涵盖WebSocket连接建立、消息收发机制、多端兼容性配置、消息实时监听等功能,适配​微信小程序、H5、Android、iOS等终端 目录 技术选型分析WebSocket协议优势UniApp跨平台特性WebSocket 基础实现连接管理消息收发连接…...

爬虫基础学习day2

# 爬虫设计领域 工商&#xff1a;企查查、天眼查短视频&#xff1a;抖音、快手、西瓜 ---> 飞瓜电商&#xff1a;京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空&#xff1a;抓取所有航空公司价格 ---> 去哪儿自媒体&#xff1a;采集自媒体数据进…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...

以光量子为例,详解量子获取方式

光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学&#xff08;silicon photonics&#xff09;的光波导&#xff08;optical waveguide&#xff09;芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中&#xff0c;光既是波又是粒子。光子本…...

《C++ 模板》

目录 函数模板 类模板 非类型模板参数 模板特化 函数模板特化 类模板的特化 模板&#xff0c;就像一个模具&#xff0c;里面可以将不同类型的材料做成一个形状&#xff0c;其分为函数模板和类模板。 函数模板 函数模板可以简化函数重载的代码。格式&#xff1a;templa…...

scikit-learn机器学习

# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: # Also add the following code, # so that every time the environment (kernel) starts, # just run the following code: import sys sys.path.append(/home/aistudio/external-libraries)机…...

MySQL 部分重点知识篇

一、数据库对象 1. 主键 定义 &#xff1a;主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 &#xff1a;确保数据的完整性&#xff0c;便于数据的查询和管理。 示例 &#xff1a;在学生信息表中&#xff0c;学号可以作为主键&#xff…...

Neko虚拟浏览器远程协作方案:Docker+内网穿透技术部署实践

前言&#xff1a;本文将向开发者介绍一款创新性协作工具——Neko虚拟浏览器。在数字化协作场景中&#xff0c;跨地域的团队常需面对实时共享屏幕、协同编辑文档等需求。通过本指南&#xff0c;你将掌握在Ubuntu系统中使用容器化技术部署该工具的具体方案&#xff0c;并结合内网…...

flow_controllers

关键点&#xff1a; 流控制器类型&#xff1a; 同步&#xff08;Sync&#xff09;&#xff1a;发布操作会阻塞&#xff0c;直到数据被确认发送。异步&#xff08;Async&#xff09;&#xff1a;发布操作非阻塞&#xff0c;数据发送由后台线程处理。纯同步&#xff08;PureSync…...

HTTPS证书一年多少钱?

HTTPS证书作为保障网站数据传输安全的重要工具&#xff0c;成为众多网站运营者的必备选择。然而&#xff0c;面对市场上种类繁多的HTTPS证书&#xff0c;其一年费用究竟是多少&#xff0c;又受哪些因素影响呢&#xff1f; 首先&#xff0c;HTTPS证书通常在PinTrust这样的专业平…...