【Spring Cloud Alibaba】9 - OpenFeign集成Sentinel实现服务降级

温馨提示:全套教程请查看 教程总览

一、简介

Sentinel 是什么

Sentinel是分布式系统的流量防卫兵。

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:Sentinel 同时提供实时的监控功能。我们可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。我们只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供Java/Go/C++ 等多语言的原生实现。
  • 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。我们可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

在这里插入图片描述
Sentinel 分为两个部分:

  • 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  • 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

如何引入Sentinel

如果要在我们的项目中引入 Sentinel,使用 group ID 为 com.alibaba.cloud 和 artifact ID 为 spring-cloud-starter-alibaba-sentinel 的 starter。

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

参考文档 请查看 官网

二、服务搭建

改造cloud-consumer-feign服务

有关 cloud-consumer-feign 服务的搭建过程,请参考之前的章节 基于Spring Boot 3.x 搭建教程

引入依赖

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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.ash</groupId>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <version>${revision}</version>
    </parent>

    <artifactId>cloud-consumer-feign</artifactId>
    <description>服务消费者-feign</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Nacos注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- loadbalancer负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

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

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- 将源码中的xml文件打包到jar中 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

添加服务降级类

新建 FeignServiceFallback.java 服务降级类,实现 FeignService.java 并实现方法
在这里插入图片描述
内容如下:

package org.ash.consumer.feign.service.fallback;

import org.ash.consumer.feign.service.FeignService;
import org.springframework.stereotype.Component;

@Component
public class FeignServiceFallback implements FeignService {
    @Override
    public String getProviderTest(String message) {
        return "对方服务不可用,开始服务降级处理";
    }
}

改造FeignService

改造 FeignService.java@FeignClient 注解,添加 fallback 属性
内容如下:

package org.ash.consumer.feign.service;

import org.ash.consumer.feign.service.fallback.FeignServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "cloud-provider", fallback = FeignServiceFallback.class)
public interface FeignService {

    @GetMapping("/provider/test/{message}")
    public String getProviderTest(@PathVariable("message") String message);
}

修改配置文件

默认 sentinel 对 feign 的支持是关闭的,我们需要在配置文件中打开。
内容如下:

server:
  port: 9003

spring:
  application:
    # 服务名称
    name: cloud-consumer-feign
  cloud:
    nacos:
      # nacos注册中心
      discovery:
        # 服务ip:port
        server-addr: 127.0.0.1:8848
    openfeign:
      client:
        config:
          default:
            #连接超时时间
            connectTimeout: 5000
            #读取超时时间
            readTimeout: 5000

# 开启feign集成sentinel服务降级
feign:
  sentinel:
    enabled: true

management:
  endpoints:
    web:
      exposure:
        include: '*'

三、运行测试

1.启动项目

1.1 启动服务提供者

在这里插入图片描述

1.2 启动服务消费者

在这里插入图片描述

2.调用测试接口

打开浏览器调用我们之前写的测试接口 http://localhost:9003/consumer/feign/test/open-feign,成功返回消息
在这里插入图片描述
这时我们停止服务提供者 cloud-provider 服务
在这里插入图片描述
然后再次通过浏览器调用测试接口 http://localhost:9003/consumer/feign/test/open-feign,看到返回服务降级的信息,表示服务降级成功
在这里插入图片描述
至此,OpenFeign集成Sentinel实现服务降级成功!!!

温馨提示:全套教程请查看 教程总览

相关推荐

  1. openfeign整合sentinel进行降级

    2024-04-06 05:34:10       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 05:34:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 05:34:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 05:34:10       82 阅读
  4. Python语言-面向对象

    2024-04-06 05:34:10       91 阅读

热门阅读

  1. sentinel与nacos集成

    2024-04-06 05:34:10       41 阅读
  2. 关于在Ubuntu上配置mysql踩的一些坑

    2024-04-06 05:34:10       39 阅读
  3. 【RV1126】Ubuntu22.04下sdk编译问题汇集

    2024-04-06 05:34:10       36 阅读
  4. c#中DocFx生成API帮助文档

    2024-04-06 05:34:10       37 阅读
  5. 点云滤波与匹配进阶

    2024-04-06 05:34:10       37 阅读
  6. 添加背景图片画图matlab

    2024-04-06 05:34:10       34 阅读
  7. 【QT教程】QT6单元测试

    2024-04-06 05:34:10       33 阅读
  8. SPI协议

    SPI协议

    2024-04-06 05:34:10      35 阅读
  9. ubuntu23.10全家桶(date2024.4.3)

    2024-04-06 05:34:10       45 阅读
  10. nginx: 集群环境配置搭建

    2024-04-06 05:34:10       30 阅读
  11. Rust个人学习之单元测试

    2024-04-06 05:34:10       36 阅读