Creating a microservice that connects with the Platform

EN | ES


We can communicate with the Platform using a microservice. To do so, we have an available template with a small code example.

Step 1: download sourcecode from the platform

To download the template, you must download the source code from th e platform:

git clone https://github.com/onesaitplatform/onesait-cloud-platform-examples.git

Step 2: copy and edit the template from the microservice

Once the platform's source code has been downloaded, go to the/onesait-cloud-platform/sources/examples directory and select the microservice-template directory:

If you study that directory, you'll find the pom.xml file. This is a file required by Maven that includes information on the project along with its configuration, library dependencies, packaging plugins, etc...

On one side you have the Spring dependencies, and on the other, the onesaitplatform-iotclient4springboot library, that allows to connect with the Platform. For more information, read this post

It is recomended to use the last version of the dependency from repository.

<dependency>
    <groupId>com.minsait.onesait.platform</groupId>
    <artifactId>onesaitplatform-iotclient4springboot</artifactId>
    <version>1.3.6</version>
</dependency>

This dependency is placed on onesaitplatform public repository.

<repositories>
    <repository>
        <id>onesait</id>
        <url>https://nexus.onesaitplatform.com/nexus/content/repositories/releases/</url>
    </repository>
</repositories>


It is here that all the dependencies your microservice neds must be added.

Step 3: compile the microservice's source code

From the directory you copied the template at, launch this command:

mvn clean install


This command will download the dependencies and compile the sources, and also will generate the microservice's .class and packages.

Once the command has been executed, you should see something like this:

Step 4: Run the microservice (with and without Eclipse)

This microservice is written in Java language and uses Spring's stack, specifically SpringBoot, as its development framework. To do this, the applicaiton's main class has been defined, with the needed annotations being the main ones:

@EnableAutoConfiguration
@ComponentScan

In Eclipse, if you right-click on the MicroserviceTemplateApplication.java class and select Run As > Java Application

If everything went OK, you will see something like:

You can also run your microservice outside Eclipse.

You must first have executed Step 3 to generate the corresponding .jar. If you go to the \microservice-template\target directory, you will find two .jar files.

One of them, ending in -exec.jar, is a file including all the dependencies along with an embedded Tomcat on which your module will be executed with the command:

java -jar microservice-example-0.0.1-SNAPSHOT-exec.jar


Step 5: Generate the microservice's Docker image, then execute the container

The application-docker.yml file contains specific properties that overwrite those in the application.yml file for the execution of your module in a Docker container:

On the other side, you also have the Dockerfile file, including the instructions needed to generate the microservice's Docker image. To do this, you need to have previously installed the Docker engine.To generate the image, you only need to copy the microservice-example-1.1.0-SNAPSHOT-exec.jar , in the /docker module's /target directory:

And from that directory, run this sentence:


docker build -t onesaitplatform/microservice:latest


With this, you have the generated image in your local. To start the container, run the command:

docker run -t -p 10000:10000 onesaitplatform/microservice:latest

You can access your module thorugh a web browser by listening in port 10000: http://localhost:10000/