Gateway网关

网关路由的配置

  1. 在配置文件yml中配置
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh                 # 路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001        # 匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**          # 断言,路径匹配的进行路由

        - id: payment_routh2
          uri: http://localhost:8001
          predicates:
            - Path=/payment/lb/**
  1. 代码注入RouteLocator的Bean
@Configuration
public class GateWayConfig {
    
    
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
    
    
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route_whut",r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }
}

访问http://localhost:9527/guonei将转发到http://news.baidu.com/guonei

  1. 动态路由
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
        - id: payment_routh                 # 路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001        # 匹配后提供服务的路由地址
          uri: lb://cloud-payment-service #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**          # 断言,路径匹配的进行路由
            
        - id: payment_routh2
#          uri: http://localhost:8001
          uri: lb://cloud-payment-service #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/lb/**

断言

https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#the-after-route-predicate-factory

  1. The After Route Predicate Factory
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2021-01-07T16:30:07.173279900+08:00[GMT+08:00]
  1. The Before Route Predicate Factory
  2. The Between Route Predicate Factory
  3. The Cookie Route Predicate Factory
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

测试:

curl http://localhost:9527/payment/lb --cookie "chocolate=ch.p"
  1. The Header Route Predicate Factory
  2. The Host Route Predicate Factory
  3. The Method Route Predicate Factory
  4. The Path Route Predicate Factory
  5. The Query Route Predicate Factory

过滤器

https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gatewayfilter-factories

  1. 自定义过滤器
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
    
        log.info("***come in MyLogGateWayFilter: "+new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname == null){
    
    
            log.info("***用户名为空,非法用户,/(ㄒoㄒ)/~~");
            exchange.getResponse().setStatusCode(org.springframework.http.HttpStatus.valueOf(HttpStatus.HTTP_NOT_ACCEPTABLE));
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);

    }

    @Override
    public int getOrder() {
    
    
        return 0;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40857365/article/details/113747845