【第27章】Spring Cloud之适配Sentinel


前言

Spring Cloud Alibaba 默认为 Sentinel 整合了 Servlet、RestTemplate、FeignClient 和 Spring WebFlux。Sentinel 在 Spring Cloud 生态中,不仅补全了 Hystrix 在 Servlet 和 RestTemplate 这一块的空白,而且还完全兼容了 Hystrix 在 FeignClient 中限流降级的用法,并且支持运行时灵活地配置和调整限流降级规则。

这里我们为提供者服务整合Sentinel,添加流量控制和服务降级,保证应用程序的健壮和稳定性。


一、准备

1. 引入依赖

<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>${spring-cloud-alibaba.version}</version>
</dependency>

2. 配置控制台信息

连接到控制台是为了更好地观察和使用Sentinel

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080

二、定义资源

1. Controller

package org.example.nacos.provider.controller;

import org.example.nacos.provider.service.SentinelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by zjg on 2024/8/26
 */
@RestController
public class SentinelController {
    @Autowired
    private SentinelService service;

    @GetMapping(value = "/hello/{name}")
    public String apiHello(@PathVariable("name") String name) {
        return service.sayHello(name);
    }
}

2. Service

package org.example.nacos.provider.service;

/**
 * Create by zjg on 2024/8/26
 */
public interface SentinelService {
    public String sayHello(String name);
}

3. ServiceImpl

这里我们使用注解@SentinelResource定义了一个资源

package org.example.nacos.provider.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.example.nacos.provider.service.SentinelService;
import org.springframework.stereotype.Service;

/**
 * Create by zjg on 2024/8/26
 */
@Service
public class SentinelServiceImpl implements SentinelService {
    @Override
    @SentinelResource(value = "sayHello")
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

三、访问控制台

1. 发起请求

在这里插入图片描述

2. 访问控制台

在这里插入图片描述

大家从控制台可以看到我们访问的请求和自定义的资源sayHello都已经成功添加到控制台,我们可以通过控制台进一步完成流控和熔断等等功能。


总结

回到顶部
更多内容请查看

这里讲一下为什么要使用注解去完成资源的定义,因为其他方式或多或少存在代码侵入,我感觉不友好,而使用注解的方式,可以很轻松地完成这个功能。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring.cloud.sentinel.filter.enabled是一个配置项,属于Spring Cloud Sentinel组件的配置之一,用于激活Spring Cloud Gateway中的Sentinel Filter。 Sentinel是一个开源的分布式系统的流量控制框架,可以在保护服务的稳定性和可靠性的同时,提供丰富的监控数据和报警能力。而Spring Cloud Gateway是一个基于Spring Boot构建的API网关的微服务组件,可以对进入的流量进行统一的配置过滤和处理。在实现流量控制的过程中,可以将SentinelSpring Cloud Gateway进行集成,使用Sentinel的限流与降级等能力,来达到对API流量的保护。 通过设置spring.cloud.sentinel.filter.enabled为true,可以激活Spring Cloud Gateway中的Sentinel Filter,使得API请求进入网关后,可以进行一系列的流量控制操作。对于需要进行限流、熔断、降级操作的API接口,在Filter加载时会进行相应的配置设置,对于流量达到某个量级或者是其他异常情况时,Sentinel Filter会进行相应的处理,比如限流、降级或者是返回异常信息等等。这样就能够有效的避免服务被高并发的流量压垮,从而提高其可靠性和稳定性。 除了配置spring.cloud.sentinel.filter.enabled之外,还需要进行其他的配置,比如定义Sentinel Dashboard的地址和端口,设置限流的规则等等。这些都需要根据具体的应用场景进行适配和配置。通过合理的配置和灵活的使用,可以将SentinelSpring Cloud Gateway完美的结合起来,构建出一个高稳定性、高可靠性的API网关。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值