spring-boot - 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?
解决方案
我不确定你的意思,但是如果你想从一台服务器到另一台服务器进行通信,你总是可以使用 rest 模板(或任何其他模板)并将 http 请求从一个服务发送到另一个服务,你可以在这里查看示例 https://www .tutorialspoint.com/spring_boot/spring_boot_rest_template.htm https://spring.io/guides/tutorials/bookmarks/
看看它应该适合你。
推荐阅读
- python - 需要帮助理解 GPT-2 源代码中的 Python 函数
- laravel - Laravel - 设置默认路由前缀
- datatables - DataTables - 服务器端:“未找到匹配记录”和“从 NaN 总条目中过滤”
- c# - Microsoft Teams 到 staticTab 的深层链接仅打开带有清单中默认 URL 的选项卡
- python-3.7 - 如何使用python在谷歌表格中一一添加数据
- python - CPython 中逆向操作 some_list[::-1] 的时间和空间复杂度是多少?
- java - Mac - TFS - Eclipse 错误:无法实例化类“com.microsoft.tfs.client.common.ui.views.TeamExplorerView
- wpf - 使用通用类制作带有复选框用户控件的 WPF 组合框
- kubernetes - 如何使用 KeyCloak 解决 Kong 入口中的“无效响应......来自上游服务器”?
- c# - 生成方法片段 Visual Studio