如何在 post 上检索 API 网关 URI

How to retrieve the API Gateway URI upon post

我正在使用 java8 和 Spring 云在微服务架构中开发一个系统。我实现了一个 post rest 控制器,它在 json 中接收一个对象,然后将它保存在数据库中。问题是;我如何获取包含刚刚保存的对象的 API 网关的 URI,以便我可以 return 它在创建的响应主体上?

就像,当我使用 URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(savedProfile.getId()).toUri(); 时,我得到 http://Boss.mshome.net:8081/profile/1 而不是 localhost:8765/nukr-profile-service/profile/1,后者是 API 网关路径的端点。

检索此 URI 的最佳做法是什么?

所以,我找到了一个解决方案,我只是不知道它是否是最佳实践。 我的 post 方法最终变成了这样:

@PostMapping("/profile")
    public ResponseEntity<Object> createProfile(@Valid @RequestBody Profile profile) {
        UriComponents requestComponents = ServletUriComponentsBuilder.fromCurrentRequest().path("").build();
        InstanceInfo gatewayService = this.eurekaClient.getNextServerFromEureka(NUKR_API_GATEWAY_SERVICE, false);
        Profile savedProfile = this.profileRepository.save(profile);

        String uriStr = requestComponents.getScheme() + "://" + gatewayService.getIPAddr() + ":" + gatewayService.getPort() + "/" + this.profileServiceName +
                requestComponents.getPath() + "/" + savedProfile.getId();

        return ResponseEntity.created(URI.create(uriStr)).build();
    }