首页 > 解决方案 > Kotlin/Spring - 将帖子请求从一项服务转发到另一项服务

问题描述

我们有一个用于处理所有前端和后端逻辑的 Spring Web 应用程序。由于我们需要扩展,我们将其拆分为两个微服务。我将如何将发布请求“转发”到另一个 url(包括其正文和身份验证标头)。例如:

microservice1有一个端点/api/doSomething

microservice2有一个端点/privateUrl/doSomething

我希望用户点击microservice1将发布microservice2 并返回结果的端点。

我尝试使用 RestTemplate 时运气不佳,我不断收到来自微服务 1 的错误“无法编写 JSON:找不到类的序列化程序...”,我怀疑这是因为微服务 1 不知道如何解析微服务 2 需要的主体对象。

微服务1:

@PostMapping("/api/DoSomething")
fun postIT(request: HttpServletRequest, @PathVariable one: String, @PathVariable two: String){ ...}

微服务2:

@CrossOrigin
@PostMapping("/privateUrl/doSomething")
fun postIT(request: HttpServletRequest, @RequestParam one: String, @RequestParam(required = false, defaultValue = "true") two: String,@RequestBody it: IT) { ... }

我知道我可以解析整个请求microservice1然后将其发送到microservice2,但是有没有办法将 http 请求“转发”到新的 url?

标签: spring-boothttpkotlin

解决方案


我不确定你的意思,但是如果你想从一台服务器到另一台服务器进行通信,你总是可以使用 rest 模板(或任何其他模板)并将 http 请求从一个服务发送到另一个服务,你可以在这里查看示例 https://www .tutorialspoint.com/spring_boot/spring_boot_rest_template.htm https://spring.io/guides/tutorials/bookmarks/

看看它应该适合你。


推荐阅读