×
Spring Cloud教程什么是云计算?Netflix云计算技术介绍Amazon Web Services 简介Spring Cloud简介Spring Cloud配置服务Spring Cloud配置客户端

史上最简单SpringCloud教程

1 服务的注册与发现Eureka2 服务消费者(rest+ribbon)3 服务消费者(Feign)4 断路器(Hystrix)5 路由网关(zuul)6 分布式配置中心7 高可用的分布式配置中心8 消息总线(Spring Cloud Bus)9 服务链路追踪(Sleuth)10 高可用的服务注册中心11 Docker部署Spring Cloud12 断路器监控(Hystrix Dashboard)13 断路器聚合监控(Hystrix Turbine)Spring Cloud Gateway 初体验Spring Cloud Gateway PredictSpring Cloud Gateway之filter篇Spring Cloud Gateway之限流篇Spring Cloud Gateway之服务注册与发现

Spring Cloud 源码解析

深入理解Feign之源码解析深入理解Eureka之源码解析深入理解Ribbon之源码解析深入理解Hystrix之文档翻译深入理解Zuul之源码解析

Spring Cloud 进阶

Spring Cloud Sleuth进阶实战面试请不要再问我Spring Cloud底层原理微服务注册中心如何承载大型系统的千万级访问?Spring Cloud参数优化实战

Spring Cloud Gateway初体验


点击获取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@RestControllerpublic 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 教程汇总

SpringBoot教程汇总

Java面试题系列汇总



分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)