CoteJS 创建多个服务不起作用

CoteJS creating multiple services does not work

AM 使用 cote.js 创建多个服务,我在自己的文件中有大约 40 个服务。所以问题是,如果我启动其中两个,它们可以正常工作,但如果我启动第三个或全部,它们就不起作用,包括之前工作的两个,控制台输出的屏幕截图:

这是应该的样子(启动两个服务时)

但这就是我得到的(当启动了两个以上时):

request journey begins 是应用程序第一个中间件中的第一行代码

sending...requester.send 函数被调用之前的输出

recieved: nullrequester.send函数回调中第一行代码执行的输出,null是回调的error参数。

我的代码是站点文档页面中的 usual/normal requester/responder,只是其中一项服务来自应用程序,该应用程序还具有 express listen 功能(ExpressJS应用程序)。

任何帮助将不胜感激,提前致谢。

我的请求者:

const requester = new Requester({
    name: 'app-main',
    key: process.env.APP_NAME
}, {
    redis: {
        host: process.env.REDIS_HOST,
        port: process.env.REDIS_PORT
    }
} as any);

app.set('requester', requester);

我基本上在整个申请过程中都使用这个请求者

我的回复者:

import {config} from 'dotenv';
import {basename, resolve} from 'path';
import {Responder} from 'cote';

import * as moment from 'moment';

import {ServiceCallback, ServiceRequest, LoggingParam} from '../helpers/types';
import logger from '../logger';

config({
    path: resolve(__dirname, '..', '.env')
});

const responder = new Responder({
    name: basename(__filename).substring(0, basename(__filename).indexOf('.service')),
    key: process.env.APP_NAME
}, {
    redis: {
        host: process.env.REDIS_HOST,
        port: Number(process.env.REDIS_PORT)
    }
} as any);

responder.on(basename(__filename).substring(basename(__filename).indexOf('.') + 1, basename(__filename).indexOf('.service')), (request: ServiceRequest, callback: ServiceCallback) => {
    const logParam: LoggingParam = {
        level: 'info',
        load: {
            route: {
                userAgent: request.userAgent,
                ip: request.ip,
                body: request.requestBody,
                name: 'test'
            },
            content: {
                message: 'Test function accessed'
            }
        }
    };

    logger(logParam);

    callback(null, {
        status: 200,
        body: `
            <h1>API is up</h1>
            <p>Server time is ${moment().utcOffset(3).toDate()}</p>
        `
    });
});

我的路由声明示例:

router.post('/settings', (request: Request, response: Response) => requestHandler(request, response, 'settings'));

请求处理函数:

export function requestHandler(request: express.Request, response: express.Response, handle: string): void {
    const requester: Requester = request.app.get('requester');

    const requestObject: ServiceRequest = {
        type: handle,
        channel: request.body.channel,
        requestBody: request.body.data,
        user: request.body.user,
        userAgent: request.get('User-Agent'),
        ip: request.ip
    };

    requester.send(requestObject, (error: any, result: ServiceResponse) => {
        if (process.env.NODE_ENV === 'development' || !request.body.keyCombo) {
            response.status(result.status).send(result.body);

            return;
        }

        let dataToEncrypt = result.body;

        if (result.body.constructor === {}.constructor) {
            dataToEncrypt = JSON.stringify(result.body);
        }

        const credentials = request.body.keyCombo.split(';');

        const key = enc.Utf8.parse(credentials[0]);
        const iv = enc.Utf8.parse(credentials[1]);

        const toEncrypt = enc.Utf8.parse(dataToEncrypt);

        const encrypted = AES.encrypt(toEncrypt, key, { iv: iv }).toString();

        response.status(result.status).send(encrypted);
    });
}

如果您需要我发布更多代码,请尽管询问。

如果你没有解决这个问题,我只是 运行 遇到同样的问题,你必须使用键

Keys cote has another mechanism to create partitions called keys. Since every component discovers and tries to communicate with every other component on the horizon (this is called a "mesh network"), if different services request and respond to different types of messages, you will experience lost messages. In other words, if service A responds to messages X and Y, and service B responds to messages Z and T, you will lose half of the messages, because messages Z and T will also end up at service A, but it won't know how to handle them. The same is true for service B: messages X and Y will end up at it, but service B won't know how to respond to them.