【发布时间】: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