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:
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

spring cloud:gateway-eureka-filter的更多相关文章

  1. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

  2. Spring cloud gateway自定义filter以及负载均衡

    自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...

  3. 网关服务Spring Cloud Gateway(二)

    上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...

  4. 跟我学SpringCloud | 第十三篇:Spring Cloud Gateway服务化和过滤器

    SpringCloud系列教程 | 第十三篇:Spring Cloud Gateway服务化和过滤器 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich. ...

  5. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  6. SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断

    1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...

  7. Spring Cloud Gateway(二):Spring Cloud Gateway整合Eureka应用

    Spring Cloud Gateway 应用概述 下面的示例启动两个服务:gataway-server 和 user-service 都注册到注册中心 Eureka上,客户端请求后端服务[user- ...

  8. Spring Cloud Gateway整合Eureka

    Spring Cloud Gateway features: Built on Spring Framework 5, Project Reactor and Spring Boot 2.0 Able ...

  9. Spring Cloud Gateway实战之五:内置filter

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. 微服务网关 Spring Cloud Gateway

    1.  为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...

随机推荐

  1. 从入门到自闭之Python三大器--迭代器

    函数名的第一类对象(概述): 使用方式: 函数名可以当做值赋值给变量 def func(): print(1) print (func) #查看函数的内存地址 a = func print (a) # ...

  2. MySQL数据库入门多实例配置

    MySQL数据库入门——多实例配置 前面介绍了相关的基础命令操作,所有的操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的,因为必须要掌握 1.什么是多实例 多实例就是一台服务器上开启 ...

  3. ajax异步 —— javascript

    目录 ajax是什么 原生ajax jquery ajax ajax跨域 ajax是什么 作用:不必重新加载整个页面,更新部分页面内容. 大概使用过程:通过后台提供的数据接口,ajax获取数据,动态修 ...

  4. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  5. yum 报错2

    Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again 打开/etc/yu ...

  6. ui自动化之selenium操作(五)简单元素操作--续

    1. 多窗口切换 有时候需要在多窗口切换,webdriver提供了switch_to_window()方法支持切换窗口: from selenium import webdriver import o ...

  7. 性能测试:oprofile的学习使用

    一.oprofile简介 Oprofile是linux上的性能监测工具,有人说是性能测试的神器.通过CPU硬件提供的性能计数器对事件进行采样,从代码层面分析程序的性能消耗情况,找出程序性能的问题点. ...

  8. MySQL 表的增删查改

    一.插入数据 1. INSERT ... VALUES ... INSERT INTO <表名> [ <列名1> [ , … <列名n>] ] VALUES (值1 ...

  9. spring+mybatis事务配置(转载)

    原文地址:http://blog.csdn.net/wgh1015398431/article/details/52861048 申明式事务配置步骤 .xml文件头部需要添加spring的相关支持: ...

  10. Spring:jar包详解

    org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spring 2.5.6的时候需要a ...