0

I have 2 applications, both written using spring boot. Both are running in different docker containers. I also have consul running in a different docker container. I have exposed port 8500 for consul using docker-compose.yml file. So, how do I specify to my spring boot applications where to register themselves, i.e, where is consul running. Do I give the address of the mapped port (port mapped to my local machine), or some other change?

The example I'm using right now: https://github.com/Java-Techie-jt/cloud-consul-service-discovery

Edit:

docker-compose.yml:

version: "2"

services:
  consul:
    container_name: consul
    image: consul
    expose:
      - "8300"
      - "8400"
      - "8500"
    restart: always
  registrator:
    container_name: registrator
    image: gliderlabs/registrator:master
    volumes:
      - "/var/run/docker.sock:/tmp/docker.sock"
    command: -internal consul://consul:8500
    restart: always
    depends_on:
      - consul
  web1:
    image: deis/mock-http-server
    container_name: web1
    expose:
      - "8080"
    environment:
      SERVICE_NAME: "web"
      SERVICE_TAGS: "web"
    restart: always
    depends_on:
      - registrator
  web2:
    image: deis/mock-http-server
    container_name: web2
    expose:
      - "8080"
    environment:
      SERVICE_8080_NAME: "web"
      SERVICE_8080_TAGS: "web"
    restart: always
    depends_on:
      - registrator
  haproxy:
    build: ./haproxy
    container_name: my-haproxy
    image: anthcourtney/haproxy-consul
    ports:
      - 80
    depends_on:
      - web1
      - web2
  test:
    container_name: test-client
    build: ./test
    depends_on:
      - haproxy

networks:
  default:
HyukHyukBoi
  • 433
  • 1
  • 7
  • 20

1 Answers1

2

You can use registrator for your service registry.
Registrator automatically registers and deregisters services for any Docker container by inspecting containers as they come online. Registrator supports pluggable service registries, which currently includes Consul, etcd and SkyDNS 2.
You can run registrator as a container.It will register each port of your application. Below is the sample compose file :-

  version: '2'
  services:
  registrator:
    image: "${REGISTRY}gliderlabs/registrator:latest"
    command: [
      "-ip=<docker-host-ip>",
      "-retry-attempts", "100",
      "-cleanup",
      # "-internal",
      "consul://vconsul:8500"
    ]

official documentation : https://gliderlabs.github.io/registrator/latest/

Munish
  • 1,377
  • 9
  • 16
  • I have edited the question to include my docker-compose.yml file. In this, I plan to replace the mock http servers with my 2 spring boot applications. So, you are telling me that I just have to change the images (include builds) and the registrator will automatically register them to consul as the containers come online (are made). No need of any extra config in application.properties, just specifying the port on which it will run will suffice. – HyukHyukBoi Feb 03 '20 at 07:52
  • you just have to add registrator service. It will register all the other services given in your compose file. it will do registration using service name and service port. so multiple services can have same images. you don't have to do anything extra, just add registrator in your compose file. – Munish Feb 03 '20 at 08:00
  • welcome. please give a up vote if you found this helpful :) – Munish Feb 03 '20 at 08:10
  • Also, on their official site, registrator usage is given as: Registrator watches for new Docker containers and inspects them to determine what services they provide. For our purposes, a service is anything listening on a port. Any services Registrator finds on a container, they will be added to a service registry, such as Consul or etcd. So, do we have to add annotation @EnableDiscoveryClient which adds the service to service discovery or all services are automatically added? – HyukHyukBoi Feb 03 '20 at 09:34
  • all the services will be automatically added as registrator listen docker events. – Munish Feb 03 '20 at 10:45