简体   繁体   English

mysql连接池如何与Node微服务一起使用?

[英]How does mysql connection pooling works with Node microservices?

I have two node microservices talking to one common mysql database. 我有两个节点微服务与一个常见的mysql数据库通信。 Both microservices have this below code to create a connection pool with a connectionlimit of 10 as follows: 这两个微服务都具有以下代码来创建连接限制为10的连接池,如下所示:

// Initializing pool //初始化池

var pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'root'
});

function addConnection(req, res) {
  pool.getConnection(function (err, connection) {
    if (err) {
      connection.release();
      res.json({ "code": 500, "status": "Error" });
      return;
    }

    connection.query("select * from user", function (err, rows) {
      connection.release();
      if (!err) {
        res.json(rows);
      }
    });

    connection.on('error', function (err) {
      res.json({ "code": 500, "status": "Error" });
      return;
    });
  });
}

For mysql database I have the max_connections set to 200(SHOW VARIABLES LIKE 'max_connections'; returns 200). 对于mysql数据库,我将max_connections设置为200(类似于SHOW VARIABLES LIKE'max_connections';返回200)。

  1. With the pool connectionLimit set to 10 for each of the microservice, in which cases or scenarios will the number of connections for any of the microservice will go above 10? 将每个微服务的pool connectionLimit设置为10时,在任何情况下或情况下,任何微服务的连接数都将超过10? ie When and how the node services would be able to maintain more connections then expected? 即,节点服务何时以及如何能够维持比预期更多的连接?
  2. If I have 3 instances running of same microservice then how does the pool connectionLimit works? 如果我有3个实例在同一微服务上运行,那么池connectionLimit如何工作? What would be the limit of connections for each instance of microservice? 每个微服务实例的连接限制是多少?
  3. In one of the microservice say I have two apis which does database transactions and both connects to the database(getting connection) through two different functions having same implementation of mysql.createPool({}) as above. 用一个微服务说,我有两个API,它们执行数据库事务,并且都通过两个不同的函数连接到数据库(获取连接),这些函数具有与mysql.createPool({})相同的实现。 What would happen if both apis are called concurrently and the number of requests made for each of them per second is 100 or more? 如果同时调用两个api,并且每个api每秒发出的请求数为100或更多,将会发生什么情况? Will the number of connections made available be 10 or 20(since there are two mysql pools created with a connectionLimit of 10 each)? 可用的连接数是10还是20(因为创建了两个mysql池,每个connectionLimit数量为10)?
  1. Ideally it would not; 理想情况下不会; but it can go above 10; 但可以超过10; if suppose some connections become stale ie they are closed from client end but are still open on Server end. 如果某些连接失效,即它们从客户端关闭,但在服务器端仍然打开。

  2. If you have multiple instances of same micro-service deployed in multiple VM or docker containers; 如果您在多个VM或Docker容器中部署了同一微服务的多个实例; they are like independent services.. and they have no connection among each other.. Hence, each of them will create its own 10 connection. 它们就像独立的服务。彼此之间没有连接。因此,它们每个都将创建自己的10个连接。

  3. Firstly if u set connection pool limit as 10; 首先,如果您将连接池限制设置为10; that does NOT mean that during first moment 10 connections would be created.. While creating a pool; 这并不意味着在第一时间将创建10个连接。 you also specify initial connection parameter suppose 5.. so, when service starts only 5 connections would be created.. and more created only when needed.. with UPPER Limit set as defined by parameter max_connections. 您还要指定初始连接参数,假设为5 ..因此,服务启动时将仅创建5个连接,并且仅在需要时才创建更多连接,且UPPER Limit由参数max_connections定义。 Coming back to your question; 回到您的问题; well if you have NOT implemented synchronization etc. properly then yes it is possible that both pools will initialize their INITIAL_CONNECTIONS.. 如果您尚未正确实现同步等,那么可以,两个池都可能会初始化其INITIAL_CONNECTIONS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM