Spring Cloud GateWay 集成 Sentinel 分布式流量控制框架

简介: 本文基于 spring.boot2.1.11.RELEASE, spring.cloud Greenwich.SR4, Spring Cloud Alibaba2.1.1.RELEASE, Sentinel 1.7.1

本文基于 Spring Boot2.1.11.RELEASE, Spring Cloud Greenwich.SR4, Spring Cloud Alibaba2.1.1.RELEASE, Sentinel 1.7.1

首先在网关模块 加入maven依赖, 等待maven 下载依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    <version>1.7.1</version>
</dependency>

image
作者这边没有指定版本号是因为做了统一的版本处理
然后编写 配置类

import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

import java.util.Collections;
import java.util.List;

/**
* Gateway Sentinel 配置
* @author Jeckxu
*/
@Configuration
public class GatewayConfiguration {

   private final List<ViewResolver> viewResolvers;
   private final ServerCodecConfigurer serverCodecConfigurer;

   public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
                               ServerCodecConfigurer serverCodecConfigurer) {
       this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
       this.serverCodecConfigurer = serverCodecConfigurer;
   }

   @Bean
   @Order(Ordered.HIGHEST_PRECEDENCE)
   public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
       // Register the block exception handler for Spring Cloud Gateway.
       return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
   }

   @Bean
   @Order(Ordered.HIGHEST_PRECEDENCE)
   public GlobalFilter sentinelGatewayFilter() {
       return new SentinelGatewayFilter();
   }
}

到此, GateWay 模块集成 Sentinel Code工作已经完成!

打开你的Sentinel 控制台, 你会发现相关的接口记录已经在Web中展示
image

目录
相关文章
|
15小时前
|
监控 Java 开发者
Spring Cloud中的服务熔断与降级
Spring Cloud中的服务熔断与降级
|
16小时前
|
负载均衡 Java API
Spring Cloud中的服务路由与过滤
Spring Cloud中的服务路由与过滤
|
16小时前
|
Java 开发工具 git
Spring Cloud中的分布式配置管理
Spring Cloud中的分布式配置管理
|
16小时前
|
负载均衡 Java 开发者
Spring Cloud实战:构建分布式系统解决方案
Spring Cloud实战:构建分布式系统解决方案
|
20小时前
|
负载均衡 Java API
Spring Cloud中的服务注册与发现策略
Spring Cloud中的服务注册与发现策略
|
20小时前
|
负载均衡 安全 Java
Spring Cloud中的服务路由与过滤详解
Spring Cloud中的服务路由与过滤详解
|
21小时前
|
存储 Java 开发工具
Spring Cloud中的分布式配置管理策略
Spring Cloud中的分布式配置管理策略
|
21小时前
|
监控 Java 数据处理
Spring Cloud Data Flow的实时数据处理详解
Spring Cloud Data Flow的实时数据处理详解
|
22小时前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【7月更文挑战第1天】Spring Cloud是Java微服务治理明星框架,整合Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心),提供完整服务治理解决方案。通过Eureka实现服务注册与发现,Ribbon进行客户端负载均衡,Hystrix确保服务容错,Config Server集中管理配置,Zuul作为API网关简化系统复杂性。理解和使用Spring Cloud是现代Java开发者的关键技能。
10 0
|
1天前
|
负载均衡 Java 开发者
Spring Cloud微服务架构中的配置管理与服务发现
Spring Cloud微服务架构中的配置管理与服务发现