Gateway系列---【快速构建gateway简单应用】

1.Gateway的介绍

Gateway是Spring官网开源的网关,采用webflux+netty+reactor开发的,所以不需要tomcat,不需要starter-web。用于替代zuul1.0,由于zuul忽然闭源,后来又开源了,zuul2.0和gateway性能差不多。因此,Gateway属于SpringCloud生态,所以,一般引入gateway的时候,一般都会引入springcloud依赖。

2.父pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.13</version>
  </parent>

  <groupId>com.demo</groupId>
  <artifactId>fast-cloud-2021.x-2.6.13</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>fast-cloud-2021.x-2.6.13</name>
  <url>脚手架模板项目</url>

  <modules>
    <module>fast-service</module>
    <module>fast-gateway</module>
    <module>fast-service-api</module>
    <module>fast-common</module>
  </modules>

  <properties>
    <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
    <spring-cloud.version>2021.0.5</spring-cloud.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <!--gateway是由springcloud开发的,所以要引入springcloud依赖-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>${spring-cloud-alibaba.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

3.gateway的pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>fast-cloud-2021.x-2.6.13</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>fast-gateway</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--gateway是由springcloud开发的,所以要引入springcloud依赖;网关是由webflux+netty+reactor开发的,所以不需要tomcat,不需要starter-web-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

</project>

4.启动类

package com.fast.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}

5.application.yml

server:
  port: 8080
spring:
  application:
    name: fast-gateway
  cloud:
    gateway:
      routes:
          #当前路径是http://localhost:8080/fast-auth/test时,期望转发到http://localhost:8081/test,如果fast-auth的context-path没有配置,
          #则需要配置StripPrefix=1,这个配置会去除“Path=/fast-auth/**”的第一层路径;如果fast-auth的context-path配置为fast-auth了,则不需要配置StripPrefix。
        - id: fast_auth
          uri: http://localhost:8081
          predicates:
            - Path=/fast-auth/**
#          filters:
#            - StripPrefix=1

posted on   少年攻城狮  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-11-24 工具类系列---【java8新特性-字符串拼接工具StringJoiner类】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示