「SpringCloud」集成Gateway实现微服务路由转发

ponymar
发布于 2022-4-26 16:10
浏览
0收藏

作者 AI全栈程序猿
来源 | 今日头条

在微服务架构里,服务的粒度被进一步细分,各个业务服务可以被独立的设计、开发、测试、部署和管理。这时,各个独立部署单元可以用不同的开发测试团队维护,可以使用不同的编程语言和技术平台进行设计,这就要求必须使用一种语言和平 台无关的服务协议作为各个单元间的通讯方式。

API 网关的定义

网关的角色是作为一个 API 架构,用来保护、增强和控制对于 API 服务的访问。

API 网关是一个处于应用程序或服务(提供 REST API 接口服务)之前的系统,用来管理授权、访问控制和流量限制等,这样 REST API 接口服务就被 API 网关保护起来,对所有的调用者透明。因此,隐藏在 API 网关后面的业务系统就可以专注于创建和管理服务,而不用去处理这些策略性的基础设施。

Gateway是什么

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

为什么用Gateway

Spring Cloud Gateway 可以看做是一个 Zuul 1.x 的升级版和代替品,比 Zuul 2 更早地使用 Netty 实现异步 IO,从而实现了一个简单、比 Zuul 1.x 更高效的、与 Spring Cloud 紧密配合的 API 网关。

Spring Cloud Gateway 里明确的区分了 Router 和 Filter,并且一个很大的特点是内置了非常多的开箱即用功能,并且都可以通过 SpringBoot 配置或者手工编码链式调用来使用。

比如内置了 10 中 Router,使得我们可以直接配置一下就可以随心所欲地根据 Header、或者 Path、或者 Host、或者 Query 来做路由。

比如区分了一般的 Filter 和全局 Filter,内置了 20 中 Filter 和 9 种全局 Filter,也都可以直接用。当然自定义 Filter 也非常方便。

1、在GitEgg-Cloud工程的子工程gitegg-gateway中引入Nacos和Spring Cloud Gateway的依赖

 <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

2、新建

package com.gitegg.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class GitEggGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GitEggGatewayApplication.class,args);
    }
}

3、新建bootstrap.yml配置文件

server:
  port: 80
spring:
  application:
    name: gitegg-service-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        group: DEFAULT_GROUP
        enabled: true
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: gitegg-service-system
          uri: lb://gitegg-service-system
          predicates:
            - Path=/gitegg-system/**
          filters:
            - StripPrefix=1

4、在gitegg-cloud-system的SystemController.java添加测试方法:

  @ApiOperation(value = "Gateway路由转发测试")
    @GetMapping(value = "gateway/forward")
    public Result gatewayForward() {
        return Result.success("gitegg-service-system测试数据");
    }

5、启动gitegg-cloud-system和gitegg-gateway服务,在浏览器中访问gitegg-gateway的服务端口+ /gitegg-system/ + /system/gateway/forward,可以看到页面返回的数据是访问的gitegg-cloud-system方法「SpringCloud」集成Gateway实现微服务路由转发-鸿蒙开发者社区

源码在

https://gitee.com/wmz1930/GitEgg 的chapter-18分支。

分类
收藏
回复
举报
回复
    相关推荐