SpringCloud-Sentinel

一、介绍

(1)提供界面配置配置服务限流、服务降级、服务熔断
(2)@SentinelResource的blockHandler只处理后台配置的异常,运行时异常fallBack处理,且资源名为value时才生效,走兜底方法

二、安装并启动sentinel

(1)官网
(2)运行java -jar sentinel-dashboard-1.8.6.jar
(3)访问http://localhost:8080/
SpringCloud-Sentinel_第1张图片
注:sentinel是懒加载的,访问接口后才会显示

三、搭建项目

(1)编写pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo20220821</artifactId>
        <groupId>com.wsh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wsh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

(2)编写application.yml

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719


management:
  endpoints:
    web:
      exposure:
        include: "*"

(3)编写启动类

package com.wsh.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @ClassName SentinelService8401
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelService8401 {
    public static void main(String[] args) {
        SpringApplication.run(SentinelService8401.class, args);
    }
}

(4)编写Controller

package com.wsh.springcloud.controller;

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

/**
 * @ClassName TestController
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@RestController
public class TestController {

    @GetMapping("/test")
    public String test(){
        return "test";
    }
}

四、服务限流

SpringCloud-Sentinel_第2张图片

(1)打开界面
SpringCloud-Sentinel_第3张图片
(2)阈值
QPS:每秒请求数
并发线程数:处理请求的线程数
SpringCloud-Sentinel_第4张图片
(3)关联
SpringCloud-Sentinel_第5张图片

/test炸了,/test1也炸

SpringCloud-Sentinel_第6张图片

(4)预热,阈值先从阈值/3开始,在预热时长(单位:秒)内逐渐上升
SpringCloud-Sentinel_第7张图片
(5)排队等待,阈值类型必须设置为QPS,超出阈值的请求将会排队,等待的超时时间设置为20秒
SpringCloud-Sentinel_第8张图片

五、服务降级

(1)满足条件后,服务在规定时间内熔断
(2)慢调用比例
SpringCloud-Sentinel_第9张图片
(3)异常比例
SpringCloud-Sentinel_第10张图片
(4)异常数
SpringCloud-Sentinel_第11张图片

六、热点参数限流

(1)要用@SentinelResource定义资源名和兜底方法

    @GetMapping("/test2")
    @SentinelResource(value = "test2", blockHandler = "test2_solve")
    public String test2(@RequestParam("name") String name) {
        log.info("test2");
        return "test2";
    }
    public String test2_solve(String name, BlockException blockException){
        return "block";
    }

(2)
SpringCloud-Sentinel_第12张图片
(3)定义规则时指定参数的值
SpringCloud-Sentinel_第13张图片

七、兜底方法编写方式优化

(1)

    @GetMapping("/test3")
    @SentinelResource(value = "test3",  blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve")
    public String test3() {
        log.info("test3");
        return "test3";
    }

(2)编写BlockHandler类

package com.wsh.springcloud.handler;

import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * @ClassName BlockHandler
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
public class BlockHandler {

    public static String test3_solve(BlockException blockException){
        return "block";
    }
}

(3)
SpringCloud-Sentinel_第14张图片

八、配置fallback、blockHandler

(1)exceptionsToIgnore 用于忽略异常,不走fallback

    @GetMapping("/test3")
    @SentinelResource(value = "test3", blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve",
    fallbackClass = FallBackHandler.class, fallback = "test3_solve1",
    exceptionsToIgnore = NullPointerException.class)
    public String test3(@RequestParam("name") String name) {
        if (name.equals("wsh")){
            throw new IllegalArgumentException();
        }
        return "test3";
    }

(2)

public class BlockHandler {

    public static String test3_solve(String name, BlockException blockException){
        return "block";
    }
}
public class FallBackHandler {

    public static String test3_solve1(String name, Throwable throwable){
        return "block1";
    }
}

九、配置openFeign

(1)编写pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo20220821</artifactId>
        <groupId>com.wsh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-order84</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wsh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

(2)编写application.yml

server:
  port: 84

spring:
  application:
    name: cloudalibaba-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: "*"

server-url: http://cloudalibaba-provider-payment

feign:
  sentinel:
    enabled: true

(3)编写启动类

package com.wsh.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @ClassName ConsumerOrder84
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerOrder84 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerOrder84.class, args);
    }
}

(4)编写PaymentService

package com.wsh.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @ClassName PaymentService
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@FeignClient(value = "cloudalibaba-provider-payment", fallback = PaymentServiceHandler.class)
public interface PaymentService {

    @GetMapping("/payment/test")
    public String test();
}

(5)编写fallback类

package com.wsh.springcloud.service;

import org.springframework.stereotype.Component;

/**
 * @ClassName PaymentServiceHandler
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@Component
public class PaymentServiceHandler implements PaymentService{
    @Override
    public String test() {
        return "fallback";
    }
}

(6)编写Controller

package com.wsh.springcloud.controller;

import com.wsh.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TestController
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/19
 * @Version V1.0
 **/
@RestController
public class TestController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/consumer/test")
    public String test(){
        return paymentService.test();
    }
}

(7)编写服务提供者Controller

package com.wsh.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TestController
 * @Description: TODO
 * @Author wshaha
 * @Date 2023/10/18
 * @Version V1.0
 **/
@RestController
public class TestController {

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

    @GetMapping("/payment/test")
    public String test(){
        int i = 1 / 0;
        return "test: " + port;
    }
}

(8)运行
SpringCloud-Sentinel_第15张图片

十、配置持久化

(1)将规则持久化到nacos保存,只能先在nacos里编写好才有效
(2)pom.xml增加依赖

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

(3)修改application.yml

    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719
      datasource:
        dsl:
          nacos:
            server-addr: localhost:8848
            dataId: cloudalibaba-sentinel-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

(4)nacos里创建配置
SpringCloud-Sentinel_第16张图片

[
    {
        "resource": "/consumer/test",
        "limitApp": "default",
        "grade": "1",
        "count": 1,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

SpringCloud-Sentinel_第17张图片

你可能感兴趣的:(SpringCloud,spring,cloud,sentinel,spring)