diff --git a/gm-server/app.ts b/gm-server/app.ts index 1a27c79de..c080533d5 100644 --- a/gm-server/app.ts +++ b/gm-server/app.ts @@ -1,7 +1,7 @@ import 'reflect-metadata' import * as mongoose from 'mongoose'; import { Application, IBoot } from 'egg'; -import { loadGmDb } from '@db/index'; +import { loadGmDb, loadSubDb } from '@db/index'; import { connectRedis } from '@pubUtils/redis'; export default class FooBoot implements IBoot { @@ -17,6 +17,7 @@ export default class FooBoot implements IBoot { // this is the last chance to modify the config. await this.connectDB(this.app) await this.connectGMDB(this.app); + await this.connectSubDB(this.app); await this.connectRedis(this.app); this.app.config.realEnv = this.app.config.env; @@ -85,6 +86,20 @@ export default class FooBoot implements IBoot { } } + public async connectSubDB(app: Application) { + const { url, options } = app.config.submongoose||app.config.mongoose + try { + if (url) { + const connection = await mongoose.createConnection(url, options) + app.context.connectionGM = connection; + loadSubDb(connection); + console.log('******connectSubDB suc', url, options) + } + } catch(e) { + console.log(e) + } + } + public async connectRedis(app: Application) { const { url, pw } = app.config.redis if (url) { diff --git a/gm-server/config/config.zy1.ts b/gm-server/config/config.zy1.ts index a6decf33e..7e89d4b86 100644 --- a/gm-server/config/config.zy1.ts +++ b/gm-server/config/config.zy1.ts @@ -16,6 +16,11 @@ export default (appInfo: EggAppInfo) => { options: { useNewUrlParser: true, useUnifiedTopology: true }, }; + config.submongoose = { + url: 'mongodb://yjzDbOp:yjz2023ZyDB@dds-8vbc6a169e1dd6941.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6942.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6943.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6944.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-516005504', // 内网 + options: { useNewUrlParser: true, useUnifiedTopology: true }, + }; + config.redis = { url: 'r-8vbkqfcnivt4ja3ndj.redis.zhangbei.rds.aliyuncs.com', // 内网 pw: 'hWCJ$HDajm9SN&&Z' diff --git a/web-server/app.ts b/web-server/app.ts index ac943cbda..d5393a644 100644 --- a/web-server/app.ts +++ b/web-server/app.ts @@ -2,7 +2,7 @@ import 'reflect-metadata' import * as mongoose from 'mongoose'; import { Application, IBoot } from 'egg'; import { connectRedis } from './app/pubUtils/redis'; -import { loadGmDb } from '@db/index'; +import { loadGmDb, loadSubDb } from '@db/index'; import { SDK_TA_CONST, THINKING_DATA_MODE, THINKING_DATA_MODE_LIST } from '@consts'; const ThinkingAnalytics = require("thinkingdata-node"); @@ -19,6 +19,7 @@ export default class FooBoot implements IBoot { // this is the last chance to modify the config. await this.connectDB(this.app); await this.connectGMDB(this.app); + await this.connectSubDB(this.app); await this.connectRedis(this.app); this.app.config.realEnv = this.app.config.env; @@ -89,6 +90,20 @@ export default class FooBoot implements IBoot { } } + public async connectSubDB(app: Application) { + const { url, options } = app.config.submongoose||app.config.mongoose + try { + if (url) { + const connection = await mongoose.createConnection(url, options) + app.context.connectionGM = connection; + loadSubDb(connection); + console.log('******connectSubDB suc', url, options) + } + } catch(e) { + console.log(e) + } + } + public async connectRedis(app: Application) { const { url, pw } = app.config.redis if (url) { diff --git a/web-server/config/config.zy1.ts b/web-server/config/config.zy1.ts index 79e547c8d..8b1cfd410 100644 --- a/web-server/config/config.zy1.ts +++ b/web-server/config/config.zy1.ts @@ -15,6 +15,11 @@ export default (appInfo: EggAppInfo) => { options: { useNewUrlParser: true, useUnifiedTopology: true }, }; + config.submongoose = { + url: 'mongodb://yjzDbOp:yjz2023ZyDB@dds-8vbc6a169e1dd6941.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6942.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6943.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbc6a169e1dd6944.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-516005504', // 内网 + options: { useNewUrlParser: true, useUnifiedTopology: true }, + }; + config.redis = { url: 'r-8vbkqfcnivt4ja3ndj.redis.zhangbei.rds.aliyuncs.com', // 内网 pw: 'hWCJ$HDajm9SN&&Z'