本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一)

本篇文章讲解 Gateway 网关的多种路由规则、动态路由规则(配合服务发现的路由规则)。

  

路由规则

  

  点击链接观看:路由规则视频(获取更多请关注公众号「哈喽沃德先生」)

  

  Spring Cloud Gateway 创建 Route 对象时, 使用 RoutePredicateFactory 创建 Predicate 对象,Predicate 对象可以赋值给 Route。

  • Spring Cloud Gateway 包含许多内置的 Route Predicate Factories。
  • 所有这些断言都匹配 HTTP 请求的不同属性。
  • 多个 Route Predicate Factories 可以通过逻辑与(and)结合起来一起使用。

  

  路由断言工厂 RoutePredicateFactory 包含的主要实现类如图所示,包括 Datetime、 请求的远端地址、 路由权重、 请求头、 Host 地址、 请求方法、 请求路径和请求参数等类型的路由断言。

  

  

  接下来我们举例说明其中一部分如何使用,其余等大家工作中需要应用时再查询资料学习或者咨询我也可以。

  

Path

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
- Path=/product/** # 匹配对应 URL 的请求,将匹配到的请求追加在目标 URI 之后
  • 请求 http://localhost:9000/product/1 将会路由至 http://localhost:7070/product/1

  

Query

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
#- Query=token # 匹配请求参数中包含 token 的请求
- Query=token, abc. # 匹配请求参数中包含 token 并且其参数值满足正则表达式 abc. 的请求

  

Method

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
- Method=GET # 匹配任意 GET 请求

  

Datetime

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
# 匹配中国上海时间 2020-02-02 20:20:20 之后的请求
- After=2020-02-02T20:20:20.000+08:00[Asia/Shanghai]

  

RemoteAddr

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
- RemoteAddr=192.168.10.1/0 # 匹配远程地址请求是 RemoteAddr 的请求,0表示子网掩码

  

Header

  

spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: http://localhost:7070/ # 目标 URI,路由到微服务的地址
predicates: # 断言(判断条件)
# 匹配请求头包含 X-Request-Id 并且其值匹配正则表达式 \d+ 的请求
- Header=X-Request-Id, \d+

  

  

动态路由(服务发现的路由规则)

  

  动态路由其实就是面向服务的路由,Spring Cloud Gateway 支持与 Eureka 整合开发,根据 serviceId 自动从注册中心获取服务地址并转发请求,这样做的好处不仅可以通过单个端点来访问应用的所有服务,而且在添加或移除服务实例时不用修改 Gateway 的路由配置。

  

添加依赖

  

<!-- netflix eureka client 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

  

动态获取 URI

  

  点击链接观看:动态获取 URI视频(获取更多请关注公众号「哈喽沃德先生」)

  

配置文件

  

  配置注册中心和动态路由规则。

server:
port: 9000 # 端口 spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
# 路由规则
routes:
- id: product-service # 路由 ID,唯一
uri: lb://product-service # lb:// 根据服务名称从注册中心获取服务请求地址
predicates: # 断言(判断条件)
- Path=/product/** # 匹配对应 URL 的请求,将匹配到的请求追加在目标 URI 之后 # 配置 Eureka Server 注册中心
eureka:
instance:
prefer-ip-address: true # 是否使用 ip 地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # 设置服务注册中心地址
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

  

启动类

  

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; // 开启 EurekaClient 注解,目前版本如果配置了 Eureka 注册中心,默认会开启该注解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication { public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
} }

  

访问

  

  访问:http://localhost:9000/product/1 结果如下:

  

服务名称转发

  

  点击链接观看:服务名称转发视频(获取更多请关注公众号「哈喽沃德先生」)

  

  即使配置了动态获取 URI 的方式,项目中微服务一旦过多几十上百个时,配置中仍然要写很多配置,这时候就可以使用服务名称转发,与服务发现组件进行结合,通过 serviceId 转发到具体服务实例。默认匹配URL /微服务名称/** 路由到具体微服务。

  

配置文件

  

  配置注册中心和动态路由规则。

server:
port: 9000 # 端口 spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
discovery:
locator:
# 是否与服务发现组件进行结合,通过 serviceId 转发到具体服务实例。
enabled: true # 是否开启基于服务发现的路由规则
lower-case-service-id: true # 是否将服务名称转小写 # 配置 Eureka Server 注册中心
eureka:
instance:
prefer-ip-address: true # 是否使用 ip 地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # 设置服务注册中心地址
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

  

启动类

  

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; // 开启 EurekaClient 注解,目前版本如果配置了 Eureka 注册中心,默认会开启该注解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication { public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
} }

  

访问

  

  配置文件中没有配置任何订单服务的信息,访问:http://localhost:9000/order-service/order/1 结果如下:

下一篇我们讲解 Gateway 网关过滤器和全局过滤器以及自定义过滤器的使用,记得关注噢~

  本文采用 知识共享「署名-非商业性使用-禁止演绎 4.0 国际」许可协议

  大家可以通过 分类 查看更多关于 Spring Cloud 的文章。

  

  

Spring Cloud 系列之 Gateway 服务网关(二)的更多相关文章

  1. Spring Cloud 系列之 Gateway 服务网关(三)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) 本篇文章讲解 Ga ...

  2. Spring Cloud 系列之 Gateway 服务网关(四)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) Spring Cl ...

  3. Spring Cloud 系列之 Gateway 服务网关(一)

    什么是 Spring Cloud Gateway Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由 ...

  4. Spring Cloud(七)服务网关 Zuul Filter 使用

    上一篇文章中,讲了Zuul 转发,动态路由,负载均衡,等等一些Zuul 的特性,这个一篇文章,讲Zuul Filter 使用,关于网关的作用,这里就不再次赘述了,重点是zuul的Filter ,我们可 ...

  5. spring cloud深入学习(十一)-----服务网关zuul

    前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...

  6. Spring Cloud系列之Eureka服务治理

    写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...

  7. Spring Cloud(六)服务网关 zuul 快速入门

    服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...

  8. Spring Cloud Gateway 服务网关快速上手

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...

  9. Spring Cloud系列(二) 介绍

    Spring Cloud系列(一) 介绍 Spring Cloud是基于Spring Boot实现的微服务架构开发工具.它为微服务架构中涉及的配置管理.服务治理.断路器.智能路由.微代理.控制总线.全 ...

随机推荐

  1. wr720n v4 折腾笔记(四):安装inode客户端njitclient

    前记: 既然折腾到这里,那就不怕再折腾一下了,之前说过最终还是安装南浦月大神的固件,折腾了一圈,怎么不直接在官方界面上安装呢,这里给出直接安装的方法,就是修改固件头为wr720nv4. 0x01 修改 ...

  2. Spring优雅整合Redis缓存

    “小明,多系统的session共享,怎么处理?”“Redis缓存啊!” “小明,我想实现一个简单的消息队列?”“Redis缓存啊!” “小明,分布式锁这玩意有什么方案?”“Redis缓存啊!” “小明 ...

  3. HDFS数据加密空间--Encryption zone

    前言 之前写了许多关于数据迁移的文章,也衍生的介绍了很多HDFS中相关的工具和特性,比如DistCp,ViewFileSystem等等.但是今天本文所要讲的主题转移到了另外一个领域数据安全.数据安全一 ...

  4. 北邮OJ 89. 统计时间间隔 java版

    89. 统计时间间隔 时间限制 1000 ms 内存限制 65536 KB 题目描述 给出两个时间(24小时制),求第一个时间至少要经过多久才能到达第二个时间.给出的时间一定满足的形式,其中x和y分别 ...

  5. SpringMVC常见面试题总结(超详细回答)

    SpringMVC常见面试题总结(超详细回答) 1.什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的 ...

  6. Python python 函数参数:参数组合

    '''在Python中定义函数,可以用必选参数.默认参数.可变参数和关键字参数, 这4种参数都可以一起使用,或者只用其中某些 参数定义的顺序必须是:必选参数.默认参数.可变参数和关键字参数 ''' d ...

  7. mac 根目录下新建文件夹并赋予权限

    在根目录中,你会发现你无法创建文件夹,即使使用命令也无法创建目录: 1.修改auto_master 编译 /etc/auto_master 文件,注释掉或者移除以 /home 开头的那一行,保存. 终 ...

  8. python之openpyxl模块(最全总结 足够初次使用)

    openpyxl模块 Python_Openpyxl 1. 安装 pip install openpyxl 2. 打开文件 ① 创建 from openpyxl import Workbook # 实 ...

  9. 读者来信 | 刚搭完HBase集群,Phoenix一启动,HBase就全崩了,是什么原因?(已解决)

    前言:之前有朋友加好友与我探讨一些问题,我觉得这些问题倒挺有价值的:于是就想在本公众号开设一个问答专栏,方便技术交流与分享,专栏名就定为:<读者来信>.如遇到本人能力有限难以解决的问题,我 ...

  10. AngularJS插件使用---angular-cookies.js

    首先在项目中引入angular-cookies.js angular模块中添加依赖:‘ngCookies’ 在控制器中依赖注入$cookies,使用$cookies操作cookie $cookiesP ...