Http request using Consul DNS in Docker environment

node.js docker consul

276 观看

1回复

118 作者的声誉

I have a very simple consul and registrator compose runnning on my machine. I can query the list of services registered in consul but I can't make a call to other service using http.

docker-compose :

version: '2.1'

services:

  consul:
    image:  gliderlabs/consul:latest
    command: "agent -server -data-dir /tmp/consul -bootstrap -ui -client=0.0.0.0"
    ports:
    - 8500:8500
    - 8600:8600
    - 8400:8400
    networks:
      app_net:
        ipv4_address: 172.28.0.10

  registrator:
    image: gliderlabs/registrator:latest
    command: "consul://consul:8500"
    depends_on:
    - consul
    volumes:
    - /var/run/docker.sock:/tmp/docker.sock
    networks:
      - app_net

  service1:
    image: solive/service1
    ports:
    - 3000
    depends_on:
    - consul
    - registrator
    networks:
      - app_net

  service2:
    image: solive/service2
    ports:
    - 4000
    depends_on:
    - consul
    - registrator
    networks:
      - app_net

networks:
  app_net:
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16

I tried to use axios but it sounds like it doesn't use the configured DNS. Once again I've managed to have the list of my services. So I can register a service and I can fetch services using consul.agent, DNS resolve works too. But the get call doesn't work. I get ENOTFOUND

const request = require('request');
var dns = require('dns');
var consul = require('consul')(
  {
    host: '172.28.0.10',
    port: '8500'
  }
);

dns.setServers(['172.28.0.10:8600']);

dns.resolveSrv("service1.service.consul", function(err, records){
        console.log("\nDNS SRV query");
        if(err){
                console.log(err);
        }else{
                console.log(records);
        }
});

request('http://service1.service.consul', { json: true }, (err, res, body) => {
  if (err) { return console.log(err); }
  console.log(body.url);
  console.log(body.explanation);
});
作者: Stephane Karagulmez 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

10414 作者的声誉

You haven't mentioned what are the ip addresses of the services you're trying to access that are registered in Consul. I suspect that registrator doesn't register the ips you're expecting in the app_net network and that's why it's not working for you. if that's the case either check in registrator's docs how to manipulate registered ip addresses or use another method to register the services to Consul.

作者: Gal Ben-Haim 发布者: 2017 年 12 月 29 日
32x32