Authored by 陈峰

commit

... ... @@ -50,8 +50,9 @@ app.use(convert(body({
})));
app.use(mount('/', webApp));
require('./middleware/zookeeper')(app);
// SSR自适应降级监控
// require('./apps/tasks/degrade-ssr-task')();
require('./apps/tasks/degrade-ssr-task')(app);
server.listen(port, () => {
console.log(`app started in ${port}`);
... ...
... ... @@ -166,6 +166,18 @@ const defaultDegrades = [
path: '/wap/open/apmrisk',
name: '【开关】启用 apm 风控中间件'
},
{
path: '/wap/webapp/yoho-community-web-yoho-protocol',
name: '打开yoho-protocol'
},
{
path: '/wap/webapp/ios-async-js',
name: '打开ios async js'
},
{
path: '/wap/open/webperf',
name: '打开前端性能上报'
},
];
class Degrade extends Model {
... ...
const schedule = require('node-schedule');
const _ = require('lodash');
const ZookeeperModel = require('../web/models/zookeeperModel');
const Operation = require('../logger/operation');
const _ = require('lodash');
const {DegradeServer} = require('../models');
const setter = require('../zookeeper/setter');
const {client} = require('../../lib/redis');
module.exports = () => {
let zookeeperModel = new ZookeeperModel();
module.exports = (koaApp) => {
schedule.scheduleJob('* * * * * *', async () => {
console.log('scheduleJob')
const appsData = await client.getAsync('degradeSSRKeys');
const apps = JSON.parse(appsData || '[]');
let server = await DegradeServer.findOne({
type: 'qcloud'
});
_.forEach(apps, async app => {
try {
const zkDegradePath = `/wap/webapp/${app}-degrade`;
const degradeKey = `${app}:degradessr`;
const isDegradeKey = `${degradeKey}:isdegrade`;
const [degradeJson, isDegrade] = await Promise.all([client.getAsync(degradeKey), client.getAsync(isDegradeKey)]);
const zkIsDegrade = await zookeeperModel.getPath(zkDegradePath)
const zkIsDegrade = _.get(koaApp, `locals.wap.webapp.${app}-degrade`, false);
const degrades = JSON.parse(degradeJson || '[]');
if (!isDegrade && zkIsDegrade === 'true') { // isdegrade的redis key已过期,且没有触发限制刷新key过期时间,则降级开关关闭
zookeeperModel.setPath(zkDegradePath, false);
if (!isDegrade && zkIsDegrade) { // isdegrade的redis key已过期,且没有触发限制刷新key过期时间,则降级开关关闭
setter(`${server.ip}:${server.port}`, zkDegradePath, 'false');
console.log('SSR降级恢复')
Operation.action({
_id: 0,
... ... @@ -50,12 +53,17 @@ module.exports = () => {
}, 'SSR降级', '触发降级策略' ,`${isDegradeKey},${JSON.stringify(degradeData)}`);
client.setexAsync(isDegradeKey, item.delay * 60, JSON.stringify(degradeData));
client.delAsync(key);
zookeeperModel.setPath(zkDegradePath, true);
setter(`${server.ip}:${server.port}`, zkDegradePath, 'true');
}
});
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 1000);
});
} catch (error) {
console.log(error);
}
})
});
});
}
\ No newline at end of file
... ...
... ... @@ -12,6 +12,7 @@ const defaults = {
host: '172.31.26.70',
port: 4444,
},
zookeeperServer: '127.0.0.1:2181',
redis: {
connect: {
host: '127.0.0.1', //'127.0.0.1',
... ... @@ -72,6 +73,7 @@ const specific = {
development: {},
test: {},
production: {
zookeeperServer: 'web.zookeeper.yohoops.org:2181',
apm: {
aws: {
host: '172.31.26.70',
... ...
const config = require('../config/config');
module.exports = (app) => {
app.locals = {};
if (config.zookeeperServer) {
require('yoho-zookeeper')(config.zookeeperServer, 'wap', app.locals.wap = {}, false);
}
}
\ No newline at end of file
... ...
... ... @@ -74,7 +74,8 @@
"superagent": "^3.6.0",
"tar": "^2.2.1",
"utility": "^1.8.0",
"xlsx": "^0.13.0"
"xlsx": "^0.13.0",
"yoho-zookeeper": "^1.0.11"
},
"devDependencies": {
"ada": "^1.1.0",
... ...