Spring Cloud Gateway 作为网关以及 Web 应用程序

Nar*_*waj 8 java spring-boot spring-webflux spring-cloud-gateway

我有一个 Spring Cloud Gateway 应用程序,我想要的是,如果有两个路由,那么在一个路由上,它应该重定向到某个外部应用程序,但对于其他路由,它应该将请求转发到具有特定 url 的同一应用程序。

-id: my_local_route
 predicates:
   - Path="/services/local"
 uri: "/mylocal/services/local" //can we do something like that

Run Code Online (Sandbox Code Playgroud)

请注意,我想在与 Spring Cloud Gateway 相同的应用程序中创建我的休息服务。我知道这不是正确的方法,但据我所知,我想知道这是否可能。

Dha*_*jay 2

如果您的 spring-cloud-gateway 项目中有一些 REST API,则无需显式为其添加路由。所以假设你在网关项目中有以下rest api

@RestController
@RequestMapping("test")
class Controller{
    @GetMapping("hello")
    public String hello(){
        return "hello";
    }
}
Run Code Online (Sandbox Code Playgroud)

对于 external-url,您想发送一些流量到https://httpbin.org. 所以在网关 application.yml 中可能看起来像这样:

spring:
  cloud:
    gateway:
      routes:
        - id: httpbin-route
          uri: https://httpbin.org
          predicates:
            - Path=/status/**
Run Code Online (Sandbox Code Playgroud)

有了这个要求就像

  • http://localhost:8080/test/hello将由您的休息控制器解决
  • http://localhost:8080/status/200将被重定向到 httpbin 站点

如果由于某种原因,两种情况具有相同的根路径,则控制器将具有优先权。