zoukankan      html  css  js  c++  java
  • Spring Cloud Alibaba 06:Gateway服务网关

    Spring Cloud Gateway 是基于 Netty,跟 Servlet 不兼容,所以你的工程中不能出现 Servlet 的组件 。

    1、pom.xml

    注意,一定不能出现 spring web 的依赖,因为 Gateway 与 Servlet 不兼容。

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

    2、application.yml

    server:
      port: 8010
    spring:
      application:
        name: gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
          routes: 
            - id: provider_route   
              uri: http://localhost:8081 
              predicates: 
                - Path=/provider/** 
              filters:
                - StripPrefix=1
    

    上面这种做法其实没有用到 nacos ,现在我们让 gateway 直接去 nacos 中发现服务,配置更加简单了。

    1、pom.xml 引入 nacos

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    

    2、application.yml

    server:
      port: 8010
    spring:
      application:
        name: gateway
      cloud:
          gateway:
            discovery:
              locator:
                enabled: true
    

    Gateway 限流

    基于路由限流

    1、pom.xml

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

    2、配置类

    package com.godfrey.configuration;
    
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
    import org.springframework.beans.factory.ObjectProvider;
    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.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.codec.ServerCodecConfigurer;
    import org.springframework.web.reactive.function.BodyInserters;
    import org.springframework.web.reactive.function.server.ServerResponse;
    import org.springframework.web.reactive.result.view.ViewResolver;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    
    import javax.annotation.PostConstruct;
    import java.util.*;
    
    /**
     * @author godfrey
     * @since 2020-12-27
     */
    @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() {
            return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
        }
    
        //配置初始化的限流参数
        @PostConstruct
        public void initGatewayRules(){
            Set<GatewayFlowRule> rules = new HashSet<>();
            rules.add(
                    new GatewayFlowRule("provider_route")
                            .setCount(1)
                            .setIntervalSec(1)
            );
            GatewayRuleManager.loadRules(rules);
        }
    
        //初始化限流过滤器
        @Bean
        @Order(Ordered.HIGHEST_PRECEDENCE)
        public GlobalFilter sentinelGatewayFilter() {
            return new SentinelGatewayFilter();
        }
    
        //自定义限流异常页面
        @PostConstruct
        public void initBlockHandlers(){
            BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
                @Override
                public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                    Map map = new HashMap();
                    map.put("code",0);
                    map.put("msg","被限流了");
                    return ServerResponse.status(HttpStatus.OK)
                            .contentType(MediaType.APPLICATION_JSON)
                            .body(BodyInserters.fromObject(map));
                }
            };
            GatewayCallbackManager.setBlockHandler(blockRequestHandler);
        }
    }
    

    3、application.yml

    server:
      port: 8010
    spring:
      application:
        name: gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
          routes:
            - id: provider_route
              uri: http://localhost:8081
              predicates:
                - Path=/provider/**
              filters:
                - StripPrefix=1
    

    基于 API 分组限流

    1、修改配置类,添加基于 API 分组限流的方法,修改初始化的限流参数

    package com.godfrey.configuration;
    
    import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
    import org.springframework.beans.factory.ObjectProvider;
    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.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.codec.ServerCodecConfigurer;
    import org.springframework.web.reactive.function.BodyInserters;
    import org.springframework.web.reactive.function.server.ServerResponse;
    import org.springframework.web.reactive.result.view.ViewResolver;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    
    import javax.annotation.PostConstruct;
    import java.util.*;
    
    /**
     * @author godfrey
     * @since 2020-12-27
     */
    
    @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() {
            return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
        }
    
        //配置初始化的限流参数
        @PostConstruct
        public void initGatewayRules(){
            Set<GatewayFlowRule> rules = new HashSet<>();
            rules.add(new GatewayFlowRule("provider_api1").setCount(1).setIntervalSec(1));
            rules.add(new GatewayFlowRule("provider_api2").setCount(1).setIntervalSec(1));
            GatewayRuleManager.loadRules(rules);
        }
    
        //初始化限流过滤器
        @Bean
        @Order(Ordered.HIGHEST_PRECEDENCE)
        public GlobalFilter sentinelGatewayFilter() {
            return new SentinelGatewayFilter();
        }
    
        //自定义限流异常页面
        @PostConstruct
        public void initBlockHandlers(){
            BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
                @Override
                public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                    Map map = new HashMap();
                    map.put("code",0);
                    map.put("msg","被限流了");
                    return ServerResponse.status(HttpStatus.OK)
                            .contentType(MediaType.APPLICATION_JSON)
                            .body(BodyInserters.fromObject(map));
                }
            };
            GatewayCallbackManager.setBlockHandler(blockRequestHandler);
        }
    
        //自定义API分组
        @PostConstruct
        private void initCustomizedApis(){
            Set<ApiDefinition> definitions = new HashSet<>();
            ApiDefinition api1 = new ApiDefinition("provider_api1")
                    .setPredicateItems(new HashSet<ApiPredicateItem>(){{
                        add(new ApiPathPredicateItem().setPattern("/provider/api1/**")
                                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
                    }});
            ApiDefinition api2 = new ApiDefinition("provider_api2")
                    .setPredicateItems(new HashSet<ApiPredicateItem>(){{
                        add(new ApiPathPredicateItem().setPattern("/provider/api2/demo1"));
                    }});
            definitions.add(api1);
            definitions.add(api2);
            GatewayApiDefinitionManager.loadApiDefinitions(definitions);
        }
    }
    

    2、Controller 添加方法

    @GetMapping("/api1/demo1")
    public String demo1() {
        return "demo";
    }
    
    @GetMapping("/api1/demo2")
    public String demo2() {
        return "demo";
    }
    
    @GetMapping("/api2/demo1")
    public String demo3() {
        return "demo";
    }
    
    @GetMapping("/api2/demo2")
    public String demo4() {
        return "demo";
    }
    

    也可以基于 Nacos 服务发现组件进行限流

    server:
      port: 8010
    spring:
      application:
        name: gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
    

    API 分组代码修改,改为 discovery 中的服务名。

    ApiDefinition api2 = new ApiDefinition("provider_api2")
            .setPredicateItems(new HashSet<ApiPredicateItem>(){{
                add(new ApiPathPredicateItem().setPattern("/p1/api2/demo1"));
            }});
    
  • 相关阅读:
    This is very likely to create a memory leak. Stack trace of thread错误分析
    spring boot 2.0 neo4j 使用
    造个简单的轮子倒是不难,但可用性健壮性高到qt这样全世界都在用,就几乎不可能了
    0xC0000005;Access Violation(栈区空间很宝贵, linux上栈区空间默认为8M,vc6下默认栈空间大小为1M)
    Object::connect: Cannot queue arguments of type 'QMap<QString,QString>'(要使用qRegisterMetaType<StringMap>进行注册)
    QFileSystemModel只显示名称,不显示size,type,modified
    非Qt工程使用Qt的信号槽机制
    web性能权威指南(High Performance Browser Networking)
    缓存穿透 缓存并发 缓存失效
    互联网架构-基础中间件
  • 原文地址:https://www.cnblogs.com/MessiXiaoMo3334/p/14199158.html
Copyright © 2011-2022 走看看