A SAGA microservice application - 6

In this post, we will make our microservice that we have designed and coded in the previous posts able to run with single command. Instead of running each service one by one, we ill understand the system and create a unified command. I will also mention the prerequisites and how the project is structured on github.

Minimum requirements

There are some mandatory prerequisites to be able to run this microservice. I have mentioned that these concepts require 2 years junior developer level of knowledge in one of the previous posts. If you are new to this subject i suggest you to try to run it by dealin with problems and studyin the code. Let me write down the ingredients:

  • You need JDK 11 installed on your computer
  • A Maven binary on your system and variables like M2 home set
  • An IDE that can open spring boot projects with maven settings configured (Eclipse, IntelliJ)
  • MySql installed and a schema created in the server
  • Minimum 5 - 6, ideally 9 - 10 gb free ram recommended :)

When it comes to ram usage, you may be able to limit or regulate it with JVM settings, since all of the projects run a tomcat server by iteself. But i didn't dig deep on that. It is also possible to run all the services on 1 tomcat. You can exclude spring-boot-starter-tomcat inside starter-web. It would be logical to create war files istead of jar files in this case. You should also set a common Tomcat deployment folder for maven to put the war files. There are CI/CD solutions about this subject so i didn't try to go deeper here. By the way, we said maven...

Maven

Maven is a compiling tool. It takes your code and compiles them with Javac and packages them in a Jar or War file in a certain folder. All the projects you create in spring initializr are maven projects in this series. Of course initializr provides gradle projects too. With the help of the POM file in these projects, maven finds the dependencies automatically and compiles them. There is also the option to create maven projects as modules. You can define a top level manifest and create maven modules under that project. But in this case, all the services would have to inherit the same pom file, which would be bad practise. That is why all the projects are an independent maven project.

Git

Git is the tool that tracks our code and let multiple developers work on the same repository. I have inserted all the projects under 1 project. This way, it will enable you to treat it like a workspace and you don't have to clone all the projects one by one. Furthermore, you can either commit the top level project or commit each projects independently while pushing your code. To achieve this structure, i have created a git repository and cloned it and copied and pasted my projects inside it. I am lazy and thought it would make everything easier :)

You also need a private repository on github for the config server to connect and fetch the property files. The filenames here must be exact math with applicaton names (application.name-<profile>). Since it is private, you can't download mine but you can download this and push it to your repo.

Import

Projelerin hepsi maven projesi olduğu için github 'dan indirildiğinde rahatlıkla import edebilirsiniz. Elinizde import edeceğiniz 11 adet proje var. Eclipse ile "Import -> Existing Maven Project" işlemi ile import edebilirsiniz.

MySql

MySql is a databse technology and a server. You need MySql in order to run the services. Because 3 small service (userservice, activityservice and paymentservice) have JPA and MySql dependencies. JPA dependency will look for DB properties and it won't run without them and connecting to a databse. The necessary properties are in the private repository files.

One click start

Let's talk about the elephant in the room :) You will run 11 spring projects and tomcat servers. I didn't have issues on my computer with 32 GB ram but when you run it all with eclipse, i have seen 9 - 10 gb of ram usage in total. All of the projects (except "ab-util") are running as web apps on tomcat because of starter web dependencies. After you import the projects, you must have a screen similar to this.

The small part on the bottom left is spring boot dashboard that comes with eclipse. You can run, restart, debug and stop spring boot projects. But first you need to right click on projects in the projects list on top and "run as -> spring boot application" for once. You should follow "Eureka -> Config Server -> Gateway and the others" order. Eureka need to run first since all the services will connect to it. Config server comes next because all other services will get properties from it. It runs after Eureka to register itself. You can't run the small services more than 1 time from the dashboard. You must start the apps twice with run as -> spring boot application. This is a torture for a lazy person like me :)

We can create a launch group to launch the whole system with one single command. This way, you can put the run operations (we did it once) with a sequence. You can define the configuration like the image below.

You can create a new launch configuration from the panel on the left. Then you must add the spring boot run configurations with the necessary order. But config server should not launch before Eureka launch is completed. Likewise, the other projects must wait for config server to complete the launch. You can set a command to wait for a command line expresison in this situations. This setting can be set with post launch action. It is the function to do after run operation. Because eclipse doesn't wait for the launching to complete like in maven install. You can go into the edit screen to set this one.

You can actually set the reqular expression however you'd like here. I have seen that the spring boot writes the main class as the application and managed to control that. When i set ".*Started AldimbiletEurekaApplication.*" as regular expression it detects the "Started AldimbiletEurekaApplication" in somehere in the console and it means the launch is completed. Similarly, i have used ".*Started AldimbiletConfigApplication.*" for the config server and make the other runs wait for it. After these two are launched, you can run all the services twice if you want, except the gateway. You can simply add the run configuration twice in order to achieve this. It will run "run as -> spring boot" command twice. If you complete this configuration and run it, you can see the image below in the eureka console.

We can basically run the whole system and the MVC app with a single command in eclipse this way. The MVC app runs ons localhost:80. As i mentioned in the beginning, if you run the failovers too, the system takes about 9 - 10 GB of ram.

Run microservice run

Finally our microservice journey is completed at http://localhost address :) If you have followed this series to this point, you will have your own microservice application up and running. In this series, i have mentioned my own mistakes and probable torubles and avoided "this code is doing this" approach. I hope now you have your own microservice and the knowledge to customize or improve it.

In the last post of this series, we will focus on alternative technologies, what could have been done, what did we do or was it the right thing to do kind of questions. I have mentioned in the first post, there is no one universal right when it comes to microservices. We have alternatives and trade offs. You may say "What kind of microservice is this" at the end. But let me defend myself in the next post :)


Leave a comment