微服务架构(五)-- Sentinel限流熔断

目录

Sentinel简介

Sentinel概述

安装Sentinel服务

访问Sentinal服务

Sentinel限流入门

准备工作

Sentinel限流入门实践

Sentinel流控规则分析

流控模式

阈值类型

拓展

通过idea启动sentinel

 添加自定义异常处理


简介

Sentinel概述

背景:某些时候,服务器流量暴涨,导致服务无法正常运转,需要对流量进行控制

Sentinel是什么?(阿里推出一个流量控制平台,防卫兵)

安装Sentinel服务

第一步:打开sentinel下载网址,下载Jar包

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

第二步:启动运行sentinel

微服务架构(五)-- Sentinel限流熔断_第1张图片

微服务架构(五)-- Sentinel限流熔断_第2张图片

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

访问Sentinal服务

浏览器访问localhost:8180

微服务架构(五)-- Sentinel限流熔断_第3张图片

 微服务架构(五)-- Sentinel限流熔断_第4张图片

Sentinel限流入门

准备工作

第一步:在服务提供方添加依赖


    com.alibaba.cloud
    spring-cloud-starter-alibaba-sentinel

第二步:bootstrap.yml添加sentinel配置

spring:
  cloud:
    sentinel:
      transport:
         dashboard: localhost:8180 # 指定sentinel控制台地址。

第三步:创建一个用于演示限流操作的Controller对象

package com.jt.provider.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/provider")
public class ProviderSentinelController {
       @GetMapping("/sentinel01")
       public String doSentinel01(){
           return "sentinel test01";
       }
}

第四步:启动sca-provider服务,然后对指定服务进行访问,刷新sentinel 控制台,实时监控信息

微服务架构(五)-- Sentinel限流熔断_第5张图片

 微服务架构(五)-- Sentinel限流熔断_第6张图片

Sentinel限流入门实践

限流策略:当每秒请求次数>1时,做出限流处理

第一步:选择要限流的链路

微服务架构(五)-- Sentinel限流熔断_第7张图片

 第二步:设置限流策略

微服务架构(五)-- Sentinel限流熔断_第8张图片

 第三步:反复刷新访问你的服务,检测是否有限流信息输出

微服务架构(五)-- Sentinel限流熔断_第9张图片

Sentinel流控规则分析

  • QPS(Queries Per Second):每秒请求数达到单机阈值时,就会限流。
  • 线程数:线程数达到单机阈值时,就会限流。

流控模式

默认为直接->快速失败模式

直接模式:超出单机阈值时,限流,抛出异常

关联模式:关联资源超出单机阈值时,限流自己

链路模式:多条链路访问同一资源,对不同链路进行限流

关联模式

第一步:添加测试方法

@GetMapping("/sentinel02")
    public String doSentinel02(){
        return "sentinel test 02";
    }

 第二步:添加限流规则

微服务架构(五)-- Sentinel限流熔断_第10张图片

 第三步:打开两个测试窗口,对/provider/sentinel02进行访问,检查/provider/sentinel01的状态

微服务架构(五)-- Sentinel限流熔断_第11张图片

 链路模式

 第一步:创建一个ResourceService资源类

package com.jt.provider.service;
@Service
public class ResourceService{
    @SentinelResource("doGetResource")
    public String doGetResource(){
        return "doGetResource";
    }
}

 第二步:添加两个测试方法(相当于两条链路)调用该资源

    @Autowired
    private ResourceService resourceService;
    @GetMapping("/sentinel03")
    public String doSentinel03(){
        resourceService.doGetResource();
        return "sentinel 03 test";
    }
    
    @GetMapping("/sentinel04")
    public String doSentinel04(){
       resourceService.doGetResource();
       return "sentinel 04 test";
    }

  第三步:在bootstrap.yml中添加配置

        原因:如果不添加该配置,则会发生链路聚焦(只有一份资源,多个链路访问的为同一份,如果限流,所有链路均被限流)

微服务架构(五)-- Sentinel限流熔断_第12张图片

sentinel:
     web-context-unify: false #打散链路聚焦

添加配置之后,03,04虽然调用的是同一个资源,但不共享同一份资源 

微服务架构(五)-- Sentinel限流熔断_第13张图片

第四步:添加限流规则

微服务架构(五)-- Sentinel限流熔断_第14张图片

微服务架构(五)-- Sentinel限流熔断_第15张图片

第五步:访问测试

        结论:03限流,04不限流

微服务架构(五)-- Sentinel限流熔断_第16张图片

阈值类型

拓展

通过idea启动sentinel

微服务架构(五)-- Sentinel限流熔断_第17张图片

微服务架构(五)-- Sentinel限流熔断_第18张图片

-Dserver.port=8180 -Dcsp.sentinel.dashboard.server=localhost:8180 -Dproject.name=sentinel-dashboard -jar D:/software/sentinel-dashboard-1.8.0.jar(其中D:/software/是包含sentinel-dashboard-1.8.0.jar的路径名)

 添加自定义异常处理

第一步::controller调用service,service调用自定义异常处理方法

微服务架构(五)-- Sentinel限流熔断_第19张图片

微服务架构(五)-- Sentinel限流熔断_第20张图片

package com.jt.provider.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Service;

@Service
public class ResourceService {
    @SentinelResource(value = "doGetResoure",
            blockHandlerClass = ResourceBlockHandler.class,
            blockHandler = "doHandle")
    public String doGetResource(){
        return "do Get Resource";
    }
}

第二步::自定义异常处理类(底层会自动调用该类,默认异常处理类失效)

package com.jt.provider.service;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ResourceBlockHandler {
    public static String doHandle(BlockException ex){
        log.error("被限流了");
        return "访问太频繁了...";
    }
}

你可能感兴趣的:(微服务,微服务,架构,java)