如何在微服务应用程序中注入 PlatformApi?
How to inject PlatformApi in a Microservice Application?
我需要构建一个小型微服务来接收 REST 请求并在 Cumulocity 中存储数据(设备、测量等)。
对我来说,从文档中我不是很清楚如何将 platformApi 轻松注入我的 Spring 启动应用程序。尤其是Scope(TenantScope & UserScope)的用法不清楚。
你能给出一个非常简单的 "hello-world" 示例如何自动装配 platformApi(例如库存)并在应用程序启动时在租户范围内做某事(打印出所有设备)并使用 RestControler RequestMapping 在租户范围内做某事用户范围?
这是一个代码片段:
package c8y.example;
import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.microservice.context.inject.TenantScope;
import com.cumulocity.sdk.client.inventory.InventoryApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
@MicroserviceApplication
@RestController
public class App{
@Autowired
InventoryApi inventoryApi;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@PostConstruct
public void init() {
System.out.println("Devices: " + inventoryApi.getManagedObjects());
}
@RequestMapping("devices")
public String greeting() {
return "Devices: " + inventoryApi.getManagedObjects();
}
}
到目前为止,我尝试的所有操作都会导致启动时出现错误,即无法注入 bean 或者它们不在所需范围内或不在上下文中。
我有一个 application.properties,其中包含 bootstrap 凭据。
我找到了第一个解决方案,但我仍然不确定租户和用户范围之间的区别以及何时使用范围。但也许这有一点帮助。
我无法自动装配库存 API 但我能够自动装配平台 API,您可以从中使用库存 API。
package c8y.example;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.model.idtype.GId;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
import com.cumulocity.sdk.client.Platform;
import com.cumulocity.sdk.client.inventory.InventoryApi;
@MicroserviceApplication
@RestController
public class App {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(App.class);
@Autowired(required = true)
@Qualifier("userPlatform")
private Platform platformApi;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@RequestMapping("/")
public String greeting() {
if (platformApi != null && platformApi.getInventoryApi() != null) {
final InventoryApi inventoryApi = platformApi.getInventoryApi();
final ManagedObjectRepresentation managedObjectRepresentation = inventoryApi.get(new GId("5749"));
log.info(managedObjectRepresentation.toString());
return "device: " + managedObjectRepresentation.getName();
}
return "hello world";
}
}
最初有一个可用的范围,它是 UserScope,但是当您查看 CumulocityClientFeature class 时,您会看到 class 将租户范围的 PlatformImpl 设置为主。这意味着当您自动装配 PlatformApi 时,它将自动使用 TenantScoped Platform 而不是 UserScoped。这将失败,因为当前范围是 UserScope。要自动装配正确的 PlatformApi,您必须使用值为 userPlatform 的限定符,以便自动装配 UserScoped PlatformAPI。从平台 API 您可以访问库存 API。
尝试自动装配清单 API 对我不起作用,因为该应用找不到 UserScoped 清单 API。不知道为什么。
我需要构建一个小型微服务来接收 REST 请求并在 Cumulocity 中存储数据(设备、测量等)。
对我来说,从文档中我不是很清楚如何将 platformApi 轻松注入我的 Spring 启动应用程序。尤其是Scope(TenantScope & UserScope)的用法不清楚。
你能给出一个非常简单的 "hello-world" 示例如何自动装配 platformApi(例如库存)并在应用程序启动时在租户范围内做某事(打印出所有设备)并使用 RestControler RequestMapping 在租户范围内做某事用户范围?
这是一个代码片段:
package c8y.example;
import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.microservice.context.inject.TenantScope;
import com.cumulocity.sdk.client.inventory.InventoryApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
@MicroserviceApplication
@RestController
public class App{
@Autowired
InventoryApi inventoryApi;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@PostConstruct
public void init() {
System.out.println("Devices: " + inventoryApi.getManagedObjects());
}
@RequestMapping("devices")
public String greeting() {
return "Devices: " + inventoryApi.getManagedObjects();
}
}
到目前为止,我尝试的所有操作都会导致启动时出现错误,即无法注入 bean 或者它们不在所需范围内或不在上下文中。 我有一个 application.properties,其中包含 bootstrap 凭据。
我找到了第一个解决方案,但我仍然不确定租户和用户范围之间的区别以及何时使用范围。但也许这有一点帮助。
我无法自动装配库存 API 但我能够自动装配平台 API,您可以从中使用库存 API。
package c8y.example;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.model.idtype.GId;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
import com.cumulocity.sdk.client.Platform;
import com.cumulocity.sdk.client.inventory.InventoryApi;
@MicroserviceApplication
@RestController
public class App {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(App.class);
@Autowired(required = true)
@Qualifier("userPlatform")
private Platform platformApi;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@RequestMapping("/")
public String greeting() {
if (platformApi != null && platformApi.getInventoryApi() != null) {
final InventoryApi inventoryApi = platformApi.getInventoryApi();
final ManagedObjectRepresentation managedObjectRepresentation = inventoryApi.get(new GId("5749"));
log.info(managedObjectRepresentation.toString());
return "device: " + managedObjectRepresentation.getName();
}
return "hello world";
}
}
最初有一个可用的范围,它是 UserScope,但是当您查看 CumulocityClientFeature class 时,您会看到 class 将租户范围的 PlatformImpl 设置为主。这意味着当您自动装配 PlatformApi 时,它将自动使用 TenantScoped Platform 而不是 UserScoped。这将失败,因为当前范围是 UserScope。要自动装配正确的 PlatformApi,您必须使用值为 userPlatform 的限定符,以便自动装配 UserScoped PlatformAPI。从平台 API 您可以访问库存 API。
尝试自动装配清单 API 对我不起作用,因为该应用找不到 UserScoped 清单 API。不知道为什么。