HyukHyukBoi
HyukHyukBoi

Reputation: 463

Spring Cloud Consul and Consul Clients dockerized

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:

Upvotes: 0

Views: 617

Answers (1)

Munish
Munish

Reputation: 1627

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/

Upvotes: 2

Related Questions