LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub
Star

What's on this Page

  • Introduction
  • Configuring the APIs
    • API A
    • API B
    • API C
    • API D
  • Start Services
    • Test Servers

Service Discovery with Docker Containers

Using service discovery patterns in a containerized environment.

Introduction

In this step, we’re going to dockerize all the APIs and then use Consul for service registry. To make it easier, we are going to use docker-compose to put everything together.

First, let’s copy from consul to consuldocker for each API.

cd ~/networknt
cp -r light-example-4j/discovery/api_a/consul light-example-4j/discovery/api_a/consuldocker
cp -r light-example-4j/discovery/api_b/consul light-example-4j/discovery/api_b/consuldocker
cp -r light-example-4j/discovery/api_c/consul light-example-4j/discovery/api_c/consuldocker
cp -r light-example-4j/discovery/api_d/consul light-example-4j/discovery/api_d/consuldocker

Configuring the APIs

API A

We will be running in a dockerized environment, so having consul configured at “localhost” is no longer valid. We will be using the DOCKER_HOST_IP in the consul.yml file. On my local computer, the IP is 192.168.1.144. You can use ifconfig on Linux to find out your local IP.

When starting the docker-compose for services, we need to set up the DOCKER_HOST_IP as an environment variable to pass it into the container. For more info on how to set up the DOCKER_HOST_IP, please refer to docker host ip.

In consul.yml, change the consulUrl to use the host IP instead of localhost as localhost inside a container is the container loopback address and it cannot go outside of the container.

consulUrl: http://192.168.1.144:8500

The Dockerfile generated from light-codegen should be available like this.

docker/Dockerfile

FROM openjdk:11.0.3-slim
ADD /target/aa-1.0.0.jar server.jar
CMD ["/bin/sh","-c","java -Dlight-4j-config-dir=/config -Dlogback.configurationFile=/config/logback.xml -jar /server.jar"]

Let’s build and tag a docker image for this service:

cd ~/networknt/light-example-4j/discovery/api_a/consuldocker
mvn clean install -Prelease
docker build -t networknt/com.networknt.aa-1.0.0 -f docker/Dockerfile .

API B

consul.yml

consulUrl: http://192.168.1.144:8500

The Dockerfile generated from light-codegen should be available as following.

docker/Dockerfile

FROM openjdk:11.0.3-slim
ADD /target/ab-1.0.0.jar server.jar
CMD ["/bin/sh","-c","java -Dlight-4j-config-dir=/config -Dlogback.configurationFile=/config/logback.xml -jar /server.jar"]

Let’s build and tag the docker image for this service:

cd ~/networknt/light-example-4j/discovery/api_b/consuldocker
mvn clean install -Prelease
docker build -t networknt/com.networknt.ab-1.0.0 -f docker/Dockerfile .

API C

consul.yml

consulUrl: http://192.168.1.144:8500

The Dockerfile generated from light-codegen should be available as following.

docker/Dockerfile

FROM openjdk:11.0.3-slim
ADD /target/ac-1.0.0.jar server.jar
CMD ["/bin/sh","-c","java -Dlight-4j-config-dir=/config -Dlogback.configurationFile=/config/logback.xml -jar /server.jar"]

Let’s build and tag the docker image for this service:

cd ~/networknt/light-example-4j/discovery/api_c/consuldocker
mvn clean install -Prelease
docker build -t networknt/com.networknt.ac-1.0.0 -f docker/Dockerfile .

API D

consul.yml

consulUrl: http://192.168.1.144:8500

The Dockerfile generated from light-codegen should be available as following.

docker/Dockerfile

FROM openjdk:11.0.3-slim
ADD /target/ad-1.0.0.jar server.jar
CMD ["/bin/sh","-c","java -Dlight-4j-config-dir=/config -Dlogback.configurationFile=/config/logback.xml -jar /server.jar"]

Let’s build and tag the docker image for this service:

cd ~/networknt/light-example-4j/discovery/api_d/consuldocker
mvn clean install -Prelease
docker build -t networknt/com.networknt.ad-1.0.0 -f docker/Dockerfile .

Start Services

Now that we have all four API built and dockerized, let’s start the docker-compose.

</div>


<div class="admonition-content">

Before we start the new docker-compose, make sure you stop the current consul docker container.

Start Consul using docker-compose:

cd ~/networknt
git clone https://github.com/networknt/light-docker.git
cd light-docker
docker-compose -f docker-compose-consul.yml up -d

Start APIs once Consul is ready from the light-docker folder.

docker-compose -f docker-compose-discovery.yml up -d

Now you can go to the Consul Web UI to check if all APIs are registered.

http://localhost:8500/

Test Servers

curl -k https://localhost:7441/v1/data

And here is the result.

["API D: Message 1 from port 7444","API D: Message 2 from port 7444","API B: Message 1","API B: Message 2","API C: Message 1","API C: Message 2","API A: Message 1","API A: Message 2"]

In this step, we have dockerized all APIs and start them together. We also, start Consul with separate compose first to allow all services to register them to the Consul server. To make it simple, we are using HTTP to connect to the Consul and still using the static port in the server.yml config file. If you want to learn production like docker-compose and Consul integration please take a look at Docker compose and Consul production.

In the next step, we are going to deploy all services to Kubernetes cluster instead of docker-compose.

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Service Discovery with Docker Containers” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute