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

相关推荐
web135085886356 小时前
【Spring Boot】Spring AOP动态代理,以及静态代理
spring boot·后端·spring
zzyh1234568 小时前
springcloud的组件及作用
后端·spring·spring cloud
山海不说话8 小时前
从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
spring boot·后端·spring·spring cloud·微服务·logback
Beekeeper&&P...9 小时前
Spring Security,servlet filter,和白名单之间的关系
hive·spring·servlet
计算机学姐12 小时前
基于SpringBoot的鲜花商城
java·vue.js·spring boot·后端·mysql·spring·java-ee
铭毅天下14 小时前
极限网关 INFINI Gateway 从 0 到 1:简单易懂的入门教程
gateway
zl97989915 小时前
Spring-AOP
java·后端·spring
铭毅天下16 小时前
极限网关核心架构解析:从 Nginx 到 INFINI Gateway 的演进
运维·nginx·架构·gateway
LiuYuHani16 小时前
spring如何解决循环依赖的问题
java·spring boot·spring
憶巷1 天前
深入剖析Spring MVC
java·spring·mvc