Microservice as a Windows service

Contents

A lot of companies use a Windows server to host microservices. I asked myself “what is the best way to deploy and start a microservice?”. Of course, the microservice has to be a Windows service so that on every server restart the services are restarted too. But what else is necessary?

Create a Windows service

There is a simple tool to create and edit Windows services: nssm (the Non-Sucking Service Manager). You can start the tool from the command line as an admin like this: nssm install <service-name>

The service name has to bei unique and I used to start the name with my company’s initials. After the creation of a service you can find the name only in the properties in the task manager.

Most important settings

nssm - application tab for Window service

As you can see in this screenshot nssm opens a dialog with several tabs. I use only a few of them to install a new service. On the first tab you can enter

  • the path to java.exe (e.g. c:\Program Files\Java\jre1.8.0_121\bin\java.exe)
  • the startup directory of the application, i.e. the directory where the jar of your microservice is located
  • minimal arguments are -jar <jar-name> but you can define heap size, spring profiles, logging configuration and other starting parameters you like.

For security reasons you should update the java version every few months. After installing the new java version there are only 2 simple actions left: a) call nssm edit <service name> and change the path on the first tab and b) restart the service.

nssm details tab

The second tab I use is about the service details and I set a display name and description for the service. These information is shown in the task manager overview and when I have a special port number for my microservice I add it in the description.

Additional settings

nssm input/output files

The next tab that I use is “I/O” with the configuration of a file for output and error. If Spring Boot banner.txt is activated, the banner is printed to the output. And every error message during the startup process should be written in the error file. All other (log) output should be written to the log file.

nssm file rotation tab

In the tab “File rotation” you can define settings for the output files.

A single-instance microservice

If you only need 1 instance of a microservice (at the moment) you can deploy all related stuff into one directory:

  • the jar file (with the embedded application server)
  • an application.yml with the configuration
  • the log4j2.xml with the log configuration
  • the logs
  • and the output files.

In this way I have only one place to look for configuration changes and information about the microservice.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.