1.什么是路由网关

截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做了负载均衡。目前为止这一切都看上运作的很好,我们通过地址访问地址http://localhost:9291/hello,实际是路由到了http://localhost:9191/hello和http://localhost:9192/hello两个服务器上。

紧接着,随着业务更进一步,我们又创建了UserService,又创建了ProductService,我们提供的服务器也越来越多,但是我们发现一个问题,即:即提供一种服务,前端程序员都需要通过IP+端口的形式去访问,很快URL地址就多的爆炸了,而且,甚至某些别有用心的同学因为知道了这些目标地址,开始采用非常规的手段去做些坏事。所以,我们必须做些手段来规避这些糟糕的事情。

路由网关出现了。

当我们输入URL,比如zuikc.com/hello或者zuikc.com/user的时候,路由网关会去分析这个地址,并且根据地址的pattern,

1:去决定到底是访问helloservice还是userservice;

2:到eureka注册中心拿到该服务的id;

3:通过ribbon去访问该服务id中的一台provider;

4:拿到response,返回给调用者;

并且,由于路由网关能做这些事情,还有额外的一些事情,比如权限验证(shiro,springsecurity等),就天然的适合放到路由网关也一并实现了。

关于路由网关,以前有zuul,但是zuul已经停止更新了,Spring Cloud Gateway被Spring Cloud官方推出来,作为第二代网关框架,取代Zuul网关。

总结一下,路由网关的作用就是:路由转发、权限校验、限流控制。

2.路由网关原理

来看Spring Cloud Gateway官方提供的架构图,

客户端向Spring Cloud Gateway发出请求。 Gateway Handler Mapping匹配路径并将其发送到Gateway web handler处理。 Gateway web handler处理请求,将其发送给过滤器链。

过滤器链主要分两大类:pre和post。“pre”过滤器一般进行权限、限流、日志输出等功能,以及请求头的更改、协议的转换;“post”过滤器是在收到响应后,可以对响应数据做统一修改,比如响应头、协议的转换等。

3.实现

现在,让我们用代码实现一下吧。

首先,创建子模块,在我们的例子中,创建完毕后,解决方案像如下这样,

现在,导入依赖如下:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>

<artifactId>springcloud.parent</artifactId>

<groupId>com.zuikc</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<name>gateway</name>

<artifactId>gateway</artifactId>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Greenwich.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-gateway</artifactId>

</dependency>

</dependencies>

</project>

先创建一个最简单的application.yml,

server:

port: 8880

然后,让我们创建application类,

package com.zuikc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName GatewayApplication

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@SpringBootApplication

@RestController

public class GatewayApplication {

public static void main(String[] args) {

SpringApplication.run(GatewayApplication.class, args);

}

@Bean

public RouteLocator myRoutes(RouteLocatorBuilder builder) {

return builder.routes()

.route("host_route", r -> r.path("/hello/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:9291"))

.route("host_route", r -> r.path("/user/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:9391"))

.build();

}

}

在这个类中,我们将“域名/hello”下的所有请求转发到了HelloService所在ribbon服务器中,将“域名/user”下所有的请求转到User所在的ribbon下。

然后,启动application。这个时候,让我们输入地址:http://localhost:8880/hello/hello,可以看到结果类似如下:

服务将在两个provider中切换。注意,上述url中,第一个hello,是指路由到helloservice中,第二个hello,是具体的服务。

接下来,让我们试一下,http://localhost:8880/user/something。由于我们目前并没有开发UserService,所以就出现errorpage了~~

4.使用配置实现

我们也可以使用配置来实现路由。

在上面的代码中,我们首先去掉application中的bean,

package com.zuikc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName GatewayApplication

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@SpringBootApplication

@RestController

public class GatewayApplication {

public static void main(String[] args) {

SpringApplication.run(GatewayApplication.class, args);

}

}

然后,修改application.yml,

server:

port: 8880

spring:

cloud:

gateway:

routes:

- id: host_route

uri: http://localhost:9291

predicates:

- Path=/hello/**

filters:

- StripPrefix=1

- id: host_route

uri: http://localhost:9391

predicates:

- Path=/user/**

filters:

- StripPrefix=1

然后,重启application,得到的效果是一样一样滴。

5.通过filters使用Hystrix

如果注意上文中的http://localhost:8880/user/something,我们发现原来在gateway中也是可以指定熔断器fallback的。

那就好办了,首先,让我们引入hystrix,

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

其次,创建一个fallback,

package com.zuikc;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName FallBack

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@RestController

@RequestMapping("/fallback")

public class FallBack {

@RequestMapping("")

public String fallback(){

return "error";

}

}

再次,修改我们的配置文件,

server:

port: 8880

spring:

cloud:

gateway:

routes:

- id: host_route

uri: http://localhost:9291

predicates:

- Path=/hello/**

filters:

- StripPrefix=1

- id: host_route

uri: http://localhost:9391

predicates:

- Path=/user/**

filters:

- StripPrefix=1

- name: Hystrix

args:

name: fallbackcmd

fallbackUri: forward:/fallback

重启application。

这个时候,再次访问http://localhost:8880/user/something,页面输出为error。

感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。

本文参考:https://spring.io/guides/gs/gateway/

一些官方的例子在:

http://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.0.RELEASE/single/spring-cloud-gateway.html

SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断的更多相关文章

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

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

  2. springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试

    上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能. 修改请求路径的过滤器 StripPrefi ...

  3. spring cloud gateway之filter篇

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

  4. 深入学习spring cloud gateway 限流熔断

    前言 Spring Cloud Gateway 目前,Spring Cloud Gateway是仅次于Spring Cloud Netflix的第二个最受欢迎的Spring Cloud项目(就GitH ...

  5. Spring Cloud Gateway - 路由法则

    1. After Route Predicate Factory 输入一个参数:时间,匹配该时间之后的请求,示例配置: spring: cloud: gateway: routes: - id: af ...

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

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

  7. SpringCloud无废话入门01:最简SpringCloud应用

    1.创建Parent Parent很简单,创建一个空的maven项目,pom如下: <?xml version="1.0" encoding="UTF-8" ...

  8. SpringCloud无废话入门04:Hystrix熔断器及监控

    1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...

  9. SpringCloud无废话入门02:Ribbon负载均衡

    1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...

随机推荐

  1. python基础——字符串、编码、格式化

    1.三种编码:ascii Unicode utf8 2.字符串和编码数字的两个函数:ord(字符转数字ord(‘A’)=65)和 chr(数字转字符chr(65)=A) 3.bytes存储编码,记住两 ...

  2. lvs-ldirectord

    Ldirectord;用来对后端服务器的检测状态后并进行操作  安装在director上 对后端的rs服务器的 健康检查包括几方面: 1通过ping 若可以ping到服务器表示主机活着 2 对端口的检 ...

  3. ghithub中PHPOffice/PHPWord的学习

    1.概念:PHPWord是用纯PHP提供了一组类写入和从不同的文档格式的文件阅读库.PHPWord的当前版本支持微软的Office Open XML(OOXML或处理OpenXML),用于Office ...

  4. L3-021 神坛 (30 分) 计算几何

    在古老的迈瑞城,巍然屹立着 n 块神石.长老们商议,选取 3 块神石围成一个神坛.因为神坛的能量强度与它的面积成反比,因此神坛的面积越小越好.特殊地,如果有两块神石坐标相同,或者三块神石共线,神坛的面 ...

  5. Python交互图表可视化Bokeh:2. 辅助参数

    图表辅助参数设置 辅助标注.注释.矢量箭头 参考官方文档:https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#col ...

  6. 051 Kafka的安装

    后来重新复习的时候,发现这篇文章不错:https://www.cnblogs.com/z-sm/p/5691760.html 一:前提 1.安装条件 Java   Scala zookeeper Ka ...

  7. 047 SparkSQL自定义UDF函数

    一:程序部分 1.需求 Double数据类型格式化,可以给定小数点位数 2.程序 package com.scala.it import org.apache.spark.{SparkConf, Sp ...

  8. redis虚拟内存

    对于redis 这样的内存数据库, 内存总是不够用的. 除了可以将数据分割到多个 redis 服务器以外. 另外的能够提高数据库容量的办法就是使用虚拟内存技术把那些不经常访问的数据交换到磁盘上 如果我 ...

  9. git合并

    git 里合并了两个分支以后,是不是两个分支的内容就完全一样了? 不是.看合并到哪个分支,这个分支有两个分支所有的内容.另外一个分支不变. 合并操作( merge )对当前所在分支产生影响. 合并分支 ...

  10. Date、Calendar、DateFormat类

    Date类与Calendar类之间的转换 package date; import java.util.Calendar; import java.util.Date; public class Da ...