0

I am using the request package from NPM to handle some internal communication between services. I have also set the DNS server to the correct one (using Hashicorp Consul as my SD and DNS).

I can do a dig on my local machine (where the services are running) to the consul DNS server and I am able to get back the correct response (an IP and port number.

How I setup DNS in my app.js file6

dns.setServers([ `${config.consul.host}:8600` ]);

Set in a different file than app.js

options = {
    baseUrl: `http://auth.service.consul`,
    json: { '': '' },
    headers: { authorization: '' }
};

Same file as options above

request.post(req.path, options, (error, response, body) => {
    console.log(error);
    if (error) throw error;
    res.status(response.statusCode).json(body);
});

Error message:

    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'auth.service.consul',
  host: 'auth.service.consul',
  port: 80 }

I am wanting to be able to make requests to the 'auth' service when using Consul as my DNS server. I currently have a very hacky way of doing this but really would like to use DNS.

I did find this but it is pertaining to the axios package not the request one I am trying to use even though it produces the same error the solution there didn't help. Consul service discovery with DNS on Nodejs

joshk132
  • 1,011
  • 12
  • 37
  • If `dig` works against IP address set in `${config.consul.host}:8600` then dns.js might not be calling this DNS server for some reason. From documenation https://nodejs.org/api/dns.html#dns_dns_setservers_servers `The dns.setServers() method affects only dns.resolve(), dns.resolve*() and dns.reverse() (and specifically not dns.lookup()).` I'm not very proficient in JavaScript, but try tracing down what DNS servers is your `request.post` trying to contact. – bagljas Jun 10 '19 at 08:10

1 Answers1

0

You might have a chicken and egg problem. Try resolving ${config.consul.host} into an IP address and then call dns.setServers with that IP address.

schtever
  • 3,210
  • 16
  • 25
  • I don't get what you mean, why would I set it to a variable when it is already one? That IP is a hard-coded value in my config file so it already has a value. Using template strings makes life easier since I don't have to concatenate everything into one variable. It just makes sense to use template strings instead of a new variable. – joshk132 Jun 09 '19 at 16:37
  • I assumed that `config.consul.host` was a hostname, not an IP Address. My mistake. You said that a `dig` returned you an IP and a port. What type of record did you request with `dig`? And what port was returned? – schtever Jun 10 '19 at 01:22