zoukankan      html  css  js  c++  java
  • eggjs中egg-mysql不支持mysql集群,代码修改为支持集群

    说明:暂不支持egg-mysql动态数据源,用到动态数据源请自行修改。欢迎各位大佬指导。。。

       集群配置: 

    exports.mysql = {
        // 单数据库信息配置
        client: {
            db1: {
                // host
                host: 'ip1',
                // 端口号
                port: '3306',
                // 用户名
                user: 'root',
                // 密码
                password: '123456',
                // 数据库名
                database: 'mydatebase',
            },
            db2: {
                // host
                host: 'ip2',
                // 端口号
                port: '3306',
                // 用户名
                user: 'root',
                // 密码
                password: '123456',
                // 数据库名
                database: 'mydatebase',
            },
            db3: {
                // host
                host: 'ip3',
                // 端口号
                port: '3306',
                // 用户名
                user: 'root',
                // 密码
                password: '123456',
                // 数据库名
                database: 'mydatebase',
            }
        }
    
    };

    单数据库配置:

    exports.mysql = {
      // 单数据库信息配置
     client: {
        // host
        host: 'ip1',
        // 端口号
        port: '3306',
        // 用户名
        user: 'root',
        // 密码
        password: '123456',
        // 数据库名
        database: 'mydatabase',
      },
      // 是否加载到 app 上,默认开启
      app: true,
      // 是否加载到 agent 上,默认关闭
      agent: false,
    
    };
    

      

    1.找到node_modules/ali-rds/lib/client.js,

    RDSClient修改如下:
     
    function RDSClient(options) {
      if (!(this instanceof RDSClient)) {
        return new RDSClient(options);
      }
      Operator.call(this);
      let configObj = JSON.stringify(options);
      let len = configObj.match(/{/g);
      if (len.length > 1) {
        delete options.connectionLimit;
        delete options.database;
        let poolCluster = mysql.createPoolCluster({
          removeNodeErrorCount: 1,
          defaultSelector: "RR" //RR,RANDOM,ORDER
        });
        for (let node in options) {
          poolCluster.add(`${node}`, options[`${node}`]);
        }
        this.pool = poolCluster.of('*', 'RR');
        console.log('<<<<<<----mysql createPoolCluster---->>>>>>>', options, len)
      } else {
        this.pool = mysql.createPool(options);
        console.log('<<<<<<-----mysql createPool--->>>>>>>', options, len)
      }
      [
        'query',
        'getConnection',
      ].forEach(method => {
        this.pool[method] = promisify(this.pool[method]);
      });
    }
    

     2./node_modules/egg-mysql/lib/mysql.js,修改如下

       

    'use strict';
    
    const assert = require('assert');
    const rds = require('ali-rds');
    
    let count = 0;
    
    module.exports = app => {
      app.addSingleton('mysql', createOneClient);
    };
    
    function createOneClient(config, app) {
      const client = rds(config);
      app.beforeStart(function* () {
        const rows = yield client.query('select now() as currentTime;');
        const index = count++;
        app.coreLogger.info(`[egg-mysql] instance[${index}] status OK, rds currentTime: ${rows[0].currentTime}`);
      });
      return client;
    }

  • 相关阅读:
    Samba 4.0 RC3 发布
    SymmetricDS 3.1.7 发布,数据同步和复制
    Express.js 3.0 发布,Node.js 的高性能封装
    GIFLIB 5.0.1 发布,C语言的GIF处理库
    jQuery UI 1.9.1 发布
    SVN Access Manager 0.5.5.14 发布 SVN 管理工具
    DynamicReports 3.0.3 发布 Java 报表工具
    HttpComponents HttpClient 4.2.2 GA 发布
    AppCan 2.0 正式发布,推移动应用云服务
    Ruby 2.0 的新功能已经冻结
  • 原文地址:https://www.cnblogs.com/qiyc/p/12966913.html
Copyright © 2011-2022 走看看