【问题标题】:Cumulocity Inter-microservice communication best practiceCumulocity 微服务间通信最佳实践
【发布时间】:2018-09-24 16:00:27
【问题描述】:

我开发了一个调用 smartrule 微服务的微服务。 我找到: http://www.cumulocity.com/guides/reference/microservice-runtime#Access-to-the-platform-and-other-microservices

但是,如果微服务是另一个租户的下标,这并不能解决。这意味着不能使用基本 URL。

我想出了以下解决方案,它正在工作,但我想问是否有更好的方法?我还看到了建立与实际租户不同的 URL 的风险。我记得在 Cumulocity 培训中,URL 模式可能不同,并且租户在反映的 URL 中不能准确!也许微服务 SDK 对在上下文中调用另一个微服务器有更好的支持?

    private boolean callSmartRuleService(SmartRuleRepresentation requestBody, String groupId) {     
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64Credentials(contextService.getContext().getUsername(), contextService.getContext().getPassword()));
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<SmartRuleRepresentation> requestEntity = new HttpEntity<>(requestBody, headers);

    StringBuilder urlBuilder = new StringBuilder();
    urlBuilder.append("https://")
              .append(contextService.getContext().getTenant())
              .append(c8yBaseURL.substring(c8yBaseURL.indexOf(".", 8)))
              .append("/service/smartrule/managedObjects/")
              .append(groupId).append("/smartrules");

    ResponseEntity<String> response = restTemplate.exchange(urlBuilder.toString(), HttpMethod.POST, requestEntity, String.class);
    if (response.getStatusCode().is2xxSuccessful()) {
        return true;
    } else
        return false;
}

【问题讨论】:

  • 抱歉,如果微服务在托管运行,则此方法不起作用。如果微服务运行托管属性 c8YBaseURL=cumulocity:8111。这应该如何在多租户环境中工作

标签: cumulocity


【解决方案1】:

您不应该尝试构建自己的 URL。只需发送到微服务中环境变量提供的 baseURL。

您只需将租户包含在凭据中(租户/用户名:密码)。然后 Cumulocity 将注意在正确的租户上下文中对其进行处理。当租户作为凭据的一部分传递时,Cumulocity 将始终从该凭据构建上下文(无论域如何)。

请记住,当您与另一个微服务交谈时,租户可能没有此微服务或者该微服务不可访问/崩溃等。您的服务应该能够处理这些情况。

【讨论】:

猜你喜欢
  • 2020-06-04
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
相关资源
最近更新 更多