转载请标明出处:
http://blog.csdn.net/forezp/article/details/83792388
本文出自方志朋的博客

点击获取SpringCloud 、Spring Boot视频

这篇文章讲述了如何简单地使用Spring Cloud Gateway,来源于Spring Cloud官方案例,地址https://spring.io/guides/gs/gateway 。

简介

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。本文首先用官方的案例带领大家来体验下Spring Cloud的一些简单的功能,在后续文章我会使用详细的案例和源码解析来详细讲解Spring Cloud Gateway.

创建工程

本案例的的源码下载于官方案例,也可以在我的Github上下载。工程使用的Spring Boot版本为2.0.5.RELEASE,Spring Cloud版本为Finchley.SR1。

新建一个工程,取名为sc-f-gateway-first-sight在工程的pom文件引用工程所需的依赖,包括spring boot和spring cloud,以及gateway的起步依赖spring-cloud-starter-gateway,代码如下:

   <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

** 注:详细的pom文件依赖,可以见源码。**

创建一个简单的路由

在spring cloud gateway中使用RouteLocator的Bean进行路由转发,将请求进行处理,最后转发到目标的下游服务。在本案例中,会将请求转发到http://httpbin.org:80这个地址上。代码如下:


@SpringBootApplication
@RestController
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Beanpublic RouteLocator myRoutes(RouteLocatorBuilder builder) {return builder.routes().route(p -> p.path("/get").filters(f -> f.addRequestHeader("Hello", "World")).uri("http://httpbin.org:80")).build();}}

在上面的myRoutes方法中,使用了一个RouteLocatorBuilder的bean去创建路由,除了创建路由RouteLocatorBuilder可以让你添加各种predicatesfilters,predicates断言的意思,顾名思义就是根据具体的请求的规则,由具体的route去处理,filters是各种过滤器,用来对请求做各种判断和修改。

上面创建的route可以让请求“/get”请求都转发到“http://httpbin.org/get”。在route配置上,我们添加了一个filter,该filter会将请求添加一个header,key为hello,value为world。

启动springboot项目,在浏览器上http://localhost:8080/get,浏览器显示如下:

{"args": {}, "headers": {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Cache-Control": "max-age=0", "Connection": "close", "Cookie": "_ga=GA1.1.412536205.1526967566; JSESSIONID.667921df=node01oc1cdl4mcjdx1mku2ef1l440q1.node0; screenResolution=1920x1200", "Forwarded": "proto=http;host=\"localhost:8080\";for=\"0:0:0:0:0:0:0:1:60036\"", "Hello": "World", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", "X-Forwarded-Host": "localhost:8080"}, "origin": "0:0:0:0:0:0:0:1, 210.22.21.66", "url": "http://localhost:8080/get"
}

可见当我们向gateway工程请求“/get”,gateway会将工程的请求转发到“http://httpbin.org/get”,并且在转发之前,加上一个filter,该filter会将请求添加一个header,key为hello,value为world。

注意HTTPBin展示了请求的header hello和值world。

使用Hystrix

在spring cloud gateway中可以使用Hystrix。Hystrix是 spring cloud中一个服务熔断降级的组件,在微服务系统有着十分重要的作用。
Hystrix是 spring cloud gateway中是以filter的形式使用的,代码如下:

   @Beanpublic RouteLocator myRoutes(RouteLocatorBuilder builder) {String httpUri = "http://httpbin.org:80";return builder.routes().route(p -> p.path("/get").filters(f -> f.addRequestHeader("Hello", "World")).uri(httpUri)).route(p -> p.host("*.hystrix.com").filters(f -> f.hystrix(config -> config.setName("mycmd").setFallbackUri("forward:/fallback"))).uri(httpUri)).build();}

在上面的代码中,我们使用了另外一个router,该router使用host去断言请求是否进入该路由,当请求的host有“*.hystrix.com”,都会进入该router,该router中有一个hystrix的filter,该filter可以配置名称、和指向性fallback的逻辑的地址,比如本案例中重定向到了“/fallback”。

现在写的一个“/fallback”的l逻辑:

@RequestMapping("/fallback")public Mono<String> fallback() {return Mono.just("fallback");}

Mono是一个Reactive stream,对外输出一个“fallback”字符串。

使用curl执行以下命令:

 curl --dump-header - --header 'Host: www.hystrix.com' http://localhost:8080/delay/3

返回的响应为:

fallback

可见,带hostwww.hystrix.com的请求执行了hystrix的fallback的逻辑。

总结

本文通过官方的一个简单的案例,来讲解了spring cloud gateway的简单用法,在spring cloud gateway中有2个重要的概念predicatesfilters,它们个将会在后续文章讲解。敬请期待。

源码下载

https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-gateway-first-sight

更多阅读

  • 史上最简单的SpringCloud教程 | 第十四篇: Spring Cloud Gateway初体验
  • 史上最简单的SpringCloud教程 | 第十五篇: Spring Cloud Gateway 之Predict篇
  • 史上最简单的SpringCloud教程 | 第十六篇: Spring Cloud Gateway 之filter篇
  • 史上最简单的SpringCloud教程 | 第十七篇: Spring Cloud Gateway 之限流篇
  • 史上最简单的SpringCloud教程 | 第十八篇: spring cloud gateway之服务注册与发现

史上最简单的 SpringCloud 教程汇总

SpringBoot教程汇总

Java面试题系列汇总


扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客)

Spring Cloud Gateway初体验相关推荐

  1. SpringCloud 2020版本教程2:使用spring cloud gateway作为服务网关

    点击关注公众号,Java干货及时送达 Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作用,网关常见 ...

  2. spring cloud gateway之服务注册与发现

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在之前的文章介绍了Spring Cloud Gateway的Predict(断言).Filter( ...

  3. spring cloud gateway之filter篇

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理, ...

  4. Spring Cloud Gateway之Predict篇

    Spring Cloud gateway工作流程 在之前的文章的Spring Cloud GateWay初体验中,大家已经对Spring Cloud Gateway的功能有一个初步的认识,网关作为一个 ...

  5. Spring Cloud Gateway 之Predict篇

    转载请标明出处: http://blog.csdn.net/forezp/article/details/84926662 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

  6. Spring Cloud Alibaba - 23 Gateway初体验

    文章目录 概述 网关的作用 官网 来个栗子 step1 搞依赖 step2 搞注解 (gateway没有注解) step3 搞配置 其他工程 & 验证 参数解读 spring.cloud.ga ...

  7. 微服务网关Zuul迁移到Spring Cloud Gateway

    https://juejin.im/post/5ba8daa56fb9a05cfe486ebf 背景 在之前的文章中,我们介绍过微服务网关Spring Cloud Netflix Zuul,前段时间有 ...

  8. Spring Cloud Gateway(路由)

    本篇文章主要介绍了什么是 Spring Cloud Gateway,并基于 Spring Cloud Gateway 的 Finchley.RC1 版本编写一个 Spring Cloud Gatewa ...

  9. Spring Cloud Gateway一次请求调用源码解析

    简介: 最近通过深入学习Spring Cloud Gateway发现这个框架的架构设计非常简单.有效,很多组件的设计都非常值得学习,本文就Spring Cloud Gateway做一个简单的介绍,以及 ...

  10. 网关Spring Cloud Gateway的配置和使用

    文章目录 1. 什么是Spring Cloud Gateway? 2. Gateway与zuul的区别 3. Gateway的配置和使用 ①:常用的路由断言工厂 ②:常用的过滤器工厂(GatewayF ...

最新文章

  1. Agile PLM Item Title Block Tab
  2. Python基础知识学习_Day5
  3. 利用CentOS快速构建自己的发行版(3)
  4. 第三届Apache Flink 极客挑战赛暨AAIG CUP攻略发布!
  5. 递归与分治——二分查找算法(折半查找算法)
  6. linux网络培训题目,linux 网络学习问题命令总结
  7. 年仅26岁!这位双一流大学的特任教授,攻克世界数学难题
  8. HDU3534 给你一个树让你找出其中最长路径以及个数数
  9. 5分钟 0元搭建个人独立博客网站(一)
  10. bzoj3159 决战
  11. selenide UI自动化进阶二 pageObject实现页面管理
  12. iostat查看服务器io
  13. acm steps chapter2总结
  14. NSRunLoop NSTimer
  15. 适配ofd签章SES_CertList
  16. ISO/IEC27000系列标准研究
  17. Ubuntu 21 .1安装wps office 2019并解决字体缺失问题教程
  18. opencv 手指位置检测
  19. 数据分析实战之超市零售分析(附python代码)
  20. oracle 物料属性批次过期,系列之五:ORACLE EBS 系统主数据管理(C)

热门文章

  1. IMDB 2003.07.12 最新排名
  2. linux制作虚拟机镜像,为OpenStack制作CoreOS虚拟机镜像(基于CoreOS官方提供镜像)
  3. 谷歌浏览器html调试iphone11,FireFox chrome 模拟手机浏览器 调试手机网页
  4. Windows中内存泄漏检测工具vld简介及使用
  5. Axure RP 8 最新注册码
  6. 思科OSPF配置实例(转)
  7. android videoview 拉伸,android - 在ExoPlayer中轻按全屏时,视频会拉伸和旋转 - 堆栈内存溢出...
  8. 3.7V升5V升压芯片
  9. 【分形理论、分形维数、多重分形、Matlab程序等整理】
  10. 台电x80h安装linux,Teclast 台电 X80h吃上win10,附实际使用体验