spring-cloud-starter-gateway-mvc的网关实现

一 概括

最近,我也一直在使用SpringCloudGateway开发我们自己的网关产品。根据我对这份正式文件的理解,内容如下:

SpringCloudGateway的默认底层依赖项是SpringWebflux。我们知道Spring Webflux是异步和响应式编程,并且编程范式是使用流范式编写的;

那么SpringCloudGateway支持同步网关吗?官方支持,官方网站提供了相应的解决方案,即将默认的底层Webflux切换到SpringMVC以支持同步;

相当于SpringCloudGateway为您提供了两种网关路由的底层实现,一种是对应于SpringWebflux的异步实现,另一种是与SpringMVC对应的同步实现。

二 spring-cloud-starter-gateway-mvc例子

1 pom.xml:

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.winning.gate</groupId>
    <artifactId>mvc-gateway</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </dependency>
    </dependencies>

</project>

2 application.yml:

yml 复制代码
server:
  port: 9098

3 com.demo.GatewaySampleApplication:

java 复制代码
package com.demo;

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

/**
 * @author xch
 * 2023/12/15 16:56
 */
@SpringBootApplication
public class GatewaySampleApplication {

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

}

4 com.demo.SimpleGateway

java 复制代码
package com.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

/**
 * @author xch
 * 2023/12/15 16:52
 */

@Configuration
class SimpleGateway {
    @Bean
    public RouterFunction<ServerResponse> getRoute() {
        return route().GET("/demo", http("http://localhost:8080/")).build();
    }
}

Above is all the code

5 Request Results

java 复制代码
GET http://localhost:9098/demo?ceshi=ceshi

HTTP/1.1 200 
date: Fri, 15 Dec 2023 09:23:37 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Keep-Alive: timeout=60
Connection: keep-alive

{
  "code": 0,
  "data": "ceshi"
}
Response file saved.
> 2023-12-15T172337.200.json

Response code: 200; Time: 19ms (19 ms); Content length: 25 bytes (25 B)

The destination service address of the route is in SimpleGateway, you can change it to your own service

相关推荐
艾菜籽17 小时前
Spring MVC练习:留言板
java·spring·mvc
William_cl17 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
William_cl1 天前
ASP.NET MVC 前置基础:宿主环境 & HttpRuntime 管道,从部署到流程拆透(附避坑指南)
后端·asp.net·mvc
唐僧洗头爱飘柔95272 天前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
xrkhy2 天前
微服务之Gateway网关(1)
微服务·架构·gateway
William_cl3 天前
【C# OOP 入门到精通】从基础概念到 MVC 实战(含 SOLID 原则与完整代码)
开发语言·c#·mvc
William_cl5 天前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
程序员小凯6 天前
Spring MVC 分布式事务与数据一致性教程
分布式·spring·mvc
艾菜籽6 天前
SpringMVC练习:加法计算器与登录
java·spring boot·spring·mvc
程序员小凯6 天前
Spring MVC 多租户架构与数据隔离教程
spring·架构·mvc