Spring Cloud系列——使用Sentinel进行微服务保护

简介: Spring Cloud系列——使用Sentinel进行微服务保护

Spring Cloud系列——使用Sentinel进行微服务保护

 

概述

 

Spring Cloud Sentinel 是一个强大的微服务保护框架,提供流量控制、熔断降级、系统负载保护和热点流量防护等功能,帮助开发者保障微服务的高可用性和稳定性。Sentinel 可以与 Spring Cloud 生态系统无缝集成,是分布式系统开发中的重要工具。

 

Sentinel 主要功能

 

1. **流量控制**:基于流量大小和请求速率进行限流。

2. **熔断降级**:根据响应时间和失败率进行熔断处理。

3. **系统负载保护**:基于系统负载进行保护,防止系统过载。

4. **热点流量防护**:对热点参数进行限流保护,防止特定参数的流量激增。

 

环境配置

 

引入依赖

 

在 Spring Cloud 项目中引入 Sentinel 相关依赖。可以在 `pom.xml` 文件中添加以下依赖:

 

```xml
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
```

 

配置文件

 

在项目的配置文件(如 `application.yml`)中添加 Sentinel 的相关配置:

 

```yaml
spring:
  application:
    name: sentinel-demo
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080
      filter:
        enabled: true
```

 

启动 Sentinel Dashboard

 

Sentinel 提供了可视化的 Dashboard 用于监控和管理。可以通过下载 Sentinel Dashboard 的 jar 包并运行:

 

```sh
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
```

 

使用 Sentinel 进行微服务保护

 

流量控制

 

通过注解的方式进行限流控制。例如,对某个服务接口进行限流:

 

```java
@RestController
public class HelloController {
 
    @GetMapping("/hello")
    @SentinelResource(value = "hello", blockHandler = "handleFlowQpsException")
    public String hello() {
        return "Hello Sentinel";
    }
 
    public String handleFlowQpsException(BlockException e) {
        return "Too many requests, please try again later.";
    }
}
```

 

在 Sentinel Dashboard 中,可以对该接口设置流量控制规则。

 

熔断降级

 

通过注解的方式进行熔断降级。例如,对某个服务接口进行熔断处理:

 

```java
@RestController
public class HelloController {
 
    @GetMapping("/hello")
    @SentinelResource(value = "hello", fallback = "fallbackHandler")
    public String hello() {
        if (new Random().nextBoolean()) {
            throw new RuntimeException("Service exception");
        }
        return "Hello Sentinel";
    }
 
    public String fallbackHandler(Throwable e) {
        return "Service is unavailable, please try again later.";
    }
}
```

 

在 Sentinel Dashboard 中,可以对该接口设置熔断降级规则。

 

系统负载保护

 

可以在 Sentinel Dashboard 中配置系统保护规则,防止系统过载。系统保护规则包括 CPU 使用率、系统平均响应时间、入口流量等指标。

 

热点流量防护

 

对某个接口的特定参数进行限流保护。例如,对用户登录接口进行参数限流:

 

```java
@RestController
public class LoginController {
 
    @PostMapping("/login")
    @SentinelResource(value = "login", blockHandler = "handleHotParamException")
    public String login(@RequestParam String username) {
        return "Welcome " + username;
    }
 
    public String handleHotParamException(String username, BlockException e) {
        return "Too many requests with username: " + username;
    }
}
```

 

在 Sentinel Dashboard 中,可以对 `username` 参数设置热点限流规则。

 

示例代码

 

以下是一个完整的示例代码,演示如何使用 Sentinel 进行流量控制、熔断降级、系统负载保护和热点流量防护。

 

```java
package com.example.sentineldemo;
 
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Random;
 
@RestController
public class DemoController {
 
    @GetMapping("/hello")
    @SentinelResource(value = "hello", blockHandler = "handleFlowQpsException", fallback = "fallbackHandler")
    public String hello() {
        if (new Random().nextBoolean()) {
            throw new RuntimeException("Service exception");
        }
        return "Hello Sentinel";
    }
 
    public String handleFlowQpsException(BlockException e) {
        return "Too many requests, please try again later.";
    }
 
    public String fallbackHandler(Throwable e) {
        return "Service is unavailable, please try again later.";
    }
 
    @PostMapping("/login")
    @SentinelResource(value = "login", blockHandler = "handleHotParamException")
    public String login(@RequestParam String username) {
        return "Welcome " + username;
    }
 
    public String handleHotParamException(String username, BlockException e) {
        return "Too many requests with username: " + username;
    }
}
```

 

总结

 

通过引入 Sentinel 并在 Spring Cloud 项目中进行配置和使用,可以有效保护微服务系统的高可用性和稳定性。Sentinel 提供的流量控制、熔断降级、系统负载保护和热点流量防护功能,可以帮助开发者在分布式系统中应对各种突发情况,保障系统的平稳运行。

目录
相关文章
|
23小时前
|
Java Nacos 微服务
Spring Cloud微服务在Windows本地开发时禁用Nacos注册中心注册
Spring Cloud微服务在Windows本地开发时禁用Nacos注册中心注册
4 0
|
1天前
|
Java API 数据格式
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
17 1
|
1天前
|
缓存 Java 微服务
Spring Cloud中@RefreshScope实现动态刷新的原理
Spring Cloud中@RefreshScope实现动态刷新的原理
5 0
|
1天前
|
缓存 负载均衡 算法
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day3 全网最全(2)
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day3 全网最全(2)
7 1
|
1天前
|
程序员 测试技术 Docker
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day3 全网最全(1)
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day3 全网最全(1)
10 1
|
1天前
|
关系型数据库 MySQL Shell
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day2 全网最快最全(下)
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day2 全网最快最全(下)
4 0
|
1天前
|
Java 程序员 Docker
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day2 全网最快最全(上)
黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day2 全网最快最全(上)
5 0
|
1天前
|
JSON Java 程序员
马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day1最快 最全(2)
马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day1最快 最全(2)
7 3
|
1天前
|
SQL Java 程序员
马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day1最快 最全(1)
马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录Day1最快 最全(1)
5 1
|
2天前
|
自然语言处理 监控 开发者
springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级
springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级
7 0