Spring Cloud微服务架构学习之gateway网关(五)

Spring Cloud Gateway定义:

1、SpringCloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

2、SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

3、Spring Cloud Gateway 的目标,不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

Spring Cloud Gateway特征:

SpringCloud官方,对SpringCloud Gateway 特征介绍如下:
(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
(2)集成 Hystrix 断路器
(3)集成 Spring Cloud DiscoveryClient
(4)Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters
(5)具备一些网关的高级功能:动态路由、限流、路径重写
从以上的特征来说,和Zuul的特征差别不大。SpringCloud Gateway和Zuul主要的区别,还是在底层的通信框架上。
简单说明一下上文中的三个术语:
(1)Filter(过滤器):
和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。
(2)Route(路由):
网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。
(3)Predicate(断言):
这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。

Spring Cloud Gateway的好处:

1.易于监控。可以在网关收集监控数据并将其推送到外部系统进行分析。
2.易于认证。可以在网关上进行认证,然后再将请求转发到后端的微服务,而无须在每个微服务中进行认证。
3.减少了客户端与各个微服务之间的交互次数。

Gateway 项目模块的搭建:

1.首先我们建一个springBoot项目取名为gateway_module,加上之前的我们搭的四个项目,现在的项目结构如下:

Spring Cloud微服务架构学习之gateway网关(五)_第1张图片

2.gateway_module项目中的pom.xml文件内容:


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.tone
    gateway_module
    0.0.1-SNAPSHOT
    gateway_module
    Demo project for Spring Boot

    
        1.8
        Hoxton.SR3
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.2.2.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-config-client
            2.2.2.RELEASE
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


以上特别要注意的是这里不需要引入spring-boot-start-web包  因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux即可,不然强制引入spring-boot-start-web包会导致与spring-boot-starter-webflux冲突问题,还有引入spring-cloud-starter-netflix-eureka-server包也会导致跟引入spring-boot-start-web冲突出现同样的问题,这里我们实际应该引用的是spring-cloud-starter-netflix-eureka-client,所以特别要注意下 ,以下引入spring-boot-start-web和spring-cloud-starter-netflix-eureka-server导致冲突的启动报错如下:
Spring Cloud微服务架构学习之gateway网关(五)_第2张图片

Spring Cloud微服务架构学习之gateway网关(五)_第3张图片

3.gateway_module项目中的bootstrap.yml内容如下(这里内容就没有啥好说的   跟其他的项目配置一样 ,都是获取配置中心的配置文件和把服务注册到注册中心):

server:
  port: 8888
spring:
  application:
    name: gatewayCenter
  # dev环境
  profiles:
    active: dev
  # 配置中心
  cloud:
    config:
      fail-fast: true
      name: ${spring.application.name}
      profile: ${spring.profiles.active}
      discovery:
        enabled: true
        service-id: configCenter
config:
  regcenter:
    ip: 127.0.0.1
    prot: 8762
    username: admin
    password: 123456
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    ## 注册服务中心的配置
    service-url:
      defaultZone: http://${config.regcenter.username}:${config.regcenter.password}@${config.regcenter.ip}:${config.regcenter.prot}/eureka/

4.gateway_module项目启动类内容:

package com.tone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayModuleApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayModuleApplication.class, args);
    }

}

5.我们把gateway的配置放在配置中心的gatewayCenter-dev.yml内容如下:

spring:
  cloud:
    gateway:
      locator:
        enabled: true
      routes:
        ######系统中心配置######
        - id: systemCenter     # <------ 这里是路由配置的ID
          uri: lb://systemCenter  # <------ 这里是路由最终目标Server的URI(Host)
          predicates:             # <------ 谓词集合配置,多个是用and逻辑连接
            - Path=/system/**      # <------- Key(name)=Expression,键是谓词规则工厂的ID,值一般是匹配规则的正则表示
          filters:
            - StripPrefix=1

        ######案例管理配置######
        - id: caseCenter
          uri: lb://caseCenter
          predicates:
            - Path=/case/**
          filters:
            - StripPrefix=1

以上filter中的StripPrefix,作用是去掉请求路径的最前面n个部分截取掉。StripPrefix=1就代表截取路径的个数为1,比如前端过来请求/test/system/1/view,匹配成功后,路由到后端的请求路径就会变成http://localhost:8888/system/1/view,不然不加的话就会报404错误

6.以上我们的gateway网关搭建就全部完成了 ,接下来我们依次启动regedit、config_modue、gateway_module、system_module、case_module等服务,先调用system_module、case_module项目中的http://127.0.0.1:8080/user/getUserList 、

http://127.0.0.1:8081/test/getUserList接口,调用如下:

Spring Cloud微服务架构学习之gateway网关(五)_第4张图片

 

Spring Cloud微服务架构学习之gateway网关(五)_第5张图片

 

7.最后我们通过gateway网关进行调用system_module,case_module中的接口,调用地址分别为:http://127.0.0.1:8888/system/user/getUserList、http://127.0.0.1:8888/case/test/getUserList,如下图所示  通过网关调用服务成功了

Spring Cloud微服务架构学习之gateway网关(五)_第6张图片

Spring Cloud微服务架构学习之gateway网关(五)_第7张图片

你可能感兴趣的:(Spring Cloud微服务架构学习之gateway网关(五))