Spring Cloud Gateway是类似Nginx的网关路由代理,有替代原来Spring cloud zuul之意:

Spring 5 推出了自己的Spring Cloud Gateway,支持Java 8、Reactor API,可在Spring Boot 2 使用,看到了响应式组件Reactor,可以理解这个网关方案目标之一是能够采用Reactive 来实现高效率的网关。

想要建立一个Spring Cloud Gateway 的话,在Spring Tool Suite 上可以选择「Gateway」这个Starter,为了能注册到服务发现服务器,也为了能开放gateway/routes 端点,以便观察路由信息,就顺便加入Eureka与Actuator 的Starter,比如在build.gradle 中可以包含:

implementation('org.springframework.boot:spring-boot-starter-actuator')  
implementation('org.springframework.cloud:spring-cloud-starter-gateway')
implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

Spring Cloud Gateway 可以在注册服务器上注册的服务ID,自动建立路由信息,为此,可以如下设定bootstrap.properties:

server.port=5555

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ management.endpoints.web.exposure.include: gateway

spring.cloud.gateway.discovery.locator.enabled启用了自动根据服务ID建立路由,路由的路径对应会使用大写ID,若想要使用小写ID,可将spring.cloud.gateway.discovery.locator.lowerCaseServiceId设为true;在设置中也开放了gateway端点。必要时,可以使用RouteLocator实现自定义路由的方式。

接下来启动相关服务,启动Spring Cloud Gateway,默认会跑在Netty上,如果测试请求http://localhost:5555/actuator/gateway/routes的话,就可以看到以下:

[
    {
        "route_id": "CompositeDiscoveryClient_ACCTSVI",
        "route_definition": {
            "id": "CompositeDiscoveryClient_ACCTSVI",
            "predicates": [
                {
                    "name": "Path",
                    "args": {
                        "pattern": "/acctsvi/**"
                    }
                }
            ],
            "filters": [
                {
                    "name": "RewritePath",
                    "args": {
                        "regexp": "/acctsvi/(?<remaining>.*)",
                        "replacement": "/${remaining}"
                    }
                }
            ],
            "uri": "lb://ACCTSVI",
            "order": 0
        },
        "order": 0
    },
    ...
]

每个路由设定会有个route_id作为识别,在路由定义的predicates中,可以看到设置了Path,这是Spring Cloud Gateway内建的断言器工厂Bean名称,pattern这个设置表示对于http://localhost:5555/acctsvi/xxxx的请求会转给uri设定的值:lb://ACCTSVI,也就是说路由转给了服务ID为ACCTSVI的服务。

filters中设置了RewritePath,这是个过滤器工厂Bean名称,依照regexp的规则,会捕捉请求中的/acctsvi/之后的部份,套用至服务的URI上,也就是http://localhost:5555/acctsvi/xxxx的请求,将会路由转发至http://acctsvi-uri/xxxx。

predicates与filters是Spring Cloud Gateway的重要特性,predicates断言哪些路径符合路由定义,filters设置具体哪些路径适用什么样的具体过滤器,除了设置之外,必要时,都可以代码自己定义。

Spring Cloud Gateway也内建了一些断言器工厂过滤器工厂,这些工厂类别,是可以通过属性档来定义的,必要时,也可以自定义工厂类别

就以上的设置来说,请求http://localhost:5555/acctsvi/accountByName?username=caterpillar就可以得到以下回应:

{
    "name": "caterpillar",
    "email": "caterpillar@openhome.cc",
    "password": "$2a$10$CEkPOmd.Uid2FpIOHA6Cme1G.mvhWfelv2hPu7cxZ/vq2drnXaVo.",
    "_links": {
        "self": {
            "href": "http://Justin-2017:8084/accountByNameEmail?username=caterpillar"
        }
    }
}

如果想要自定义路由,可以写个application.yml(若不想自动建立路由,可以将spring.cloud.gateway.discovery.locator.enabled与spring.cloud.gateway.discovery.locator.lowerCaseServiceId注解掉):

spring:
    application:
            name: gateway
    cloud:
        gateway:
            routes: 
                - predicates:
                    - Path=/acct/**
                  filters:
                      - StripPrefix=1
                  uri: lb://acctsvi
                - predicates:
                    - Path=/msg/**
                  filters:
                      - StripPrefix=1
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/email/**
                  filters:
                      - StripPrefix=1
                  uri: lb://email         

上述配置filters中的StripPrefix也是内建的过滤器工厂Bean名称,设定值为1表示将路由中的第一个层去除,其余保留用来转发请求,请求http://localhost:5555/actuator/gateway/routes的话,就可以看到以下:

[
    {
        "route_id": "545d278b-192b-4370-8156-161815957f91",
        "route_definition": {
            "id": "545d278b-192b-4370-8156-161815957f91",
            "predicates": [
                {
                    "name": "Path",
                    "args": {
                        "_genkey_0": "/acct/**"
                    }
                }
            ],
            "filters": [
                {
                    "name": "StripPrefix",
                    "args": {
                        "_genkey_0": "1"
                    }
                }
            ],
            "uri": "lb://acctsvi",
            "order": 0
        },
        "order": 0
    },
    ...
]

也就是对http://localhost:5555/acct/accountByName?username=caterpillar的请求,会转给http://acctsvi-url/accountByName?username=caterpillar。

如果想要设定api前置路径,就是修改一下StripPrefix=1为StripPrefix=2:

spring:
    application:
            name: gateway
    cloud:
        gateway:
            default-filters:
                - StripPrefix=2
            routes: 
                - predicates:
                    - Path=/api/acct/**
                  uri: lb://acctsvi
                - predicates:
                    - Path=/api/msg/**
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/api/email/**
                  uri: lb://email               

对于每个路由都要套用的过滤器,可以使用default-filters来设置,就以上设定来说,可以请求http://localhost:5555/api/acct/accountByName?username=caterpillar来取得使用者信息。

一开始自动根据服务ID建立路由时,可以看到RewritePath,它也是内建的过滤器工厂,可以运用规则表示式来进行路径重写,因此,也可以这么设置api前置:

spring:
    application:
            name: gateway
    cloud:
        gateway:
            default-filters:
                - RewritePath=/api/.*?/(?<remaining>.*), /$\{remaining}
            routes: 
                - predicates:
                    - Path=/api/acct/**
                  uri: lb://acctsvi
                - predicates:
                    - Path=/api/msg/**
                  uri: lb://msgsvi     
                - predicates:
                    - Path=/api/email/**
                  uri: lb://email            

就目前的设定来说,在客户端的部份,〈使用Zuul〉中的gossip就可以了,毕竟交互的接口没有改变,但是因为使用spring.application.gateway作为应用代理了,还是要记得改一下@FeignClient中的服务ID为gateway。

可以在首页qq群加群获取代码

Spring Cloud Gateway使用简介的更多相关文章

  1. 简单尝试Spring Cloud Gateway

    简单尝试Spring Cloud Gateway 简介 Spring Cloud Gateway是一个API网关,它是用于代替Zuul而出现的.Spring Cloud Gateway构建于Sprin ...

  2. Spring Cloud Gateway使用

    简介 Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权.路由转发.熔断.限流等. Sprin ...

  3. Spring Cloud Gateway 使用

    简介 Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权.路由转发.熔断.限流等. Sprin ...

  4. Spring Cloud gateway 网关服务二 断言、过滤器

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  5. Spring Cloud gateway 网关四 动态路由

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  6. Spring Cloud gateway 五 Sentinel整合

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  7. Spring Cloud gateway 六 Sentinel nacos存储动态刷新

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  8. Spring Cloud Gateway入坑记

    Spring Cloud Gateway入坑记 前提 最近在做老系统的重构,重构完成后新系统中需要引入一个网关服务,作为新系统和老系统接口的适配和代理.之前,很多网关应用使用的是Spring-Clou ...

  9. Spring Cloud Gateway(十一):全局过滤器GlobalFilter

    本文基于 spring cloud gateway 2.0.1 1.简介 GlobalGilter 全局过滤器接口与 GatewayFilter 网关过滤器接口具有相同的方法定义.全局过滤器是一系列特 ...

随机推荐

  1. robotframework框架 - seleniumLibrary 关键字解读-全攻略

    在robotframework当中,要实现web自动化,则需要使用SeleniumLibrary这个库. 目前版本中,有180+关键字.随着版本的更新,关键字的个数和名字也会有所变动. 在网上没有找到 ...

  2. Java通过JDK动态代理简单的实现一个AOP

    首先说一下,因为自己还没有去研读spring的AOP的源码,只是大致知道其功能,便想着自己先手动实现一个先看看,觉得这样以后研读源码的时候会收获更多! 实现:做一个在添加注解的方法执行之前,可以先执行 ...

  3. JVM Java字节码的角度分析switch的实现

    目录 Java字节码的角度分析switch的实现 引子 前置知识 一个妥协而又枯燥的方案 switch的实现 回顾历史 字节码分析 其他实现方式? Java字节码的角度分析switch的实现 作者 k ...

  4. Save your cats Aizu - 2224

    Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. T ...

  5. Get https://172.18.255.243:6443/api/v1/namespaces/kube-system/configmaps/kubelet-config-1.12: dial tcp 172.18.255.243:6443: i/o timeout

    问题描述 使用外网加入集群的时候报如下错误: Get https://172.18.255.243:6443/api/v1/namespaces/kube-system/configmaps/kube ...

  6. drf框架中所有视图及用法

    0909自我总结 drf框架中所有视图及用法 一.drf框架中的所有视图类 from django.views import View from rest_framework import views ...

  7. ThinkPHP5 远程命令执行漏洞分析

    本文首发自安全脉搏,转载请注明出处. 前言 ThinkPHP官方最近修复了一个严重的远程代码执行漏洞.这个主要漏洞原因是由于框架对控制器名没有进行足够的校验导致在没有开启强制路由的情况下可以构造恶意语 ...

  8. 机器学习:不平衡信息有序平均加权最近邻算法IFROWANN

    一 背景介绍 不平衡信息,特点是少数信息更珍贵,多数信息没有代表性.所以一般的分类算法会被多数信息影响,而忽略少数信息的重要性. 解决策略: 1.数据级别 (1)上采样:增加稀有类成本数 (2)下采样 ...

  9. Halcon一日一练:图像分割之基本概念

    1.什么是图像分割: 图像分割就是把图像中特定的目标提出来,进行处理. 2.为什么要做图像分割: 图像分割是由图像处理到图像分析的关键步骤,准确的来说,没有图像分割,图像处理将无法实现其后续的操作.进 ...

  10. shell变量(二)

    变量名的命名规范: 1.命名只能使用英文字母.数字和下划线,且不能以数字开头: 2.不能存在空格‘: 3.不能使用标点符号: 4.不能使用bash里的关键字(可使用help命令查看保留关键字) 变量的 ...