Create Microservice Docker Containers with MATLAB
In contrast to traditional monolithic applications where all the logic is found in a single piece of code, microservices are an architectural style where your application is composed of many small, independent services. Modern microservices are typically implemented as a Docker® container with an HTTP RESTful API communicating over JSON payloads. Learn how to create a microservice Docker container with MATLAB® in two easy steps.
Published: 21 Mar 2022
Did you know that you can create microservices with MATLAB? In this video, we will show you how in two easy steps. First, let's examine what a microservice is.
I took this definition from the Amazon Web Services site. Basically, microservices are an architectural style where your application is composed of many small, independent services. This is in contrast to traditional, monolithic applications, where all the logic is located in a single piece of code.
Each microservice today is typically implemented as a Docker container, with HTTP RESTful endpoint communicating over JSON payloads. To get started, you need MATLAB Compiler SDK R22a Note that you need to run in Linux to create the microservice. But once created, the microservice can be run on any platform that runs Docker containers.
Let's see how we can create a microservice in MATLAB. We have, here, a simple function that calculates the price of a bond from four basic inputs. And we are going to turn it into a microservice in two simple steps. Now the first step, here, is we are going to compile this microservice, using the compiler.build.ProductionServerArchive command, specifying the function we want to include.
Now, the compiled artifact for a microservice and a MATLAB Production Server API are the same. So we use the same command. And what this does is, that it allows you to migrate to MATLAB Production Server, at a later point, without any modification.
All right, now that we have compiled our function, the second step is that we need to package it into a Docker container with a microservice endpoint. And for that, we're going to use the command called compiler.packagedot.microserviceDockerImage, and give the container a name, which, in this case, is bondtools.
Now, if this is the first time you are doing this, it might take a couple of minutes to download all the components and to perform the Docker build. I've already done this earlier so it completes very quickly. Now you'll notice at the end, over here, that we provide a set of helpful instructions in a getting started guide to run the container. Let's copy the command to run the container.
And then we'll switch over to our terminal. And over here, you can see that, if I run the docker images command, that our newly built image is listed over here-- bondtools. And if I were to paste our run command-- execute this-- the running container is now going to listen to requests on the port that we specified. In this case, port 9900.
Now, let's switch over to a utility called Postman, and make some restful calls to our microservice. You'll see over here that we are making a call to the URL, using the port that we specified. And for the input payloads, we're using JSON, and we're passing in the four inputs that are found over here. So let's go ahead and send this to our microservice, and we'll receive a result, over here-- just, basically, the price of our bond.
Now that you have your microservice, what do you do with it? Well, in most cases, you would use Docker push to send it to a corporate or cloud repository to learn more, check out these links.