Traefik

Traefik overview

Traefik is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease.

It can route HTTP requests like Zuul, so it has some overlap with a JHipster gateway, but it works on a lower level than an API Gateway: it only routes HTTP requests and does not provide rate limiting, security or Swagger documentation aggregation.

One of the benefits of Traefik is that is can work with many different service discovery solutions: with JHipster, however, it only works with Consul by default.

It can be used in two different architecture styles, described below.

Architecture diagram 1: default configuration

As Traefik is a reverse proxy and load balancer, it supercedes Zuul, and it routes directly all HTTP requests to the correct service.

Diagram

In that architecture, a JHipster “gateway” is not a real gateway anymore, it is mostly here to serve the Angular application.

This is our default configuration.

Architecture diagram 2: Traefik and Zuul

Traefik can also work with Zuul: in that case, an HTTP request to a microservice goes through Traefik and then through Zuul before reaching its destination.

Diagram

This makes one more network request, and thus is less efficient than the previous architecture. However, this allows to use a JHipster gateway to its full potential: it can handle rate limiting or Swagger documentation aggregation.

As a result, Traefik can be used as an edge service, which allows to scale JHipster gateways.

This configuration works out-of-the-box with JHipster: the only issue is that client-side applications use an absolute URL, so for example, for “microservice1”:

  • The default URL is “/microservice1”, which goes only through Traefik (this is the “default configuration” above).
  • The “/gateway/microservice1” URL would use the “gateway” application configured in Traefik, which would then use Zuul to reach the “microservice1” application.

Getting started

Please note that Traefik only works with Consul, so this cannot work if you use a JHipster Registry.

To use Traefik in a microservice architecture, run the docker-compose sub-generator and select Traefik when you have the question asking you which gateway you want to use.

This will generate a traefik.yml configuration for running Traefik in Docker, as well as a traefik/traefik.toml file, which is Traefik’s configuration file.

This configuration file is set up so that:

  • Traefik runs on port 80, so if you have an application called gateway, you can reach it by going to http://localhost/gateway.
  • The Traefik administration UI is available on port 8080, so you can visit it at http://localhost:8080.

As Traefik is using Consul, it will also be useful to check the Consul administration UI, which is available on port 8500: http://localhost:8500.