网站建设马鞍山,公众号登录平台入口,个人网站域名名字,自己怎么接单做网站之前你如果要用spring cloud gateway #xff0c;就必须是webflux 的#xff0c;也就是必须是异步响应式编程。不能和spring mvc 一起使用。现在spring cloud 新出了一个可以不用webflux的gateway。 具体使用mvc的gateway步骤如下
普通的Eureka Client的项目
如果你只是想测…之前你如果要用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: 8080TestController.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 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.2.4/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesjava.version17/java.versionspring-cloud.version2023.0.0/spring-cloud.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway-mvc/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementapplication.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 RouterFunctionServerResponse 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 RouterFunctionServerResponse gatewayRouterFunctionsAddReqHeader() {return route(api_route).GET(/eureka-client/**, http()).filter(lb(EUREKA-CLIENT)).build();} Spring Cloud Gateway Server MVC :: Spring Cloud Gateway