同时使用Node.js中的许多MySQL服务器的正确方法是什么? [英] What's the right way to work with many MySQL servers from Node.js simultaneously?

查看:30
本文介绍了同时使用Node.js中的许多MySQL服务器的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了移动社交网络.所有数据存储在MySQL中(通过分片).热数据存储在Redis中,Redis中也有碎片映射.

I develop mobile social network. All data stored in MySQL (via shards). Hot data stored in Redis, and there is shards map in Redis too.

后端= node.js(快速表达)

Backend = node.js (express)

在从移动客户端向后端的一个请求中,快速向不同的MySQL服务器快速请求的正确方法是什么?

What is the right way of fast requests to a different MySQL servers within one request to the back-end from mobile client?

示例:

客户端向服务器请求ID = 1的用户配置文件,我们将其存储在服务器mysql-1分片#1中.同时,另一个客户端向服务器请求ID = 2的用户配置文件,并将其存储在服务器mysql-2分片#2中.

The client requests the server to the user profile with id = 1, which we store on the server mysql-1 shard #1. At the same moment another client requests the server to the user profile with id = 2, which we store on the server mysql-2 shard #2.

MySQL服务器的数量将增加.并且对服务器的请求数量也将增加.在对后端的每个请求上初始化与MySQL的新连接-我认为这不是一个好主意.

Number of mysql servers will grow. And the number of requests to the server will grow too. Initialize a new connection to MySQL on each request to the backend - I think that isn't good idea.

推荐答案

欢迎@Dmitry,一个方法可以使用

Welcome @Dmitry, one aproach can be using a pool-cluster of connections.

此模块 https://github.com/felixge/node-mysql 可以提供帮助你.

This module https://github.com/felixge/node-mysql, can help you.

// create

var poolCluster = mysql.createPoolCluster();
poolCluster.add(config); // anonymous group
poolCluster.add('MASTER', masterConfig);
poolCluster.add('SLAVE1', slave1Config);
poolCluster.add('SLAVE2', slave2Config);

// Target Group : ALL(anonymous, MASTER, SLAVE1-2), Selector : round-robin(default)
poolCluster.getConnection(function (err, connection) {});

// Target Group : MASTER, Selector : round-robin
poolCluster.getConnection('MASTER', function (err, connection) {});

// Target Group : SLAVE1-2, Selector : order
// If can't connect to SLAVE1, return SLAVE2. (remove SLAVE1 in the cluster)
poolCluster.on('remove', function (nodeId) {
  console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1 
});

poolCluster.getConnection('SLAVE*', 'ORDER', function (err, connection) {});

// of namespace : of(pattern, selector)
poolCluster.of('*').getConnection(function (err, connection) {});

var pool = poolCluster.of('SLAVE*', 'RANDOM');
pool.getConnection(function (err, connection) {});
pool.getConnection(function (err, connection) {});

// destroy
poolCluster.end();

这篇关于同时使用Node.js中的许多MySQL服务器的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆