美文网首页
moleculer使用

moleculer使用

作者: 沈祥佑 | 来源:发表于2019-05-12 21:45 被阅读0次

    原文地址

    Installation

    $ npm install moleculer --save
    

    or

    $ yarn add moleculer
    

    Create your first microservice

    This example shows you how to create a small service with an add action which can add two numbers and how to call it.

    const { ServiceBroker } = require("moleculer");
    
    // Create a broker
    let broker = new ServiceBroker({ logger: console });
    
    // Create a service
    broker.createService({
        name: "math",
        actions: {
            add(ctx) {
                return Number(ctx.params.a) + Number(ctx.params.b);
            }
        }
    });
    
    // Start broker
    broker.start()
        // Call service
        .then(() => broker.call("math.add", { a: 5, b: 3 }))
        .then(res => console.log("5 + 3 =", res))
        .catch(err => console.error(`Error occurred! ${err.message}`));
    

    相关文章

      网友评论

          本文标题:moleculer使用

          本文链接:https://www.haomeiwen.com/subject/arphaqtx.html