Build solutions in ELMA365 / Create a custom microservice

Create a custom microservice

ELMA365 is an application with a microservice architecture. It consists of small-scale, loosely connected modules called microservices. These services are dedicated to certain business needs, and they are mostly used for one or several basic functions. Services are deployed independently. They can be written in different programming languages and use different data storage methods. Read more in the Architecture article.

You can create your own service and locate it inside the MicroK8s orchestrator. This allows you to expand the system’s functionality. For example, you can create a service for working with .xlsx files. It can read rows of data and save them to apps.

You need to create custom services only when other options don’t suffice. For example, you may need to add an external library. To create an integration with an external system, it is enough to use an extension.

You can write a service in any programming language. You just need a Docker image to deploy it in ELMA365.

Below is an example of creating a service using .NET Core 5.

Create a service with .NET

Open Visual Studio 2019. Create a new project and configure the settings as shown on screenshots below.

As ELMA365 is deployed on Ubuntu, enable Docker and select Linux as the container type.

create-service-1

create-service-2

create-service-3

Visual Studio will create a default project. You can run it in Docker to make sure that the service works and can be accessed.

After testing, move on to the next step.

Prepare the project and upload it to Docker Hub

To let the system download a Docker image, you need to place it in a Docker repository. In our example, we are going to use the Docker Hub public repository. To use it, you need to register an account.

  1. Select the project you need and right-click on its name.
  2. Click AddDocker Support… (depends on the target platform). Wait until the configuration is finished.
  3. Right-click the project’s name.
  4. Select Add Container Orchestrator Support…, then choose Docker Compose. Wait until the configuration is finished.
  5. Right-click the project’s name.
  6. Select Publish — Docker Container Registry — Docker Hub.

create-service-4

create-service-5

  1. Specify the login and password to Docker Hub and publish the project.

create-service-6

  1. When you click Finish, a page in Visual Studio will open. Here you can specify a tag for the image before publishing. You can use it to version the service.

create-service-7

  1. Click Publish. Visual Studio will build the Docker image and send it to the Docker Hub repository.

This public repository is used only as an example. Use private repositories for your services or create your own using docker-registry.

Install ELMA365

  1. Install ELMA365. Find detailed instructions in the Install ELMA365 to MicroK8s article.
  2. After installation you need to check service statuses. To do that, run the following command:

sudo microk8s kubectl get pods

  1. Make sure that the status of each service is Running.

Run your service

Create a deployment to manage the pod. A pod runs the container based on the provided Docker image.

To do that, run the following command:

sudo elma365ctl add-service service_name --image=user094404/testservice:004

Where:

service_name is the name of your service.

user094404/testservice:004 is the address where the image is stored (username/repository:tag).

You can expose the pod to the public interned by running the following command:

sudo elma365ctl expose-service service_name --port=80 --target-port=80

Test the service in the console

  1. Make sure the service appears in the list of pods by running the following command:

sudo microk8s kubectl get pods

You will see a list of pods with their statuses:

...
service_name-5655677c8d-rhswd   1/1     Running   0          4m21s
...

The service you added has be in the Running status.

  1. Test the service with the following command:

curl -v http://service_name:80/api/State

If the testing is successful and the service can be accessed, you will see this message:

*  Trying10.152.183.51...
*TCP_NODELAYset
*Connectedtoservice_name(10.152.183.51)port80(#0)
 
>GET/WeatherForecastHTTP/1.1
>Host:my-service
>User-Agent:curl/7.54.0
>Accept:*/*
>
 
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 08:19:53 GMT
< Content-Type: application/json; charset=utf-8
< Server: Kestrel
< Transfer-Encoding: chunked
<
 
* Connection #0 to host my-service left intact
 
{"type":"GET","version":"47.0.200.1129"} /

After testing you can move on to the next step.

Example of using a service in a widget

Let’s see how a service can be used.

We need to write a client-side script. When the widget is initialized, the server method WeatherForecast() will be called.

Client-side script:

async function onInit(): Promise<void> {
    await Server.rpc.WeatherForecast();
}

Now let’s write the script on the server side. 

Server script:

async function WeatherForecast(): Promise<void> {
    Context.data.test = "debug";
    try {
        const res = await fetch(`http://service_name:80/WeatherForecast`);
        if (!res.ok) {
            Context.data.test = `request fail: ${res.statusText}`;
            return;
        }
        Context.data.test = JSON.stringify(await res.json());
    }
    catch (error) {       
        Context.data.test = typeof(error) === 'object' ? `error: ${JSON.stringify(error)}` : `${error}`;
    }
}

Let’s add the widget to a page. The result will be the following:

create-service-8

Deallocation

To delete the service, run the following command:

sudo elma365ctl delete-service service_name

Found a typo? Highlight the text, press ctrl + enter and notify us