简介
大家好, 我正在尝试从一个使用Express编写的nodejs前端应用程序通过Consul DNS接口调用nodejs后端微服务,但是我遇到了错误。
我使用nodejs dns api来为唯一的节点应用程序设置dns,以便使用本地Consul DNS接口进行后续dns.resolve()调用。
目标
我希望能够在不需要在客户端代码中连接IP和端口的情况下向后端服务发出http请求。我也不想编写自定义代码来查询Consul HTTP API以获取每次需要调用服务时的ip:port对。
问题
问题在于,当我使用axios(类似于request)向后端服务发出HTTP调用时,我总是会收到错误,因为它无法解析地址。似乎Axios没有使用我之前设置的dns:
dns.setServers(['127.0.0.1:8600']);
更新_1
将虚拟机的DNS(/etc/resolv.conf)设置为localhost,并使用-consul命令行选项的-recursor参数和默认DNS一起,一切正常!但我仍然希望了解仅在我的nodejs应用程序上设置DNS时出了什么问题。
安装
- 1个前端节点,运行一个使用Expressjs实现简单Web服务器的nodejs进程。在app.get('/')路由中,它通过consul和axios(类似于request)向名为be01的后端服务进行REST POST调用。
- 2个后端节点,每个节点有一个使用Expressjs实现简单Web服务器的nodejs进程,暴露REST API。
- 1个Consul节点,其中安装了Consul作为服务器。
- 每个节点都有其自己的consul agent运行并加入到集群中。
- TCP端口已正确打开。
在FE节点上运行consul members命令,得到以下结果:
agent-server 10.0.1.7:8301 alive server 1.0.1 2 dc1 <all>
agent-be-01 10.0.1.5:8301 alive client 1.0.1 2 dc1 <default>
agent-be-02 10.0.1.6:8301 alive client 1.0.1 2 dc1 <default>
agent-fe 10.0.1.4:8301 alive client 1.0.1 2 dc1 <default>
如果我在前端节点运行 dig @localhost -p 8600 be01.service.consul SRV,我将正确获得以下结果(与 Consul 文档中的结果相同):
root@NGINXLB:~/node8080$ dig @localhost -p 8600 be01.service.consul SRV
; <<>> DiG 9.9.5-3ubuntu0.16-Ubuntu <<>> @localhost -p 8600 be01.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43367
;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 5
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;be01.service.consul. IN SRV
;; ANSWER SECTION:
be01.service.consul. 0 IN SRV 1 1 8081 agent-be-02.node.dc1.consul.
be01.service.consul. 0 IN SRV 1 1 8080 agent-be-01.node.dc1.consul.
;; ADDITIONAL SECTION:
agent-be-02.node.dc1.consul. 0 IN A 10.0.1.6
agent-be-02.node.dc1.consul. 0 IN TXT "consul-network-segment="
agent-be-01.node.dc1.consul. 0 IN A 10.0.1.5
agent-be-01.node.dc1.consul. 0 IN TXT "consul-network-segment="
;; Query time: 2 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Fri Dec 01 10:09:00 UTC 2017
;; MSG SIZE rcvd: 246
这是BE服务的代码:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/api/getUserTiles', jsonParser, function (req, res) {
console.log("> API request for '/api/getUserTiles'");
if (!req.body) { return res.sendStatus(400) }
else {
var user = req.body.user;
console.log("User: " + user);
res.json({
"authorizedTiles": [
{"tileID": "profile"}
,{"tileID": "search"}
,{"tileID": "test"}
],
"email": "max@google.com",
"userName":"Max",
"has_error": false,
"error_message": ""
});
}
});
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
通过curl使用其IP:端口从前端调用它没有任何问题:
root@NGINXLB:~/node8080$ curl -d="user=Max" -X POST http://10.0.1.5:8080/api/getUserTiles
{"authorizedTiles":[{"tileID":"profile"},{"tileID":"search"},{"tileID":"test"}],"email":"max@google.com","userName":"Max","has_error":false,"error_message":""}
FE节点上的网络服务,简单地说,就是这样的:
var axios = require('axios');
var dns = require('dns');
var consul = require('consul')();
dns.setServers(['127.0.0.1:8600']);
//console.log(dns.getServers());
dns.resolveSrv("be01.service.consul", function(err, records){
console.log("\nDNS SRV query");
if(err){
console.log(err);
}else{
console.log(records);
}
});
consul.health.service({
"service": "be01",
"dc": "dc1",
"passing": true
}, function(err, result){
console.log("\nConsul.health.service query:");
if(err){console.log(err); throw err;}
else if(result.length > 0){
for(var i=0; i<result.length; i++){
console.log(result[i].Node.Address + ":" + result[i].Service.Port);
}
}
});
axios.post("http://be01.service.consul/api/getUserTiles", {'user':'Max'})
.then(function(response){
if (response.data.has_error) {
console.log("\nRESPONSE HAS ERROR!");
}else {
console.log("\nSUCCESS!");
}
})
.catch(function(err) {
console.log("\nERROR CALLING THE BE SERVICE");
});
运行它后,我得到以下结果:
root@NGINXLB:~/node8080$ node app.js
DNS SRV query
[ { name: 'agent-be-01.node.dc1.consul',
port: 8080,
priority: 1,
weight: 1 },
{ name: 'agent-be-02.node.dc1.consul',
port: 8081,
priority: 1,
weight: 1 } ]
Consul.health.service query:
10.0.1.5:8080
10.0.1.6:8081
ERROR CALLING THE BE SERVICE
{ Error: getaddrinfo ENOTFOUND be01.service.consul be01.service.consul:80
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'be01.service.consul',
host: 'be01.service.consul',
port: 80,
...
...
结论
从结果可以看出,Node.js客户端正在调用be服务,但在尝试解析“be01.service.consul”时失败。此外,它使用的端口是80而不是Consul DNS接口提供的8080或8081端口。我错过了什么吗?