2018-08-30 14:23:50.0|分类: docker+node.js+zookeeper构建微服务|浏览量: 2383
node.js搭建服务网关,请求到来根据规则获取到服务名字,然后请求zookeeper服务注册中心服务列表。然后将请求转发到服务实例。 node项目结构 node首页index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Demo</title> </head> <body> <div id="console"></div> <script src="js/jquery.min.js"></script> <script> $(function () { $.ajax({ method: 'GET', url: '/hello', headers: { 'Service-Name': 'helloServce' }, success: function (data) { $('#console').text(data); } }); }); </script> </body> </html> package.json内容 { "name": "node.js-web", "version": "0.0.1", "dependencies": { "express": "^4.16.3", "http-proxy": "^1.17.0", "node-zookeeper-client": "^0.2.2" } } app.js var express = require('express'); var zookeeper = require('node-zookeeper-client'); var httpProxy = require('http-proxy'); var port = 1234; //zookeeper服务地址 var CONNECT_URL = "127.0.0.1:2181"; var REGISTRY_ROOT = "/registry"; //创建zookeeper连接 var zk = zookeeper.createClient(CONNECT_URL); zk.connect(); //创建http代理 var proxy = httpProxy.createProxyServer(); proxy.on('error', function (err, req, res){ res.end(); return ; }); //web var app = express(); app.use(express.static('public')); app.all('*', function (req, res){ if(req.path=='/favison.ico'){ res.end(); return ; }
//获取服务名称,服务名称是zookeeper节点 var serverName = req.get('Service-Name'); console.log('Service-name is : '+serverName);
if(!serverName){ console.log('Service-name is not exist '); res.end(); return ; }
//组装服务节点路径 /registry/helloServce var servicePath = REGISTRY_ROOT +"/"+serverName; console.log('Service path is '+servicePath);
//获取服务节点下面所有的节点,也就是服务实例列表 zk.getChildren(servicePath, function(error, addressNodes){ if(error){ console.log(error.stack); res.end(); return ; }
var length = addressNodes.length; if(length==0){ console.log('address node is zero '); res.end(); return ; }else{ console.log('address node length is '+length); }
//如果只有一个服务实例,直接使用,如果超过1个,则随机选择 var addressPah = servicePath + "/"; if(length==1){ addressPah += addressNodes[0]; }else{ addressPah += addressNodes[parseInt(Math.random()*length)]; } console.log('addressPah is '+addressPah);
//获取服务实例的IP+端口号 zk.getData(addressPah, function(error, serviceAddress){ if(error){ console.log(error.stack); res.end(); return ; }
console.log('serviceAddress is '+serviceAddress); if(!serviceAddress){ console.log('serviceAddress is not exist '); res.end(); return ; } //转发请求 proxy.web(req, res,{ target:'http://'+serviceAddress });
});
});
}); app.listen(port, function (){ console.log('server is running by ' + port); }); 运行app.js eclipse打印信息: server is running by 1234 浏览器请求localhost:1234 eclipse打印信息,明显发现访问了不同服务实例 server is running by 1234 Service-name is : helloServce Service path is /registry/helloServce Service-name is : undefined Service-name is not exist address node length is 2 addressPah is /registry/helloServce/address-0000000010 serviceAddress is 127.0.0.1:8080 Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000010 serviceAddress is 127.0.0.1:8080 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000011 serviceAddress is 127.0.0.1:8081 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000010 serviceAddress is 127.0.0.1:8080 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000011 serviceAddress is 127.0.0.1:8081 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000010 serviceAddress is 127.0.0.1:8080 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000011 serviceAddress is 127.0.0.1:8081 Service-name is : undefined Service-name is not exist Service-name is : helloServce Service path is /registry/helloServce address node length is 2 addressPah is /registry/helloServce/address-0000000010 serviceAddress is 127.0.0.1:8080 Service-name is : undefined Service-name is not exist |