gateway网关限流

org.springframework.http.codec.ServerCodecConfigurer‘ that could not be found 报错;

出现该错误是因为Spring Cloud Gateway依赖了spring-boot-starter-web包

 

因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。

 

我们这里的话 ,gateway模块单独引入依赖,不引入common模块;
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowCredentials(true)
//                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
//                .maxAge(3600);
//    }
server:
  port: 88
  servlet:
    context-path: "/"

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_miaosha?serverTimezone=Asia/Shanghai
    username: root
    password: 123456
  redis:  # redis配置
    host: 192.168.30.113 # IP
    port: 6379  # 端口
    password:  # 密码
    connect-timeout: 10s  # 连接超时时间
    lettuce: # lettuce redis客户端配置
      pool:  # 连接池配置
        max-active: 8  # 连接池最大连接数(使用负值表示没有限制) 默认 8
        max-wait: 200s  # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
        max-idle: 8 # 连接池中的最大空闲连接 默认 8
        min-idle: 0 # 连接池中的最小空闲连接 默认 0
  jackson:
    time-zone: Asia/Shanghai
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowCredentials: true
            allowedHeaders: "*"
            allowedOrigins: "*"
            allowedMethods: "*"
      routes:
        - id: user_router
          uri: lb://miaosha-user
          predicates:
            - Path=/user/**

        - id: goods_router
          uri: lb://miaosha-goods
          predicates:
            - Path=/goods/**

        - id: miaoShaGoods_router
          uri: lb://miaosha-goods
          predicates:
            - Path=/miaoShaGoods/**

        - id: miaosha_router
          uri: lb://miaosha-miaosha
          predicates:
            - Path=/miaoSha/**, /verifyCode/**
          filters:
            - name: RequestRateLimiter  # 限流过滤器
              args:
                redis-rate-limiter.replenishRate: 1   # 令牌桶每秒填充速率
                redis-rate-limiter.burstCapacity: 2   # 令牌桶总容量
                redis-rate-limiter.requestedTokens: 1  # 一个请求需要消费的令牌数
                key-resolver: "#{@pathKeyResolver}"

        - id: order_router
          uri: lb://miaosha-order
          predicates:
            - Path=/order/**


  application:
    name: miaosha-gateway

export function getServerUrl(url,module){
  if(module=="user"){
    return "http://localhost:88/user/"+url;
  }else if(module=="miaoShaGoods"){
    return "http://localhost:88/miaoShaGoods/"+url;
  }else if(module=="verifyCode"){
    return "http://localhost:88/verifyCode/"+url;
  }else if(module=="miaoSha"){
    return "http://localhost:88/miaoSha/"+url;
  }else if(module=="order"){
    return "http://localhost:88/order/"+url;
  }
}

package com.java1234.miaosha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class MiaoshaGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MiaoshaGatewayApplication.class, args);
    }

}

package com.java1234.miaosha.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * 限流规则配置类
 * @author java1234_小锋
 * @site www.java1234.com
 * @company Java知识分享网
 * @create 2021-01-23 11:16
 */
@Configuration
public class KeyResolverConfiguration {

    @Bean
    public KeyResolver pathKeyResolver(){

        /*return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                return Mono.just(exchange.getRequest().getURI().getPath());
            }
        };*/


        return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
    }
}

你可能感兴趣的:(gateway)