Gateway

Gateway routing configuration

  1. Configure in the configuration file 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. Code injected into 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();
    }
}

Visit http://localhost:9527/guonei and it will be forwarded to http://news.baidu.com/guonei

  1. Dynamic routing
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/**

Affirmation

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

test:

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

filter

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

  1. Custom filter
@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;
    }
}

Guess you like

Origin blog.csdn.net/qq_40857365/article/details/113747845