Microservices Best Practices: Multi-Tenant microservices with Java SDK

Cumulocity IoT is a comprehensive IoT platform that enables device management, data collection, and analysis. The Cumulocity IoT Microservice SDK for Java allows developers to interact with the platform programmatically. Multi-tenancy is a key feature of Cumulocity, allowing multiple independent tenants to coexist on the same platform, each with their own set of resources.

Usage of Cumulocity IoT Microservice SDK for Java

The Cumulocity IoT Microservice SDK for Java can be used for a variety of tasks including device management, data collection, sending and receiving measurements, alarms, events, and more. It is designed to simplify the integration of devices and applications with the Cumulocity platform.

Multi-Tenancy in Cumulocity

Multi-tenancy in Cumulocity allows for the separation of data and resources for different tenants (organizations or users) on a single platform instance. Each tenant has its own space where devices, users, applications, and other resources are managed independently.

Use Case of Cumulocity Multi-Tenant

Objective: To effectively manage multiple tenants within a single Cumulocity IoT platform instance, ensuring data isolation, security, and efficient resource utilization.

Scenario: A global manufacturing company, “XYZ Industries,” operates in several regions worldwide, each with its own set of IoT devices, data requirements, and management needs. They want to use Cumulocity IoT to manage their devices, collect data, and perform analytics while ensuring that each region operates independently within the same platform.

Key Requirements:

  1. Data Isolation: Each region’s data must be isolated from others to maintain privacy and security.
  2. Customizable Dashboards: Regions need customized dashboards to monitor their specific devices and KPIs.
  3. Scalable Services: The ability to scale services based on the number of devices and data load in each region.
  4. Centralized Management: A central administrator should be able to manage global policies and settings while allowing regional administrators to manage their configurations.
  5. Cost Efficiency: Optimize the use of resources to reduce costs by sharing the same infrastructure.

Implementation:

  1. Tenant Structure:
  • Root Tenant: “XYZ”
  • Sub-Tenants:
    • “North America”
    • “Europe”
    • “Asia”
    • “South America”
    • “Africa”
  1. Features and Configuration:
  • Data Isolation: Each region (sub-tenant) has its own isolated data environment ensuring that data from “North America” is not accessible to “Europe,” and vice versa.
  • Customizable Dashboards: Regional administrators can create and customize dashboards tailored to their specific needs, showing relevant data, alerts, and analytics.
  • Scalable Services: The platform supports horizontal scaling to accommodate the varying number of devices in each region. Shared subscriptions allow for efficient handling of message loads.
  • Centralized Management: The central administrator at “XYZ” (root tenant) can define global policies, security settings, and shared services. Regional administrators manage local devices, users, and configurations.
  • Cost Efficiency: By using a multi-tenant structure, “XYZ” reduces infrastructure costs while maintaining flexibility and scalability for each region.

Benefits:

  • Efficient Resource Utilization: Shared infrastructure reduces overall costs while maintaining high performance and reliability.
  • Enhanced Security: Data isolation and customizable security policies ensure that each region’s data remains secure and private.
  • Scalability: Scaling services based on regional demand allows for efficient resource management.
  • Flexibility: Each region can operate independently with its custom configurations while adhering to global standards set by the central administration.
  • Centralized Control: Global policies and settings can be managed centrally, ensuring consistency and compliance across all regions.

Conclusion: The use of Cumulocity’s multi-tenant capabilities allows “XYZ Industries” to efficiently manage their global IoT operations, providing each region with the tools and autonomy they need while maintaining overall control and optimizing resource use.

Available Methods within Cumulocity IoT Microservice SDK for Java

The Cumulocity IoT Microservice SDK for Java provides a comprehensive set of APIs. Some of the key APIs and their methods include:

  • MicroserviceSubscriptionsService: Main entry point to the tenant as microservice gets subscribed on a tenant.
    • callForTenant(String var1, Callable<T> var2): Within this method any Cumulocity Java SDK API could be called for a specific tenant if a message needs to be returned.
    • runForTenant(String var1, Runnable var2): Within this method any Cumulocity Java SDK API could be called for a specific tenant if nothing is returned
    • runForEachTenant(Runnable var1): Within this method any Cumulocity Java SDK API could be called for all the subscribed tenants and nothing will be returned.

Example of Multi-Tenancy with Cumulocity IoT Microservice SDK for Java

Here’s an example of how you might interact with different tenants using the Cumulocity IoT Microservice SDK for Java:

Setup

  1. Add the Cumulocity Microservice SDK for Java to your project. Ensure you have the SDK dependency in your pom.xml if you are using Maven:
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.nsn.cumulocity.clients-java</groupId>
				<artifactId>microservice-dependencies</artifactId>
				<version>${c8y.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
</dependencyManagement>
<dependencies>
		<dependency>
			<groupId>com.nsn.cumulocity.clients-java</groupId>
			<artifactId>microservice-autoconfigure</artifactId>
			<version>${c8y.version}</version>
		</dependency>
</dependencies>
  1. Create a client for all the subscribed tenants:
 subscriptionService.runForTenant(tenantId, ()-> {
            try {
                //Do the Cumulocity API call for the specified tenant
            }catch(Exception e) {
                //log the error message
            }
        });
return subscriptionService.callForTenant(tenantId, ()-> {
            try {
                 //Do the Cumulocity API call for the specified tenant and return the result
            }catch(Exception e) {
                 //Log the error message as well as return the same
            }
        });
subscriptionService.runForEachTenant(() -> {
        try {
         
               //Do the Cumulocity API call for each subscribed tenant

        } catch (SDKException exception) {
               //log the error message
        }
    });

Advantages of Multi-Tenancy

  1. Resource Isolation: Each tenant’s data and resources are isolated, ensuring privacy and security.
  2. Scalability: Efficiently manage multiple organizations or projects on a single platform.
  3. Cost Efficiency: Reduce infrastructure costs by sharing resources among multiple tenants.
  4. Simplified Management: Centralized management of multiple tenants with easy maintenance and updates.

Conclusion

The Cumulocity IoT Microservice SDK for Java provides a robust framework for managing IoT devices and data in a multi-tenant environment. Its comprehensive APIs simplify the development process, allowing for efficient and secure management of multiple tenants. Understanding the advantages, limitations, and available methods will enable you to leverage the full potential of the Cumulocity platform in your IoT projects.

1 Like