SpringCloud 2.x学习笔记:12、Spring Cloud Gateway简单例子(Greenwich版本)

本文介绍了Spring Cloud Gateway,它是Spring Cloud官方第二代网关框架,构建于Spring生态系统之上,不能在传统Servlet容器工作。还给出简单例子演示,包括添加依赖、配置文件等,最后展示了运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Spring Cloud Gateway介绍

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。
Spring Cloud Gateway构建于Spring生态系统之上,包括Spring5,SpringBoot2等。它的目标是提供简单、有效的方式路由的API

Spring Cloud Gateway不能在传统的Servlet容器中工作。

请参考官方教程:
https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#_spring_cloud_gateway

2、简单例子演示

(1)父级pom

<?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>
    <groupId>com.cntaiping.tpa</groupId>
    <artifactId>gateway</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>eureka-server</module>
        <module>simple-geteway</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

(2)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>
    <groupId>com.cntaiping.tpa</groupId>
    <artifactId>simple-geteway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>simple-geteway</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--因为spring cloud gateway是基于webflux的,不要导入spring-boot-start-web-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

</project>

添加spring-cloud-starter-gateway依赖。请注意,这里千万不能有spring-boot-starter-web(父级pom.xml也没有spring-boot-starter-web),它们两个不能同时存在。
(3)application.properties

server.port= 7010

(4)Application类

package com.cntaiping.tpa.simplegeteway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SimpleGetewayApplication {

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

    /**
     * gateway中使用RouteLocator的Bean进行路由转发,将请求进行处理,最后转发到目标的下游服务。
     * 本例中将请求转发到http://httpbin.org:80这个地址上
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                //适用于lambda表达式的接口称之为函数型接口(只有一个抽象方法)
                //含参Lambda表达式:(x) -> x.f()
                //添加一个route让请求“/get”请求都转发到“http://httpbin.org/get”
                .route(p -> p.path("/get")
                             .filters(f -> f.addRequestHeader("flag", "HelloWorld"))
                             .uri("http://httpbin.org:80"))
                .build();
    }
}

说明:

httpbin.org 这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。

3、运行效果

http://localhost:7010/get

{
  "args": {}, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "zh-CN,zh;q=0.9", 
    "Flag": "HelloWorld", 
    "Forwarded": "proto=http;host=\"localhost:7010\";for=\"0:0:0:0:0:0:0:1:55191\"", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", 
    "X-Forwarded-Host": "localhost:7010"
  }, 
  "origin": "0:0:0:0:0:0:0:1, 180.169.108.92, ::1", 
  "url": "https://localhost:7010/get"
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值