zoukankan      html  css  js  c++  java
  • spring cloud:gateway-eureka-filter

    Spring Cloud Gateway 的 Filter 的生命周期不像 Zuul 的那么丰富,它只有两个:“pre” 和 “post”。

    • PRE: 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
    • POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的 HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。

    Spring Cloud Gateway 的 Filter 分为两种:GatewayFilter 与 GlobalFilter。GlobalFilter 会应用到所有的路由上,而 GatewayFilter 将应用到单个路由或者一个分组的路由上。

    Spring Cloud Gateway 内置了9种 GlobalFilter,比如 Netty Routing Filter、LoadBalancerClient Filter、Websocket Routing Filter 等,根据名字即可猜测出这些 Filter 的作者,具体大家可以参考官网内容:Global Filters

    利用 GatewayFilter 可以修改请求的 Http 的请求或者响应,或者根据请求或者响应做一些特殊的限制。 更多时候我们会利用 GatewayFilter 做一些具体的路由配置,下面我们做一些简单的介绍。

    gateway-eureka-filter

    1. File-->new spring starter project

    2.add dependency

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

    3.Edit application.yml

    server:
      port: 8080
    spring:
      application:
        name: gateway-server-eureka-filter
      cloud:
        gateway:
         discovery:
            locator:
             enabled: true
         routes:
    # forward by serviceID
         - id: add_request_parameter_route
           uri: lb://producer
    #       uri: http://localhost:8005/getHello
           filters:
           - AddRequestParameter=name,wang
           predicates:
             - Method=GET
             - Path=/getHello
             
     # forward by address
    #     - id: add_request_parameter_route
    #       uri: http://localhost:9000
    #       filters:
    #       - AddRequestParameter=foo, bar
    #       predicates:
    #         - Method=GET
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    logging:
      level:
        org.springframework.cloud.gateway: debug

    4.program

    package com.smile;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class GatewayServerEurekaFilterApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GatewayServerEurekaFilterApplication.class, args);
        }
    
    }

    5.Run

    http://localhost:8080/getHello

     访问结果交替出现

    hello wang

    hello wang,this is producer 1

  • 相关阅读:
    HDU 1114 Piggy-Bank
    HDU 2955 Robberies
    NTOJ 290 动物统计(加强版)
    POJ 3624 Charm Bracelet
    HDU 2602 Bone Collector
    POJ 1523 SPF(无向图割顶)
    HDU 5311 Hidden String
    HDU 1421 搬寝室
    HDU 1058 Humble Numbers
    POJ 3259 Wormholes(spfa判负环)
  • 原文地址:https://www.cnblogs.com/alittlesmile/p/10913237.html
Copyright © 2011-2022 走看看