Spring Cloud Gateway Server MVC

之前你如果要用spring cloud gateway ,就必须是webflux 的,也就是必须是异步响应式编程。不能和spring mvc 一起使用。现在spring cloud 新出了一个可以不用webflux的gateway。

具体使用mvc的gateway步骤如下

普通的Eureka Client的项目

如果你只是想测试一下gateway mvc,可以建一个普通的spring boot项目,然后写一个/test rest api就可以了。

application.yml

java 复制代码
spring:
  application:
    name: eureka-client
  cloud:
    compatibility-verifier:
      enabled: false
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost
server:
  servlet:
    context-path: /eureka-client
  port: 8080

TestController.java

java 复制代码
@RestController
public class TestController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return "test";
    }
}

EurekaClientApplication.java

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

访问http://localhost:8080/eureka-client/test

Gateway MVC 的项目

在pom.xml加spring-cloud-starter-gateway-mvc

XML 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </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>

application.yml

XML 复制代码
spring:
  application:
    name: gateway-mvc
server:
  port: 8088

RouteConfiguration.java

java 复制代码
package com.example.gateway;
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;

@Configuration
public class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> getRoute() {
        return route("simple_route").GET("/eureka-client/**", http("http://localhost:8080")).build();
    }
}

或者用配置文件的方式.application.yml

java 复制代码
spring:
  cloud:
    gateway:
      mvc:
        routes:
          - id: simple_route
            uri: http://localhost:8080
            predicates:
              - Path=/eureka-client/**

GatewayApplication.java

java 复制代码
@SpringBootApplication
public class GatewayApplication {

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

}

访问http://localhost:8080/eureka-client/test

如果你不想hardcode 你的hostname和端口,也可以用Eureka 的方式来获取hostname和端口号

java 复制代码
    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
        return route("api_route")
                .GET("/eureka-client/**", http())
                .filter(lb("EUREKA-CLIENT"))
                .build();
    }

Spring Cloud Gateway Server MVC :: Spring Cloud Gateway

相关推荐
一只叫煤球的猫2 小时前
手撕@Transactional!别再问事务为什么失效了!Spring-tx源码全面解析!
后端·spring·面试
赤橙红的黄3 小时前
自定义线程池-实现任务0丢失的处理策略
数据库·spring
小时候的阳光4 小时前
SpringBoot3 spring.factories 自动配置功能不生效?
spring boot·spring·失效·factories·imports
张小洛5 小时前
Spring IOC容器核心阶段解密:★Bean实例化全流程深度剖析★
java·后端·spring·ioc容器·bean实例化
非ban必选7 小时前
spring-ai-alibaba官方 Playground 示例
java·人工智能·spring
要开心吖ZSH8 小时前
《Spring 中上下文传递的那些事儿》Part 2:Web 请求上下文 —— RequestContextHolder 与异步处理
java·spring
master-dragon9 小时前
spring-ai 工作流
人工智能·spring·ai
考虑考虑9 小时前
使用jpa中的group by返回一个数组对象
spring boot·后端·spring
圆滚滚肉肉9 小时前
后端MVC(控制器与动作方法的关系)
后端·c#·asp.net·mvc
ithadoop10 小时前
Spring生态:云原生与AI的革新突破
人工智能·spring·云原生