springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级

简介: springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级

官网

https://sentinelguard.io/zh-cn/docs/introduction.html

一、Sentinel介绍

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

二、监控端安装

1、下载地址

https://github.com/alibaba/Sentinel/releases

2、直接启动或者使用IDEA指定端口启动

3、登录(接口按照自己的写)

http://localhost:8858/#/login

账号密码均为:sentinel


三、客户端配置sentinel

1、引入依赖

<!--流量控制-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

2、修改配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloudstudy
    username: root
    password: 123456
  cloud:
    sentinel:
      transport:
        # 添加监控页面地址即可
        dashboard: localhost:8858
      # 关闭Context收敛,这样被监控方法可以进行不同链路的单独控制
      web-context-unify: false
      # 将刚刚编写的请求映射设定为限流页面
      block-page: /blocked

3、启动客户端调用接口

http://localhost:8301/borrow1/1

刷新控制台,查看实时监控

四、流控、熔断、热点

详情查看官方文档

https://sentinelguard.io/zh-cn/docs/flow-control.html

部分测试代码

 
 
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.minos.entity.UserBorrowDetail;
import com.test.service.BorrowService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.io.IOException;
 
@RestController
public class BorrowController {
 
    @Resource
    BorrowService service;
 
    @RequestMapping("/borrow1/{uid}")
    UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid) {
        return service.getUserBorrowDetailByUid(uid);
    }
 
    @RequestMapping("/borrow2/{uid}")
    UserBorrowDetail findUserBorrows2(@PathVariable("uid") int uid) {
        return service.getUserBorrowDetailByUid(uid);
    }
    @RequestMapping("/borrow3/{uid}")
    String findUserBorrows3(@PathVariable("uid") int uid) throws InterruptedException {
        Thread.sleep(1000);
        return "findUserBorrows3";
    }
    @RequestMapping("/borrow4/{uid}")
    String findUserBorrows4(@PathVariable("uid") int uid) throws InterruptedException {
       throw new RuntimeException("异常了");
    }
 
    @RequestMapping("/blocked")
    JSONObject blocked() {
        JSONObject object = new JSONObject();
        object.put("code", 403);
        object.put("success", false);
        object.put("massage", "您的请求频率过快,请稍后再试!");
        return object;
    }
 
    @RequestMapping("/test")
    @SentinelResource("test")
        //注意这里需要添加@SentinelResource才可以,用户资源名称就使用这里定义的资源名称
    String test(@RequestParam(value = "a", required = false) String a,
                @RequestParam(value = "b", required = false) String b,
                @RequestParam(value = "c", required = false) String c) {
        return "请求成功!a = " + a + ", b = " + b + ", c = " + c;
    }
 
    @RequestMapping("/test2")
    @SentinelResource(value = "test2",
            fallback = "except",    //fallback指定出现异常时的替代方案
            blockHandler = "black",//blockHandler执行位于except之前
            exceptionsToIgnore = IOException.class)
        //忽略那些异常,也就是说这些异常出现时不使用替代方案
    String test2() {
        return "test2";
    }
 
    //替代方法必须和原方法返回值和参数一致,最后可以添加一个Throwable作为参数接受异常
    String except(Throwable t) {
        return "except";
    }
 
    String black(BlockException t) {
        return "Block";
    }
}
- 直接:只针对于当前接口。
- 关联:当其他接口超过阈值时,会导致当前接口被限流。
- 链路:更细粒度的限流,能精确到具体的方法。

热点数据进行精准限流,比如在某一时刻,不同参数被携带访问的频率是不一样的:
 
- http://localhost:8301/test?a=10  访问100次
- http://localhost:8301/test?b=10  访问0次
- http://localhost:8301/test?c=10  访问3次

目录
相关文章
|
7天前
|
监控 Java Sentinel
使用Sentinel进行服务调用的熔断和限流管理(SpringCloud2023实战)
Sentinel是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
26 3
|
3天前
springCloud之服务降级熔断Hystrix、OpenFeign
springCloud之服务降级熔断Hystrix、OpenFeign
9 0
|
14天前
|
监控 Java API
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
23 0
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
|
25天前
|
人工智能 Java Spring
使用 Spring Cloud Alibaba AI 构建 RAG 应用
本文介绍了RAG(Retrieval Augmented Generation)技术,它结合了检索和生成模型以提供更准确的AI响应。示例中,数据集(包含啤酒信息)被加载到Redis矢量数据库,Spring Cloud Alibaba AI Starter用于构建一个Spring项目,演示如何在接收到用户查询时检索相关文档并生成回答。代码示例展示了数据加载到Redis以及RAG应用的工作流程,用户可以通过Web API接口进行交互。
52334 62
|
8天前
|
监控 Java 应用服务中间件
替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门
替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门
|
24天前
|
消息中间件 Java 持续交付
Spring Cloud Alibaba 项目搭建步骤和注意事项
Spring Cloud Alibaba 项目搭建步骤和注意事项
191 0
Spring Cloud Alibaba 项目搭建步骤和注意事项
|
22天前
|
存储 SpringCloudAlibaba 关系型数据库
springcloud alibaba(5)
springcloud alibaba
97 0
|
22天前
|
SpringCloudAlibaba Nacos
springcloud alibaba(4)
springcloud alibaba
124 0
|
22天前
|
SpringCloudAlibaba 容灾 测试技术
springcloud alibaba(3)
springcloud alibaba
68 0
|
22天前
|
SpringCloudAlibaba 负载均衡 前端开发
springcloud alibaba(2)
springcloud alibaba
76 0

热门文章

最新文章