2、项目目录下的index.js
/*
* index.js
* /
const startCluster = require('egg-cluster').startCluster;
const Etcd3 = require('etcd3');
const fs = require('fs');
const moment = require('moment');
// node env 与 egg env 映射
const env = "dev";
//egg-cluster启动配置
const serverOptions = {
baseDir: __dirname,
};
// etcd配置
const etcdConfig = {};
(async () => {
const etcd = new Etcd3.Etcd3(etcdConfig);
try {
const res = await etcd.getAll();
//获取到etcd的配置后,写入文件
fs.writeFileSync("./config/etcd.json", JSON.stringify(res), { flag: 'w' })
} catch (error) {
const errMessage = moment().format('YYYY-MM-DD HH:mm:ss')
+ 'There is an error occured for getting config from Etcd =>'
+ error.toString()
fs.writeFileSync('./logs/startError.log', errMessage, { flag: 'w' })
process.exit(1)
}
startCluster(serverOptions);
})();
3、写入config目录下的配置文件etcd.json
/*
* etcd.json
* /
{
"MYSQL_DBNAME": "test",
"MYSQL_PORT": "3306",
"MYSQL_READ_HOST": "127.0.0.1",
"MYSQL_READ_PASSWORD": "test",
"MYSQL_READ_USERNAME": "test"
}
4、在config中使用配置文件
/*
* config.env.js
* /
*
const etcd = require('./etcd.json');
const MYSQL_DBNAME = etcd.MYSQL_DBNAME
const MYSQL_HOST_READ = etcd.MYSQL_READ_HOST
const MYSQL_PORT_READ = etcd.KANKANTAOBAO_MYSQL_PORT
const MYSQL_USERNAME_READ = etcd.MYSQL_READ_USERNAME
const MYSQL_PASSWORD_READ = etcd.MYSQL_READ_PASSWORD
module.exports = appInfo => {
return {
sequelize: {
datasources: [
{
dialect: 'mysql',
delegate: 'test',
baseDir: 'model/test',
database: MYSQL_TAOBAO_DBNAME,
logQueryParameters: true,
replication: {
read: [
{
database: MYSQL_DBNAME,
host: MYSQL_HOST_READ,
port: MYSQL_PORT_READ,
username: MYSQL_USERNAME_READ,
password: MYSQL_PASSWORD_READ,
}
],
write: {
}
},
freezeTableName: false,
underscored: true,
timezone: '+08:00',
}
]
},
}
};
5、关于此种方式的思考 在我们项目的实际部署使用中,测试服使用pm2统一维护node项目,在正式服中,用docker部署项目,所以几乎是用不到egg-script的维护功能,如果大家有什么更好的方式,或者我的错误,欢迎指点。
跟进- [ ] some task
- [ ] PR URL