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

相关推荐
珹洺1 天前
Java-Spring入门指南(二十七)Android Studio 第一个项目搭建与手机页面模拟器运行
java·spring·android studio
laopeng3011 天前
基于Spring AI Deep Researcher Agent
java·人工智能·spring
m0_736927041 天前
Spring Boot自动配置与“约定大于配置“机制详解
java·开发语言·后端·spring
Knight_AL1 天前
Spring AOP 中@annotation的两种写法详解
java·spring
顾漂亮1 天前
Spring AOP 实战案例+避坑指南
java·后端·spring
野生技术架构师1 天前
JAVA 架构师面试题含答案:JVM+spring+ 分布式 + 并发编程
java·jvm·spring
唐僧洗头爱飘柔95271 天前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
ChinaRainbowSea2 天前
11. Spring AI + ELT
java·人工智能·后端·spring·ai编程
xrkhy2 天前
微服务之Gateway网关(1)
微服务·架构·gateway
AntBlack2 天前
虽迟但到 :盘一盘 SpringAI 现在发展得怎么样了?
后端·spring·openai