sentinel网关流控

springboot整合sentinel:

springboot整合sentinel_呆萌很的博客-CSDN博客

网关服务引入依赖

        
            com.alibaba.cloud
            spring-cloud-alibaba-sentinel-gateway
            2.1.0.RELEASE
        

sentinel控制台查看,功能有区别其他服务

sentinel网关流控_第1张图片

新增网关流控规则

sentinel网关流控_第2张图片 超出请求限制显示:

sentinel网关流控_第3张图片

带有指定请求头的流控

sentinel网关流控_第4张图片

带有hello参数头超出流控显示

sentinel网关流控_第5张图片 

 API组流控

新增API组

sentinel网关流控_第6张图片

 新增流控规则,选择API组,对整个API分组都将起到作用

sentinel网关流控_第7张图片

修改限流返回数据

package com.hdb.pingmoweb.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import com.hdb.pingmoweb.common.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class SentinelGatewayConfig {

    public SentinelGatewayConfig() {
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                R error = R.error(400, "太多请求");
                String errJson = JSON.toJSONString(error);
                Mono body = ServerResponse.ok().body(Mono.just(errJson), String.class);
                return body;
            }
        });
    }
}

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