seneca - communication between two microservices

356 views Asked by At

I'm new in Seneca. I have been trying to make two microservices to communicate each other but I keep failing and get this errors:

Error: Response Error: 404 Not Found at module.exports.internals.Utils.internals.Utils.handle_response (c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\seneca-transport\lib\transport-utils.js:71:11) at c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\seneca-transport\lib\http.js:154:25 at read (c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\wreck\lib\index.js:590:24) at finish (c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\wreck\lib\index.js:398:20) at wrapped (c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\hoek\lib\index.js:879:20) at module.exports.internals.Recorder.onReaderFinish (c:\Users\Actiview\Desktop\microservices\orderManager\node_modules\wreck\lib\index.js:449:16) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:111:20) at module.exports.internals.Recorder.emit (events.js:208:7) at finishMaybe (_stream_writable.js:614:14)

=== SENECA FATAL ERROR === MESSAGE: ::: seneca: Action failed: Response Error: 404 Not Found. CODE: ::: act_execute INSTANCE ::: Seneca/pcbyi7v5c76v/1534346071465/6536/3.7.0/- DETAILS ::: { message: 'Response Error: 404 Not Found', pattern: '', fn: { [Function: transport_client] id: 'host:127.0.0.2,pg:,port:8080' }, callback: { [Function: bound action_reply] seneca: Seneca { 'private$': { act: { parent: { start: 1534346071559, end: 1534346071561, and more...

this is my code:

orderIndex.ts

    {
    const orderPlugin = require('./orderManagerPlugin');
    const  express = require('express');
    const SenecaWeb = require('seneca-web');
    const seneca = require("seneca")();
    let bodyParser = require('body-parser');



    var Routes = [{
        prefix: '/orders',
        pin: 'area:order,action:*',
        map: {
            fetch: { GET: true },
            create: { GET: false, POST: true },
            delete: { GET: false, DELETE: true },
        }
    }]

    var config = {
        routes: Routes,
        adapter: require('seneca-web-adapter-express'),
        context: express().use(bodyParser.urlencoded({ 'extended': 'true' })).use(bodyParser.json()),
        options: {parseBody: false}
    }

    seneca.use(SenecaWeb,config);
    seneca.use(  orderPlugin  );


    seneca.ready(function (err) {
        const app = seneca.export('web/context')();
        app.listen({ host: "127.0.0.4", port: 8081 });
    });
    }

orderPlugin.ts

{
var plugin = function orderPlugin(options) {
    var seneca = this;
    var senecaEmailer;

    seneca.add({ area: "order", action: "fetch" }, function (args,
        done) {
        var orders = this.make("orders");
        orders.list$({ id: args.id }, done);
    });

    seneca.add({ area: "order", action: "delete" }, function (args,
        done) {
        var orders = this.make("orders");
        orders.remove$({ id: args.id }, function (err) {
            done(err, null);
        });
    });

    seneca.add({ area: "order", action: "create" }, function (args,
        done) {
        console.log('create order');
       senecaEmailer.act( 'role:web', {area: 'email', action:'send'}   , done);

    });

    this.add( { init: "orderPlugin" }, function (args, done) {  
        senecaEmailer = require("seneca")().client({ host: "127.0.0.2", port: 8080 });
        done();
    });
}

module.exports = plugin;
}

emailIndex.ts

{

const mailPlugin = require('./emailingPlugin');
const  express = require('express');
const SenecaWeb = require('seneca-web');
const seneca = require("seneca")();
let bodyParser = require('body-parser');

var Routes = [{
    prefix: '/emails',
    pin: 'area:email, action:*',
    map: {
        send: { GET: true },
    }
}]

var config = {
    routes: Routes,
    adapter: require('seneca-web-adapter-express'),
    context: express().use(bodyParser.urlencoded({ 'extended': 'true' })).use(bodyParser.json()),
    options: {parseBody: false}
}

seneca.use(SenecaWeb,config);
seneca.use(  mailPlugin  );


seneca.ready(function (err) {
    const app = seneca.export('web/context')();
    app.listen({ host: "127.0.0.2", port: 8080 } );
});
}

emailPlugin.ts

{

import {EmailService} from './emailService';
var plugin = function emailPlugin(options) {
    var seneca = this;
    let mailer :EmailService ;


    seneca.add({area: "email", action: "send"}, function(args, done) {
        mailer.sendMail('[email protected]', done);
    });

    this.add( { init: "emailPlugin" }, function (args, done) {  
        console.log('before init');
        mailer = require('./emailService')();
        console.log('after init');
        done();
    });
};

   module.exports = plugin;
}

please help me. Tnx.

1

There are 1 answers

0
jack-y On

Seneca is explained by Richard Rodger in this post. The chapter "Service Discovery" talks about meshing the microservices in a network.

For my applications I use the seneca-mesh plugin. This plugin README says:

To join the network, all a service has to do is contact one other service already in the network. The network then shares information about which services respond to which patterns. There is no need to configure the location of individual services anywhere.

Reading Richard's post and the plugin documentation could be a good starting point for your project. Hope it helps!