只有生产环境才使用服务自发现功能

This commit is contained in:
zhl 2021-01-30 12:52:56 +08:00
parent 7d07e29720
commit c074e3bf19
2 changed files with 11 additions and 6 deletions

View File

@ -19,7 +19,7 @@ const app = express()
const port = Number(process.env.PORT || 2500);
let config: Config = require('../config/config.json');
const isProd = process.env.NODE_ENV === 'production'
initData();
app.use(cors());
app.use(express.json());
@ -37,7 +37,9 @@ let opts = {url: config.redis}
new RedisClient(opts)
app.listen(port, async function () {
await registerGracefulShutdown((err) => gracefullyShutdown(true, err));
await registService(port)
if (isProd) {
await registerGracefulShutdown((err) => gracefullyShutdown(true, err));
await registService(port)
}
console.log(`App is listening on port ${port}!`);
});

View File

@ -4,6 +4,7 @@ import { error, sysLog } from '../common/Debug'
import { Config } from '../cfg/Config'
import ip from 'internal-ip'
const config: Config = require('../../config/config.json')
const isProd = process.env.NODE_ENV === 'production'
@singleton
export class Service {
/**
@ -35,7 +36,9 @@ export class Service {
constructor() {
this.serviceMap.set(Service.INFO_NODE, [])
this.serviceMap.set(Service.ROBOT_NODE, [])
this.discoveryServices()
if (isProd) {
this.discoveryServices()
}
}
public discoveryServices() {
@ -74,7 +77,7 @@ export class Service {
this.serviceMap.set(key, svrList)
}
let svrList = this.serviceMap.get(key)
if (svrList.length == 0) {
if (svrList.length == 0 || !isProd) {
error('no info service found')
return config.info_svr
}
@ -88,7 +91,7 @@ export class Service {
this.serviceMap.set(key, svrList)
}
let svrList = this.serviceMap.get(key)
if (svrList.length == 0) {
if (svrList.length == 0 || !isProd) {
error('no robot service found')
return 'http://127.0.0.1:2500'
}