Kotlin / Spring-将发件人请求从一项服务转发到另一项

时间:2019-07-29 01:16:22

标签: spring-boot http kotlin

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

microservice1具有端点/api/doSomething

microservice2具有端点/privateUrl/doSomething

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

我在没有太多运气的情况下尝试过RestTemplate,我一直从microservice1中收到错误消息“无法编写JSON:找不到类的序列化程序...”,我怀疑这是因为microservice1不知道如何解析主体对象microservice2所需

微服务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?

1 个答案:

答案 0 :(得分:0)

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

看看它应该对您有用。

相关问题