AlibabaCloud-sentinel getway current limiting articles (1)

AlibabaCloud-sentinel current limit articles (1)

First, let's look at a few questions

  • What does AlibabaCloud sentinel do?

    Sentinel can be simply divided into Sentinel core library and Dashboard. The core library does not rely on Dashboard, but combined with Dashboard can achieve the best results.

    This article mainly introduces the use of Sentinel core library. If you want the fastest and most have a direct understanding, you can refer to the Beginner's Guide to get one of the most intuitive feelings.

    The resource we are talking about can be anything, a service, a method in a service, or even a piece of code. The use of Sentinel for resource protection is mainly divided into several steps:

    1. Define resources
    2. Define rules
    3. Check whether the rules are in effect

    First define the resources that may need to be protected (buried points), and then configure the rules. It can also be understood that as long as we have resources, we can flexibly define various flow control rules at any time. When coding, you only need to consider whether the code needs protection, and if protection is needed, define it as a resource.

    For mainstream frameworks, we provide adaptations. You only need to configure them according to the instructions in the adaptation, and Sentinel will define the provided services and methods as resources by default.

    To put it simply, the fuse current limit framework for service current limit, fuse, and resource protection

  • Why use sentinel?

    Most of it is because the springcloud version of the fuse Hystrix has been closed source, and they are all going in the direction of alibabacloud, so~ you know! Hahahaha

  • Several ways to use sentinel

    Sentinel can integrate Web Servlet, Dubbo, Spring Cloud, gRPC, Spring WebFlux, Reactor. The following introduces the cloud that integrates the nacos version, and briefly summarizes the use of sentinel in two ways:

    1: Based on the gateway route dimension

    2: Based on the Api dimension

These few questions, Xiaobai, let me answer briefly! Ha ha ha ha ha ha ha! ! ! ! !

Not much to say, let's talk about the integration of getway:

  • First, download the Alibaba sentinel console:

The version here is 1.8.0. Note that the fuse mechanism of sentinel 1.6.0 and below does not include business exceptions:

download link:

https://github-production-release-asset-2e65be.s3.amazonaws.com/128018428/31fc3d00-e329-11ea-8ee6-0f4c3d54c405?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200904%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200904T024259Z&X-Amz-Expires=300&X-Amz-Signature=311db8993367e884aea58afea95cb9617f64acb91912442c08e83acf026d414c&X-Amz-SignedHeaders=host&actor_id=33771942&key_id=0&repo_id=128018428&response-content-disposition=attachment%3B%20filename%3Dsentinel-dashboard-1.8.0.jar&response-content-type=application%2Foctet-stream

  • Start sentinel-dashboard-1.8.0.jar

    The default port is 8080, which can be modified by yourself, the default login name and password sentinel, sentinel

    java -jar sentinel-dashboard-1.8.0.jar -Dserver.port=8888
    
  • control panel

    Insert picture description here

  • Gateway dependencies, mainly related to sentinel, to start boot dependencies are added by themselves. Above 2.0X, the cloud uses the matching version.

\

<!--网关相关依赖-->
<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-gateway</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
     </dependency>
<!--断点暴露健康检查-->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>  
<!--sentinel 服务限流-->
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
     </dependency>

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

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-gateway</artifactId>
     </dependency>  
  • Nacos getway configuration, self-added gateway related

    spring:
      cloud:
        sentinel:
          transport:
            dashboard: http://localhost:8889 #控制台的端口url,通讯是用的tcp长协议
    #暴露端点,主要与sentinel相关的两个配置
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
    
    下面可参考我的配置:
    
    server:
      port: 8301
    spring:
      cloud:
        sentinel:
          transport:
            dashboard: http://localhost:8889  
        gateway:
          globalcors:
            corsConfigurations:
              '[/**]':
                allowedOrigins: "*"
                allowedMethods: "*"
                allowedHeaders: "*"
                allowCredentials: true
          routes:
            - id: lcw-Auth-Social
              uri: lb://lcw-Auth
              predicates:
                - Path=/auth/social/**
              filters:
                - name: Hystrix
                  args:
                    name: socialfallback
                    fallbackUri: forward:/fallback/lcw-Auth
            - id: lcw-Auth
              uri: lb://lcw-Auth
              predicates:
                - Path=/auth/**
              filters:
                - name: Hystrix
                  args:
                    name: authfallback
                    fallbackUri: forward:/fallback/lcw-Auth            
            - id: lcw-Server-System
              uri: lb://lcw-Server-System
              predicates:
                - Path=/system/**
              filters:
                - name: Hystrix
                  args:
                    name: systemfallback
                    fallbackUri: forward:/fallback/lcw-Server-System
            - id: lcw-Server-Generator
              uri: lb://lcw-Server-Generator
              predicates:
                - Path=/generator/**
              filters:
                - name: Hystrix
                  args:
                    name: generatorfallback
                    fallbackUri: forward:/fallback/lcw-Server-Generator
            - id: lcw-Server-Job
              uri: lb://lcw-Server-Job
              predicates:
                - Path=/job/**
              filters:
                - name: Hystrix
                  args:
                    name: jobfallback
                    fallbackUri: forward:/fallback/lcw-Server-Job
            - id: lcw-Server-test
              uri: lb://lcw-Server-Test
              predicates:
                - Path=/test/**
              filters:
                - name: Hystrix
                  args:
                    name: testfallback
                    fallbackUri: forward:/fallback/lcw-Server-Test
          loadbalancer:
            use404: true
          default-filters:
            - StripPrefix=1
            - FebsDocGatewayHeaderFilter
    #  autoconfigure:
    #    exclude: org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
        
    # 网关增强配置
      data:
         mongodb:
           host: ${
          
          mongo.url}
           port: 27017
           database: febs_cloud_route
    
      redis:
        database: 0
        host: ${
          
          redis.url}
        port: 6379
        username: root
        password: 123456
        lettuce:
          pool:
            min-idle: 8
            max-idle: 500
            max-active: 2000
            max-wait: 10000
        timeout: 5000
    
    # 网关增强配置
    febs:
      gateway:
        enhance: true
        jwt:
          secret: 123456
          expiration: 36000
      doc:
        gateway:
          enable: true
          resources: "lcw-Server-System,lcw-Server-Test,lcw-Auth,lcw-Server-Generator,lcw-Server-Job"
    
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 10000
        socialfallback:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 60000    
    
    ribbon:
      eager-load:
        enabled: true
    
    management:
      endpoint:
        health:
          show-details: ALWAYS
      endpoints:
        web:
          exposure:
            #include: health,info,gateway,'*'
            include: '*'
    
    
    

    Configure current limiting rules

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-GLXqHnNn-1599203323001) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images) ]

    Set the gateway current limit:[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-o69VVlrT-1599203323002) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images) ]

For source is behind the service application suffix. The unit threshold refers to how many requests are limited per second, and the flow control rules are found after clicking OK.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-zUhSvMPo-1599203323005) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images) ]

Configure resource name, resource path:

  • Write test controller

    [External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-AwxP2vra-1599203323006) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images) ]

  • Test interface

    Here I use the Apache JMeter test to simulate concurrency.

Insert picture description here

Configure the number of threads in the thread group:

Insert picture description here

  • Run test

    [External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-aooyYUSJ-1599203323009) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20200904143916924.png)]

  • Console output

Insert picture description here

Hey, how come there are three? Wasn't it the 2 set just now? Hahahaha. This is two of the units 1s can pass. Normally there are two.

In the case of Api, the service interface current limit will do. Now I have to deal with the return to the fruit. Continue to say pull later!
There are also many current limiting methods, such as manggo plus redis.
Let’s talk about the idea. The current limiting blacklist is nothing more than ip+url. The rules are stored in manggo, read to the cache, and the redis data is judged every time. Write a timer. The blacklist ip is a unique identifier. The original URL of the current limit is a unique identifier. The limit was not reached this time. incr line.
Of course, look at the official documentation:

https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8

https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel

Guess you like

Origin blog.csdn.net/qq_38893133/article/details/108404636