当前位置: 首页 > news >正文

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

spring:application:name: eureka-clientcloud:compatibility-verifier:enabled: false
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:hostname: localhost
server:servlet:context-path: /eureka-clientport: 8080

TestController.java

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

EurekaClientApplication.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 

    <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

spring:application:name: gateway-mvc
server:port: 8088

RouteConfiguration.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 {@Beanpublic RouterFunction<ServerResponse> getRoute() {return route("simple_route").GET("/eureka-client/**", http("http://localhost:8080")).build();}
}

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

spring:cloud:gateway:mvc:routes:- id: simple_routeuri: http://localhost:8080predicates:- Path=/eureka-client/**

GatewayApplication.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和端口号

    @Beanpublic RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {return route("api_route").GET("/eureka-client/**", http()).filter(lb("EUREKA-CLIENT")).build();}

Spring Cloud Gateway Server MVC :: Spring Cloud Gateway

http://www.lryc.cn/news/326831.html

相关文章:

  • 建立动态MGRE隧道的配置方法
  • 【MySQL】9. 内置函数
  • 芯片工程系列(5)2.5D 3D封装
  • KubeSphere简单介绍及安装使用
  • Java零基础-集合:Java 8新增的集合操作
  • C++经典面试题目(七)
  • 让手机平板成为AI开发利器:AidLux
  • Python物理学有限差分微分求解器和动画波形传播
  • 游戏本续航@控制中心的省电模式效果如何
  • centOS 安装MySQL8.0
  • 力扣 1.两数之和
  • Occupancy field----其他应用
  • Spring_MVC
  • 【动手学深度学习】深入浅出深度学习之线性神经网络
  • 2024/3/26 C++作业
  • LinkedList讲解指南
  • IP如何异地共享文件?
  • HCIA-Datacom H12-811 题库补充(3/28)
  • 轻量级富文本编辑 Trumbowyg —— 基于 jQuery 插件配置
  • 那些王道书里的题目-----计算机网络篇
  • 【前端学习——js篇】 10.this指向
  • 项目搭建之统一返回值
  • 嵌入式和 Java 走哪条路?
  • C++ 控制语句(一)
  • mysql 用户管理-权限表
  • 【Postman如何进行接口测试简单详细操作实例】
  • docker搭建Project Calico环境
  • pyecharts操作一
  • 『Apisix进阶篇』动态负载均衡:APISIX的实战演练与策略应用
  • 【开发篇】十一、GC调优的分析工具