博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud:gateway-eureka-filter
阅读量:6910 次
发布时间:2019-06-27

本文共 2279 字,大约阅读时间需要 7 分钟。

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 的作者,具体大家可以参考官网内容:

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

gateway-eureka-filter

1. File-->new spring starter project

2.add dependency

org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

 

3.Edit application.yml

server:  port: 8080spring:  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=GETeureka:  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@EnableEurekaClientpublic class GatewayServerEurekaFilterApplication {    public static void main(String[] args) {        SpringApplication.run(GatewayServerEurekaFilterApplication.class, args);    }}

5.Run

 访问结果交替出现

hello wang

hello wang,this is producer 1

 

转载于:https://www.cnblogs.com/alittlesmile/p/10913237.html

你可能感兴趣的文章
【分享】博客美化(8)让你的博客“推荐按钮”动起来
查看>>
javascript prototype
查看>>
Linux 上的基础网络设备详解
查看>>
到底是否应该重复造轮子
查看>>
c 从语言中的内存管理
查看>>
Linux中ping命令
查看>>
oracle数据库导入导出命令!
查看>>
zoj 1610 Count the Colors 线段树区间更新/暴力
查看>>
android解决内存溢出的问题(没有从根本上解决)
查看>>
我心中想念那位偷吃玉米的老朋友
查看>>
“Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED”
查看>>
Kryo 为什么比 Hessian 快
查看>>
使用svn hooks 脚本post-commit时遇到的故障
查看>>
Android.mk 文件语法详解
查看>>
ThreadPool.QueueUserWorkItem的性能问题
查看>>
解读ASP.NET 5 & MVC6系列(11):Routing路由
查看>>
机器学习算法一览图
查看>>
识别出脸部以及给脸部打马赛克
查看>>
[转载]git 忽略某些文件
查看>>
jQuery 效果 - 隐藏和显示
查看>>