gateway整合sentinel流控降级

第一步:POM文件

<?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">
    <parent>
        <artifactId>springcloudalibaba</artifactId>
        <groupId>com.vn.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <dependencies>
        <!--gateway的依赖 springcloud开发-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- nacos服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>


        <!--sentinel整合gateway  以前的版本adapter-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

        <!--sentinel的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>
添加配置
server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    gateway: # gateway的配置
      routes: #路由规则
        - id: order_route  # 路由的唯一标识,路由到order
          uri: lb://order-service  #需要转发的地址   lb: 使用nacos中的本地负载均衡策略  order-service服务名
          predicates: #断言规则 用于路由规则的匹配
            - Path=/order-serv/**    # http://localhost:8088/order-serv/vn/order 路由转到  http://localhost:80/order-serv/vn/order
            - CheckAuth=vn     #自定义CheckAuth断言工厂
          filters:
            - StripPrefix=1 # 转发之前去掉1层路径  变成 -> http://localhost:80/vn/order
    nacos:
      server-addr: 192.168.43.197:8848
      discovery:
        username: nacos
        password: nacos
    # 配置sentinel
    sentinel:
      transport:
        dashboard: 127.0.0.1:8858
      # 自定义异常方式:
      scg:
        fallback:
          mode: response
          response-body: "{code:'',message:''}"
自定义异常方式:

第一种方式:通过yml

spring:cloud.sentinel.scg.fallback.mode = response  
spring.cloud.sentinel.scg.fallback.response‐body = '{"code":403,"mes":"限流了"}'

第二种方式:通过GatewayCallbackManager

 

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.HashMap;


@Configuration
public class GatewayConfig {

    @PostConstruct
    public void init(){

         BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
             @Override
             public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {

                 System.out.println(t);

                 HashMap<String,String> map=new HashMap<>();
                 map.put("code",HttpStatus.TOO_MANY_REQUESTS.toString());
                 map.put("message","限流了");

                 // 自定义异常处理
                 return ServerResponse.status(HttpStatus.OK)
                         .contentType(MediaType.APPLICATION_JSON)
                         .body(BodyInserters.fromValue(map));
             }
         };

        GatewayCallbackManager.setBlockHandler(blockRequestHandler);

    }
}

 sentinel的控制台管理图片:

定义API分组:

 

流控规则设置:

 

 降级规则设置:

 

posted @ 2022-03-15 21:16  VNone  阅读(439)  评论(0)    收藏  举报