Web application as microservice in Java

Web application as microservice in Java

Avadhut Phatarpekar bio photo By Avadhut Phatarpekar Comment

Motivation:

  1. I want to run multiple web services (applications, REST APIs, etc.) on shared infrastructure.
  2. However, if one of these goes down it should not take down the entire web container with them, thereby disabling other services.
  3. Solution: embed the container, which in my case is Jetty.

The setup we run:

Step 1: Have your main start an instance of the embedded Jetty on a port number you desire. In our organisation, we use ports 8XXX. See Startup.Java (→link).

Step 2: Package it.

mvn -V clean package

Step 3: Run/Deploy the JAR on your infrastructure box. We use Fabric.

Step 4: Use Apache vhost configuration to supply custom (non-port-number-based) URLs.

<VirtualHost *:80>
  ServerName custom.sample.url
  ProxyPass / http://localhost:8090/
  ProxyPassReverse / http://localhost:8090/
</VirtualHost>

<VirtualHost *:443>
  RequestHeader set X-Forwarded-Proto "https"
  RequestHeader set X-Forwarded-Port "443"
  
  ServerName custom.sample.url
  ProxyPass / http://localhost:8092/
  ProxyPassReverse / http://localhost:8092/
</VirtualHost>

Here’s a link to the entire codebase on Github—web-app-microservice.

I like this approach as I think it is simple, clean, and relatively easy to understand and implement. However, if you spot any problems or have any suggestions, please leave a comment. (ツ)