springcloud alibab + sentinel1.8.1集成文档

集成版本说明:
springboot 2.5.2
spring cloud 2020.0.5
spring cloud alibaba 2.2.6.RELEASE
sentinel 1.8.1
nacos 1.4.2
spring cloud gateway

sentinel:分布式系统的流量防卫兵。随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。

histrix:断路性,主要是做对于服务链路的保护和整个微服务系统的保护。

sentinel:以流量为切入点,可以做流量控制(控制到每一秒的请求数,或者是并发的线程数),熔断(对调用链路的保护,当A=>B=>C时,C因为超时、异常等挂了,导致请求发生堆积,久而久之B长时间等待C返回中,B有可能把自己的线程资源耗尽,导致微服务出现许多现象,所以当C出现类似现象时,要立即熔断,快速做rollback,防止资源靠近)降级(熔断必然会导致降级,降级是指将系统中的边缘服务关闭,而保证系统的核心服务的运行,降级从一整个大的角度来讲是对整个系统的保护,而熔断是对链路的保护)

一、sentinel

1、sentinel dashboard的安装
1、下载
  • https://github.com/alibaba/Sentinel/releases

在这里插入图片描述

2、启动
  • 仪表盘是个jar包可以直接通过java -jar 的命令启动 ,默认端口为8080

  • java -jar sentinel-dashboard-1.8.0.jar

  • 默认账号密码为:sentinel sentinel
    如果想要修改默认的账号密码和端口,可增加参数

  • Dsentinel.dashboard.auth.username=sentinel

  • Dsentinel.dashboard.auth.password=123456

  • Dserver.port=9090

java -Dserver.port=9090 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456 -jar sentinel-dashboard.jar

  • 添加sentinel对网关的控制
    -Dcsp.sentinel.app.type=1

在这里插入图片描述

3、访问web页面,登录
  • http://localhost:9090/#/login
    默认账号密码为:sentinel sentinel
    如果想要修改默认的账号密码,可增加参数

  • Dsentinel.dashboard.auth.username=sentinel

  • Dsentinel.dashboard.auth.password=123456
    java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

本地启动,访问:localhost:9090,输入账户密码登录后,可以看到如下页面:
在这里插入图片描述

2、sentinel实时监控服务
1、创建项目引入依赖
<properties>    
 <java.version>1.8</java.version>   
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <springboot.version>2.5.2</springboot.version>		<spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
 <spring.cloud.version>2020.0.5</spring.cloud.version>
</properties>

<!--引入sentinel依赖--> 
<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>  
</dependency>

<!-- 引入nacos discover依赖-->  
<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 
</dependency>


<!-- 全局维护版本--> 
<dependencyManagement>    
<dependencies>
	<!-- Spring Cloud -->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring.cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
  </dependency>

	<!-- Spring Cloud Alibaba -->
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring.cloud.alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
  </dependency>   
  <dependency>
      <!-- Import dependency management from Spring Boot -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>${springboot.version}</version>
      <type>pom</type>
      <scope>import</scope> 			
 </dependency>  
</dependencies> 
</dependencyManagement> ```
  • sentinel默认为延时加载,不会在启动之后立即创建服务监控,需要对服务进行调用才会初始化
    在这里插入图片描述
2、添加sentinel到配置文件

其中,spring.cloud.sentinel.eager=true 可使 你的SpringCloud应用启动时,直接与Sentinel建立心跳连接,访问sentinel 控制台就可以看到服务连接情况,不需要第一次访问应用的某个接口,才连接sentinel。

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:9090 #指定dashboard的ip端口
        port: 8719 #sentinel日志端口
      eager: true #直接连接sentinel
3、建立controller
@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${server.port}")
    private Integer port;

	/*
	直接调用/product/getProduct,sentinel就会监控在某一时刻/product/getProduct的并发访问,有可能是并发的线程数,有可能是并发的请求数
	监控以后会把sentinel的日志传给sentinel dashboard,dashboard拿到日之后会进行分析
	*/
    @GetMapping("/getProduct")
    public Result getProduct(@RequestParam("id") Integer id){
        return Result.success("端口:" + port + ",id:" + id);
    }

}
3、sentinel流量控制
3.1、sentinel服务限流

在这里插入图片描述

流控效果:

  • 快速失败:返回失败的方法,未定义则返回默认信息。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • warm up:单机阈值是指每秒设定只能处理20个请求,如果每秒请求数超过了20,那么经过10秒的预热后,才会开始每秒接受20个请求,初始接受请求数是20/3,其他请求直接抛弃。慢慢的变多,10秒的预热时长后,才能接受20个。

 [
在这里插入图片描述

  • 排队等待:单机阈值是指设定每秒只能处理20条请求,如果超过20条就排队等待,等待超过3000毫秒的,直接抛弃。

在这里插入图片描述

3.2、sentinel服务熔断

在这里插入图片描述

  • 慢调用比例:一秒内的请求数超过了5,并且超过50%的请求返回时间都大于了最大RT时间,则触发降级,打开熔断器,熔断窗口期为10s。

在这里插入图片描述

  • 异常比例:请求数超过了5条,且超过50%的请求都抛出异常,则触发熔断,程序降级,熔断时长为10s,超过10s后恢复。

在这里插入图片描述

  • 异常数:如果近一分钟的请求数大于5,且异常数达到2条,那么触发熔断,熔断时长为20秒。

在这里插入图片描述

3.3、sentinel服务热点规则

假设我们对一个接口进行热点参数限流访问,保护接口不被突发的流量击垮。
但如果对整个接口设置,假定接口支持最大qps=1000/s,那么有2个问题:

  • 当流量高峰来临,接口超过1000/s时,这时部分请求会被限流然后快速失败,但因为是对整个接口做的限流,这时访问非热点商品,也可能出现限流,影响了用户体验
  • 可能项目里会对热点商品查询做单独的优化,比如缓存等,它比普通商品详情接口而言能承担更高的qps阈值,对整个接口设置限流阈值粒度太粗,设高了可能应用拖垮,设低了优化后的程序没利用起来

因此需要用到sentinel热点参数规则。但Sentinel的热点限流规则本来是用于热点数据场景的,但目前对sentinel-web-servlet(基于普通servlet)和sentinel-spring-webmvc-adapter(基于springmvc)两种适配都不支持。
因此以下只做配置说明:
在这里插入图片描述
在 sentinel-dashboard 中添加热点规则
在这里插入图片描述

  • 资源名(getProductDetail):@SentinelResource(value = “getProductDetail”) 中的 value 值;
  • 参数索引(0):针对该接口的第 0 个参数;
  • 单机阈值(50):不是 1 的情况下,允许的 QPS 就是 10;
  • 参数类型(int):索引位置是 0 的参数的类型是 int;
  • 参数值(74829):如果该接口的第0个参数的值是74829;
  • 限流阈值(100):允许的 QPS 就是100;
  • 统计时间窗口(1):统计的时间单位,一般都是 1s;

配完了之后,对于被 @SentinelResource(value = “getProductDetail”) 标注的接口,如果参数 id 带的值是 74829,那么允许它的 QPS 就是 100;对于 id 值是其他的值的请求,允许其 QPS 就是 50;

3.4、sentinel系统规则

在这里插入图片描述
系统规则和热点规则不一样,热点规则是针对方法设定的,系统规则是针对一个应用设定的;
系统规则的 5 中预置类型
在这里插入图片描述

  • LOAD:只有在 Linux 系统的机器上才会生效,可以根据当前操作系统的负载,来决定是否触发保护(把请求拒绝掉);
  • RT:这个应用上,所有请求的平均响应时间,如果超过某个值,就停止新的请求;
  • 线程数:这个应用上,所有的请求消耗的线程数加起来,如果超过某个值,就停止新的请求;
  • 入口 QPS:这个应用上,所有接口的 QPS 加起来,如果超过某个值,就停止新的请求;
  • CPU 使用率:CPU 的使用率,如果超过一个百分比,就停止新的请求;

发生系统规则中配置的情况的时候,会把整个应用都断掉,所有的接口对不能对外提供服务了,这个设计很少用,因为粒度太大了,用 Sentinel 一般都是做细粒度的维护,如果设置了系统规则,可能自己都不知道怎么回事,系统就用不了了

3.5、sentinel服务授权规则
  • resource:资源名,即限流规则的作用对象
  • limitApp:对应的黑名单/白名单,不同 origin 用逗号分隔,如 appA,appB
  • strategy:限制模式,AUTHORITY_WHITE 为白名单模式,AUTHORITY_BLACK 为黑名单模式,默认为白名单模式

在这里插入图片描述
如果配置的策略是黑名单且requester在配置在limitApp中,则请求拦截;如果配置的策略是白名单且requester在配置不在limitApp中,则请求拦截;否则请求不拦截。

二、springcloud gateway整合nacos

1、nacos下载

下载后解压,打开bin目录双击startup.cmd或者进入cmd命令行敲startup.cmd命令启动,若启动失败,修改nacos配置,找到set MODE="cluster"把集群改为单机:

rem set MODE="cluster"
 
set MODE="standalone"
2、网关服务注册到nacos
2.1、添加依赖:
		<!-- Spring Cloud Gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

		<!--loadbalancer-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

		<!-- Spring Cloud Nacos Config -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>

		<!-- Spring Cloud Nacos Discovery -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		</dependency>
2.2、修改网关配置文件注册网关服务到nacos中:

bootstrap.yml:

spring:
  application:
    name: sdp-gateway
  profiles:
    active: dev

bootstrap-dev.yml:

spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yaml
      discovery:
        server-addr: localhost:8848

application.yml:

spring:
  application:
    name: sdp-gateway
  profiles:
    active: dev

application-dev.yml(暂时将网关路由的分发配置在项目中):

server:
  port: 12000
spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:9090
      eager: true
    gateway:
#      discovery:
#        locator:
#          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
        - id: sdpproject
          uri: lb://sdpserver
          predicates:
            - Path=/sdpserver/**

management:
  endpoints:
    web:
      exposure:
        include: '*' #暴露所有路由规则

2.3:同样的方法sdpserver服务注册到nacos服务中
2.4:打开nacos控制台查看是否已注册:

在这里插入图片描述

三、springcloud gateway整合sentinel网关限流实现

1、添加alibaba对gateway-sentinel的集成依赖:
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

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

        <!--sentinel gateway-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

        <!--sentinel gateway adapter-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-parameter-flow-control</artifactId>
        </dependency>
2、修改网关启动器:

在这里插入图片描述

加上System.setProperty(“csp.sentinel.app.type”, “1”);重启gateway服务,添加sentinel对网关的控制,启动后,进入sentinel控制台查看网关服务是否被sentinel所监控到。
在这里插入图片描述

3、网关服务限流:
3.1、以routeID为api类型,进行流量控制。

在这里插入图片描述
在这里插入图片描述

3.2 以API分组为api类型

就是对接口进行分组,然后对不同组的接口,实施不同的限流策略。
添加API分组,对API进行限流,需要先对API进行分组。如下图所示:
在这里插入图片描述

配置模式:精确、前缀和正则三种模式。

  • 精确模式是指对URL的路径完全匹配时,进行限流。例如,匹配串配置为/sdpserver/1

  • 前缀模式是指对URL的路径前缀匹配时,进行限流。例如,匹配串配置为/sdpserver/*

  • 正则模式是指对URL的路径符合正则表示式规则时,进行限流。例如,匹配串配置为/sdpserver/\d*

4、配置限流规则

根据API分组配置限流规则,API名称可以选择不同的API分组,进行配置。

四、流控数据持久化到nacos

1、添加依赖:
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-parameter-flow-control</artifactId>
        </dependency>
2、配置限流规则:
spring: 
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:9090
      eager: true
      datasource:
        flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: flow
        degrade:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
        system:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-system-rules
            groupId: SENTINEL_GROUP
            rule-type: system
        authority:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-authority-rules
            groupId: SENTINEL_GROUP
            rule-type: authority
        param-flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-param-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: param-flow
        gw-flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-gw-rules
            groupId: SENTINEL_GROUP
            rule-type: gw-flow
        gw-api-group:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-gw-api-rules
            groupId: SENTINEL_GROUP
            rule-type: gw-api-group
3、限流测试
3.1、初始加载nacos配置

1、nacos有如下配置文件,对资源“/rest/config/bar”设置限流规则。

在这里插入图片描述

2、启动sentinel后,可以看到sentinel控制台已经读取到sdprest-flow-rules的限流规则。
在这里插入图片描述

3.2、有nacos的配置情况下,对sentinel控制台修改

1、添加一条新的限流规则

在这里插入图片描述

2、新增后,可以看见nacos配置文件中,已经新增了这条配置。

在这里插入图片描述

3.3 无nacos的配置情况,对sentinel控制台修改

1、如图在nacos中删除这条流控规则。
在这里插入图片描述
2、可看见sentinel控制台流控规则已清空。
在这里插入图片描述
3、在sentinel控制台添加一条规则。

在这里插入图片描述

4、回到nacos中可以看见,限流规则已创建。
在这里插入图片描述
在这里插入图片描述

naco配置参数说明:

  • resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。

  • resourceMode:规则是针对 API Gateway 的 route(RESOURCE_MODE_ROUTE_ID)还是用户在 Sentinel 中定义的 API 分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是 route。

  • grade:限流指标维度,同限流规则的grade字段。

  • count:限流阈值

  • intervalSec:统计时间窗口,单位是秒,默认是 1 秒。

  • controlBehavior:流量整形的控制效果,同限流规则的controlBehavior字段,目前支持快速失败和匀速排队两种模式,默认是快速失败。

  • burst:应对突发请求时额外允许的请求数目。

  • maxQueueingTimeoutMs:匀速排队模式下的最长排队时间,单位是毫秒,仅在匀速排队模式下生效。

  • paramItem
    

    :参数限流配置。若不提供,则代表不针对参数进行限流,该网关规则将会被转换成普通流控规则;否则会转换成热点规则。其中的字段:

    • parseStrategy:从请求中提取参数的策略,目前支持提取来源 IP(PARAM_PARSE_STRATEGY_CLIENT_IP)、Host(PARAM_PARSE_STRATEGY_HOST)、任意 Header(PARAM_PARSE_STRATEGY_HEADER)和任意 URL 参数(PARAM_PARSE_STRATEGY_URL_PARAM)四种模式。
    • fieldName:若提取策略选择 Header 模式或 URL 参数模式,则需要指定对应的 header 名称或 URL 参数名称。
    • pattern:参数值的匹配模式,只有匹配该模式的请求属性值会纳入统计和流控;若为空则统计该请求属性的所有值。(1.6.2 版本开始支持)
    • matchStrategy:参数值的匹配策略,目前支持精确匹配(PARAM_MATCH_STRATEGY_EXACT)、子串匹配(PARAM_MATCH_STRATEGY_CONTAINS)和正则匹配(PARAM_MATCH_STRATEGY_REGEX)。(1.6.2 版本开始支持)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值