ゲートウェイ

ゲートウェイルーティング構成

  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. RouteLocatorBeanに挿入されたコード
@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. アフタールート述語ファクトリ
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. ビフォアルート述語ファクトリ
  2. ルート間述語ファクトリ
  3. クッキールート述語ファクトリ
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. ヘッダールート述語ファクトリ
  2. ホストルート述語ファクトリ
  3. メソッドルート述語ファクトリ
  4. パスルート述語ファクトリ
  5. クエリルート述語ファクトリ

フィルタ

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