sentinel 整合spring cloud限流的过程解析

spring cloud基于http进行服务调用,大致过程如下:

  • 服务提供端:提供http接口,并向服务中心注册服务信息
  • 服务消费端:将服务端的http接口作为本地服务,从注册中心读取服务提供端信息,使用feign发起远程调用

相关依赖

           <!-- 服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

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

示例

为简化处理,直接对url接口进行限流,不做服务调用

application.yml

spring:
  application:
    name: hello-sentinel
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8081

限流可使用本地配置、或者sentinel dashboard配置

HelloController

@RestController
public class HelloController {

    @SentinelResource(value = "hello", blockHandler = "blockHandle")
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
    public String blockHandle(BlockException e){
        e.printStackTrace();
        return "被限流了";
}

************

本地限流配置

CustomFlowRule

public class CustomFlowRule implements InitFunc {

    @Override
    public void init() throws Exception {
        List<FlowRule> flowRules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("hello");
        flowRule.setCount(1);
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        flowRules.add(flowRule);
        FlowRuleManager.loadRules(flowRules);
    }
}

META-INF/services/com.alibaba.csp.sentinel.init.InitFunc

com.example.demo.rule.CustomFlowRule

jmeter 测试

2022-03-27 22:30:50.534  INFO 1791 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-27 22:30:50.547  INFO 1791 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-27 22:30:50.557  INFO 1791 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.227 seconds (JVM running for 2.824)
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-27 22:31:04.049  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

************

sentinel dashboard配置限流

启动sentinel dashboard

java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

参数说明:
-Dserver.port=8081:指定控制台启动端口
-Dcsp.sentinel.dashboard.server:指定控制台地址和端口
-Dproject.name=sentinel-dashboard:指定控制台项目名称

localhost:8081,控制台配置流控策略

说明:若需使用本地降级方法,需在下方的hello配置流控规则

jmeter 测试

2022-03-28 08:50:29.165  INFO 853 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-28 08:50:29.198  INFO 853 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-28 08:50:29.210  INFO 853 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.315 seconds (JVM running for 4.03)
2022-03-28 08:52:05.792  INFO 853 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-28 08:52:05.793  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-28 08:52:05.802  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

到此这篇关于sentinel 整合spring cloud限流的过程解析的文章就介绍到这了,更多相关sentinel 整合spring cloud限流内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • spring cloud gateway整合sentinel实现网关限流

    这篇文章主要介绍了spring cloud gateway整合sentinel实现网关限流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentinel流控可以放在gateway网关端,也可以放在各微服务端. 1,以父工程为基础,创建子工程 2,添加pom依赖

  • Spring Cloud Alibaba之Sentinel实现熔断限流功能

    微服务中为了防止某个服务出现问题,导致影响整个服务集群无法提供服务的情况,我们在系统访问量和业务量高起来了后非常有必要对服务进行熔断限流处理. 其中熔断即服务发生异常时能够更好的处理:限流是限制每个服务的资源(比如说访问量). spring-cloud中很多使用的是Hystrix组件来进行限流的,现在我们这里使用阿里的sentinel来实现熔断限流功能. sentinel简介 这个在阿里云有企业级的商用版本 应用高可用服务 AHAS:现在有免费的入门级可以先体验下,之后再决定是否使用付费的专业版

  • SpringCloud中使用Sentinel实现限流的实战

    目录 前言 正文 Sentinel Sentinel的限流原理 第一步:部署sentinel-dashboard 第二步:在项目中整合sentinel 前言 在分布式的项目中经常会遇到那种高并发的场景,为了保证系统不会被突然激增的请求导致宕机,我们常常会使用一种服务降级的手段来保护我们的系统,本篇博客将介绍如何使用SpringCloud中使用Sentinel实现限流,从而达到服务降级的目的. 正文 Sentinel Sentinel 是面向微服务的轻量级流量控制框架,从流量控制.熔断降级.系统负

  • SpringCloud-Alibaba-Sentinel服务降级,热点限流,服务熔断

    前言: 除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.一个服务常常会调用别的模块,可能是另外的一个远程服务.数据库,或者第三方 API 等.例如,支付的时候,可能需要远程调用银联提供的 API:查询某个商品的价格,可能需要进行数据库查询.然而,这个被依赖服务的稳定性是不能保证的.如果依赖的服务出现了不稳定的情况,请求的响应时间变长,那么调用服务的方法的响应时间也会变长,线程会产生堆积,最终可能耗尽业务自身的线程池,服务本身也变得不可用 熔断策略 Sentin

  • sentinel 整合spring cloud限流的过程解析

    spring cloud基于http进行服务调用,大致过程如下: 服务提供端:提供http接口,并向服务中心注册服务信息 服务消费端:将服务端的http接口作为本地服务,从注册中心读取服务提供端信息,使用feign发起远程调用 相关依赖 <!-- 服务注册与发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibab

  • Spring cloud 限流的多种方式

    在频繁的网络请求时,服务有时候也会受到很大的压力,尤其是那种网络攻击,非法的.这样的情形有时候需要作一些限制.例如:限制对方的请求,这种限制可以有几个依据:请求IP.用户唯一标识.请求的接口地址等等. 当前限流的方式也很多:Spring cloud 中在网关本身自带限流的一些功能,基于 redis 来做的.同时,阿里也开源了一款:限流神器 Sentinel.今天我们主要围绕这两块来实战微服务的限流机制. 首先讲 Spring cloud 原生的限流功能,因为限流可以是对每个服务进行限流,也可以对

  • Spring Cloud Zuul添加过滤器过程解析

    这篇文章主要介绍了Spring Cloud Zuul添加过滤器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Zuul作为网关的其中一个重要功能,就是实现请求的鉴权.而这个动作我们往往是通过Zuul提供的过滤器来实现的. 一.过滤器方法的作用 想要使用Zuul实现过滤功能,我们需要自定义一个类继承ZuulFilter类,并实现其中的四个方法,我们先看一下这四个方法的作用是什么 public class MyFilter extends

  • Spring Cloud Feign组成配置过程解析

    Feign的组成 接口 作用 默认值 Feign.Builder Feign的入口 Feign.Builder Client Feign底层用什么去请求 和Ribbon配合时:LoadBalancerFeignClient 不和Ribbon配合时:Fgien.Client.Default Contract 契约,注解支持 SpringMVCContract Encoder 解码器,用于将独享转换成HTTP请求消息体 SpringEncoder Decoder 编码器,将相应消息体转成对象 Res

  • Springboot整合Spring Cloud Kubernetes读取ConfigMap支持自动刷新配置的教程

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Docker & Kubernetes相关文章:容器技术 之前介绍了Spring Cloud Config的用法,但对于Kubernetes应用,可能会需要读取ConfigMap的配置,我们看看Springboot是如何方便地读取ConfigMap和Secret. 2 整合Spring Cloud Kubenetes Spring Cloud Kubernetes提供了Spring Cloud应用与Kubernetes服

  • Spring Cloud oauth2 认证服务搭建过程示例

    目录 安装httpie 导入数据库脚本 sts中导入项目 修改 POM文件 修改配置文件 修改主类文件 编译,运行 测试 查看Redis缓存 安装httpie 安装httpie 需要 python 环境 pip install --upgrade httpie 进入D:\Project目录,在此目录下打开CMD,调用httpie,创建 oauth2 项目 http -d https://start.spring.io/starter.zip javaVersion==17 groupId==co

  • spring 声明式事务实现过程解析

    这篇文章主要介绍了spring 声明式事务实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 /** * 声明式事务: * * 环境搭建: * 1.导入相关依赖 * 数数据 * 3.给方法上标注 @Transactional 表示当前方法是一个事务方法: * 4. @EnableTransactionManagement 开启基于注解的事务管理功能:据源.数据库驱动.Spring-jdbc模块 * * 2.配置数据源.JdbcTempl

  • Spring Boot Logback配置日志过程解析

    这篇文章主要介绍了Spring Boot Logback配置日志过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 出于性能等原因,Logback 目前是springboot应用日志的标配: 当然有时候在生产环境中也会考虑和三方中间件采用统一处理方式. 配置时考虑点 支持日志路径,日志level等配置 日志控制配置通过application.yml下发 按天生成日志,当天的日志>50MB回滚 最多保存10天日志 生成的日志中Pattern自

  • spring cloud Ribbon用法及原理解析

    这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简介 这篇文章主要介绍一下ribbon在程序中的基本使用,在这里是单独拿出来写用例测试的,实际生产一般是配置feign一起使用,更加方便开发.同时这里也通过源码来简单分析一下ribbon的基本实现原理. 基本使用 这里使用基于zookeeper注册中心+ribbon的方式实现一个简单的客户端负载均衡案例. 服务提供方 首先是一个

  • Spring Boot启动流程断点过程解析

    这篇文章主要介绍了Spring Boot启动流程断点过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 启动入口 跟进run方法 : 一个用来使用默认的配置从特定的源运行SpringApplication的静态帮助类. 这个类有两个重载方法,另一个用来传入多个源.通常,单个参数方法是数组方法的一个特例 创建一个新的SpringApplication实例.这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化.

随机推荐