A demonstration of using Consul Connect service discovery and Traefik reverse proxy and load balancer with Spring Boot application.
Traefik is a dynamic reverse proxy. ConsulConnect is a service discovery server. Traefik uses ConsulConnect as a backend. SpringBoot application registers a service to Consul with Traefik specific metadata. Traefik adds a new route using this metadata.
Start Portainer, Consul and Traefik:
docker-compose up -d portainer traefik consul
Open browser:
- Portainer https://localhost:9000
- Consul https://localhost:8500
- Traefik https://localhost:8080
Build backend service and frontend application:
./build.sh
docker-compose up -d
Check that there are user-repository-v1 and user-application-v1 services in Consul: https://localhost:8500
- Stop user-repository in Portainer
- Note that user-repository-v1 shows error in Consul
- Start user-repository in Portainer
- Note how quickly user-repository-v1 becomes available in Consul
Traefik shows a http port for user-application: https://localhost:8080
Application should be available through reverse proxy: https://localhost/users
- Login to application https://localhost/users
- Note service logs in Portainer
Traefik also show alternative address for Consul: https://https://consul.docker.localhost
scale services:
docker-compose up -d --scale user-repository=2
- Portainer shows two instances for user-repository
- Consul shows two instances of service user-repository-v1
- Trafik shows two instances
- Login to application
- Note service logs in Portainer
- Refresh application or logout/login
- Note service logs in Portainer, services are called in round-robin manner
- Login to application
- Note the runtime environment value in footer
- Add config value to Consul: config/user-application/runtime/environment=production
- Refresh application view in browser
- Note the new runtime environment value
- Note the application log in Portainer