首页 > 解决方案 > Zuul Gateway 服务器在本地处理请求

问题描述

如何让 Spring Boot Zuul 代理服务器在本地处理特定请求,而不是将其代理到其他服务器?
假设我想通过 API 对 Zuul 代理服务器本身进行自定义健康检查,该 API 应该返回本地响应,而不是从其他远程服务器返回代理响应。

转发请求需要什么样的路由配置?

标签: spring-bootlocalnetflix-zuul

解决方案


以下是我想出的,它对我有用:

1)创建本地请求处理程序:
在Zuul代理服务器代码的文件中添加一个带有RequestMapping的常规Controller/RestController

@RestController
public class LocalRequestHandler {
    @GetMapping(path = "/status", produces = MediaType.TEXT_PLAIN_VALUE);
    private static ResponseEntity<String> getServiceStatus() {
        String status = "I am alive";
        return ResponseEntity.ok().body(status);
    }
}

2)在定义了其他路由的配置文件(application.properties 或视情况而定)中创建本地转发路由。在我的例子中,Spring Boot Zuul 代理服务器在 Jetty 容器上运行,GatewaySvc 作为应用程序上下文。

zuul.routes.gatewaysvc.path=/GatewaySvc/status
zuul.routes.gatewaysvc.url=forward:/GatewaySvc/status 

对于我尚未测试过的独立 Spring Boot,您可能必须从上述配置中删除上下文路径。

zuul.routes.gatewaysvc.path=/status
zuul.routes.gatewaysvc.url=forward:/status

参考: 绞杀模式和本地前锋


推荐阅读