14.Gateway整合Sentinel

一、启动控制台

java -Dsentinel.dashboard.auth.username=lry -Dsentinel.dashboard.auth.password=lry -Dserver.port=8858 -jar sentinel-dashboard-1.8.5.jar


二、程序配置

1.引入依赖

<?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>Spring-Cloud-Test</artifactId>
        <groupId>cn.luoruiyuan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!--  热部署 -->
        <!-- 开发工具(feign.sentinel.enabled=true 时不要导入此依赖,不然项目报错)
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!--  nacos  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!--  gataway整合sentinel  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>



    </dependencies>

</project>

2.修改配置文件

server:
  port: 9000

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        password: nacos
        username: nacos
        server-addr: 127.0.0.1:8848
    #gateway的配置
    gateway:
      #路由配置 [路由 就是指定当请求满足什么条件的时候转到哪个微服务]
      routes:
          #gateway唯一标识
        - id: gateway-route
          #需要转发的地址,lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
          uri: lb://order-service
          #断言规则,就是路由转发要满足的条件
          predicates:
              #当请求路径满足Path指定的规则时,才进行路由转发
            - Path=/order-ser/**
            - Lry=a
          #过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
          filters:
              # 转发之前去掉第一层路由
            - StripPrefix=1
            - Lry=b
      #跨域配置
      globalcors:
        cors-configurations:
          #允许跨域访问的资源
          '[/**]':
            #跨域允许的来源
            allowedOrigins: "*"
            #允许的方法类型
            allowedMethods:
              - GET
              - POST
    #配置sentinel
    sentinel:
      transport:
        dashboard: 127.0.0.1:8858

3.自定义异常

package cn.luoruiyuan.config;

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;
import java.util.Map;

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void init(){
        BlockRequestHandler blockRequestHandler=new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                Map<String,String> map=new HashMap<String,String>();
                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);
    };
}

三、配置流控

1.请求链路中设置流控


2.API管理设置流控



四、访问效果



(1)